当前位置:编程学习 > C#/ASP.NET >>

请问哪位大神能具体解释下下面把word和ppt格式转换成PDF格式的asp.net代码?急求,谢谢!

 1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Web;
  5 using System.Web.Security;
  6 using System.Web.UI;
  7 using System.Web.UI.WebControls;
  8 using System.Web.UI.WebControls.WebParts;
  9 using System.Web.UI.HtmlControls;
 10 using Word = Microsoft.Office.Interop.Word;
 11 using Excel = Microsoft.Office.Interop.Excel;
 12 using PowerPoint = Microsoft.Office.Interop.PowerPoint;
 13 using Microsoft.Office.Core;
 14 
 15 /// <summary>
 16 /// Office2Pdf 将Office文档转化为pdf
 17 /// </summary>
 18 public class Office2Pdf
 19 {
 20     public Office2Pdf()
 21     {
 22         //
 23 // TODO: 在此处添加构造函数逻辑
 24 //
 25     }
 26     /// <summary>
 27 /// Word转换成pdf
 28 /// </summary>
 29 /// <param name="sourcePath">源文件路径</param>
 30 /// <param name="targetPath">目标文件路径</param>
 31 /// <returns>true=转换成功</returns>
 32     public bool DOCConvertToPDF(string sourcePath, string targetPath)
 33     {
 34         bool result = false;
 35         Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
 36         object paramMissing = Type.Missing;
 37         Word.ApplicationClass wordApplication = new Word.ApplicationClass();
 38         Word.Document wordDocument = null;
 39         try
 40         {
 41             object paramSourceDocPath = sourcePath;
 42             string paramExportFilePath = targetPath;
 43             Word.WdExportFormat paramExportFormat = exportFormat;
 44             bool paramOpenAfterExport = false;
 45             Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
 46             Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
 47             int paramStartPage = 0;
 48             int paramEndPage = 0;
 49             Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
 50             bool paramIncludeDocProps = true;
 51             bool paramKeepIRM = true;
 52             Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
 53             bool paramDocStructureTags = true;
 54             bool paramBitmapMissingFonts = true;
 55             bool paramUseISO19005_1 = false;
 56             wordDocument = wordApplication.Documents.Open(
 57                 ref paramSourceDocPath, ref paramMissing, ref paramMissing,
 58                 ref paramMissing, ref paramMissing, ref paramMissing,
 59                 ref paramMissing, ref paramMissing, ref paramMissing,
 60                 ref paramMissing, ref paramMissing, ref paramMissing,
 61                 ref paramMissing, ref paramMissing, ref paramMissing,
 62                 ref paramMissing);
 63             if (wordDocument != null)
 64                 wordDocument.ExportAsFixedFormat(paramExportFilePath,
 65                     paramExportFormat, paramOpenAfterExport,
 66                     paramExportOptimizeFor, paramExportRange, paramStartPage,
 67                     paramEndPage, paramExportItem, paramIncludeDocProps,
 68                     paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
 69                     paramBitmapMissingFonts, paramUseISO19005_1,
 70                     ref paramMissing);
 71             result = true;
 72         }
 73         catch
 74         {
 75             result = false;
 76         }
 77         finally
 78         {
 79             if (wordDocument != null)
 80             {
 81                 wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
 82                 wordDocument = null;
 83             }
 84             if (wordApplication != null)
 85             {
 86                 wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
 87                 wordApplication = null;
 88             }
 89             GC.Collect();
 90             GC.WaitForPendingFinalizers();
 91             GC.Collect();
 92             GC.WaitForPendingFinalizers();
 93         }
 94         return result;
 95     }







144 /// 把PowerPoint文件转换成PDF格式文件       
145 ///</summary>        
146 ///<param name="sourcePath">源文件路径</param>     
147 ///<param name="targetPath">目标文件路径</param> 
148 ///<returns>true=转换成功</returns> 
149     public bool PPTConvertToPDF(string sourcePath, string targetPath)
150     {
151         bool result;
152         PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
153         object missing = Type.Missing;
154         PowerPoint.ApplicationClass application = null;
155         PowerPoint.Presentation persentation = null;
156         try
157         {
158             application = new PowerPoint.ApplicationClass();
159             persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
160             result = true;
161         }
162         catch
163         {
164             result = false;
165         }
166         finally
167         {
168             if (persentation != null)
169             {
170                 persentation.Close();
171                 persentation = null;
172             }
173             if (application != null)
174             {
175                 application.Quit();
176                 application = null;
177             }
178             GC.Collect();
179             GC.WaitForPendingFinalizers();
180             GC.Collect();
181             GC.WaitForPendingFinalizers();
182         }
183         return result;
184     }
185 } asp.net C#
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,