word ppt excel文档转换成pdf

8/10/2015来源:ASP.NET技巧人气:1448

Word PPT Excel文档转换成pdf

1.把word文档转换成pdf

(1).添加引用

1 using Microsoft.Office.Interop.Word;
添加引用

(2).转换方法

 1 /// <summary> 2     /// 把Word文件转换成pdf文件 3     /// </summary> 4     /// <param name="sourcePath">需要转换的文件路径和文件名称</param> 5     /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param> 6     /// <returns>成功返回true,失败返回false</returns> 7     public static bool WordToPdf(string sourcePath, string targetPath) 8     { 9         bool result = false;10         WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式11         object missing = Type.Missing;12         Microsoft.Office.Interop.Word.applicationClass applicationClass = null;13         Document document = null;14         try15         {16             applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();17             object inputfileName = sourcePath;//需要转格式的文件路径18             string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称19             WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式20             bool openAfterExport = false;//转换完成后是否打开21             WdExportOptimizeFor wdExportOptimizeForPRint = WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,质量较差,生成的文件大小较小。22             WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全部内容(枚举)23             int from = 0;//起始页码24             int to = 0;//结束页码25             WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记26             bool includeDocProps = true;//指定是否包含新导出的文件在文档属性27             bool keepIRM = true;//28             WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。29             bool docStructureTags = true;30             bool bitmapMissingFonts = true;31             bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)32             document = applicationClass.Documents.Open(ref inputfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);33             if (document != null)34             {35                 document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing);36             }37             result = true;38         }39         catch40         {41             result = false;42         }43         finally44         {45             if (document != null)46             {47                 document.Close(ref missing, ref missing, ref missing);48                 document = null;49             }50             if (applicationClass != null)51             {52                 applicationClass.Quit(ref missing, ref missing, ref missing);53                 applicationClass = null;54             }55         }56         return result;57     }
方法
 1     /// <summary> 2     /// 把Word文件转换成pdf文件 3     /// </summary> 4     /// <param name="sourcePath">需要转换的文件路径和文件名称</param> 5     /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param> 6     /// <returns>成功返回true,失败返回false</returns> 7     public static bool WordToPdf(object sourcePath, string targetPath) 8     { 9         bool result = false;10         WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;11         object missing = Type.Missing;12         Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;13         Document document = null;14         try15         {16             applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();17             document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);18             if (document != null)19             {20                 document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);21             }22             result = true;23         }24         catch25         {26             result = false;27         }28         finally29         {30             if (document != null)31             {32                 document.Close(ref missing, ref missing, ref missing);33                 document = null;34             }35             if (applicationClass != null)36             {37                 applicationClass.Quit(ref missing, ref missing, ref missing);38                 applicationClass = null;39             }40         }41         return result;42     }
简洁方法

(3).调用

1 OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");
调用

2.把Excel文档转换成pdf

(1).添加引用

1 using Microsoft.Office.Interop.Excel;
添加引用

(2).转换方法

 1     /// <summary> 2     /// 把Excel文件转换成pdf文件 3     /// </summary> 4     /// <param name="sourcePath">需要转换的文件路径和文件名称<