word ppt excel文档转换成pdf

1.把word文档转换成pdf

(1).添加引用

word ppt excel文档转换成pdf
1 using Microsoft.Office.Interop.Word;
添加引用

(2).转换方法

word ppt excel文档转换成pdf
 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         try
15         {
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         catch
40         {
41             result = false;
42         }
43         finally
44         {
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     }
方法
word ppt excel文档转换成pdf
 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         try
15         {
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         catch
25         {
26             result = false;
27         }
28         finally
29         {
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).调用

word ppt excel文档转换成pdf
1 OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");
调用

2.把Excel文档转换成pdf

(1).添加引用

word ppt excel文档转换成pdf
1 using Microsoft.Office.Interop.Excel;
添加引用

(2).转换方法

word ppt excel文档转换成pdf
 1     /// <summary>
 2     /// 把Excel文件转换成pdf文件
 3     /// </summary>
 4     /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
 5     /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
 6     /// <returns></returns>
 7     public static bool ExcelToPdf(string sourcePath, string targetPath)
 8     {
 9         bool result = false;
10         XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
11         object missing = Type.Missing;
12         Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
13         Workbook workbook = null;
14         try
15         {
16             applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
17             string inputfileName = sourcePath;//需要转格式的文件路径
18             string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称
19             XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
20             XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量
21             bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
22             bool openAfterPublish = false;//发布后不打开
23             workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
24             if (workbook!=null)
25             {
26                 workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
27             }
28             result = true;
29         }
30         catch
31         {
32             result = false;
33         }
34         finally
35         {
36             if (workbook != null)
37             {
38                 workbook.Close(true, missing, missing);
39                 workbook = null;
40             }
41             if (applicationClass != null)
42             {
43                 applicationClass.Quit();
44                 applicationClass = null;
45             }
46         }
47         return result;
48     }
方法
word ppt excel文档转换成pdf
 1     /// <summary>
 2     /// 把Excel文件转换成pdf文件
 3     /// </summary>
 4     /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
 5     /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
 6     /// <returns></returns>
 7     public static bool ExcelToPdf(string sourcePath, string targetPath)
 8     {
 9         bool result = false;
10         XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
11         object missing = Type.Missing;
12         Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
13         Workbook workbook = null;
14         try
15         {
16             applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
17             workbook = applicationClass.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
18             if (workbook != null)
19             {
20                 workbook.ExportAsFixedFormat(xlTypePDF, targetPath, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
21             }
22             result = true;
23         }
24         catch
25         {
26             result = false;
27         }
28         finally
29         {
30             if (workbook != null)
31             {
32                 workbook.Close(true, missing, missing);
33                 workbook = null;
34             }
35             if (applicationClass != null)
36             {
37                 applicationClass.Quit();
38                 applicationClass = null;
39             }
40         }
41         return result;
42     }
简洁方法

(3).调用

word ppt excel文档转换成pdf
1 OfficeToPdf.ExcelToPdf("d:\\1234.xls", "d:\\1234.pdf");
调用

3.把ppt转换成pdf

(1).添加引用

word ppt excel文档转换成pdf
1 using Microsoft.Office.Core;
2 using Microsoft.Office.Interop.PowerPoint;
添加引用

(2).转换方法

word ppt excel文档转换成pdf
 1     ///<summary>        
 2     /// 把PowerPoint文件转换成PDF格式文件       
 3     ///</summary>        
 4     ///<param name="sourcePath">源文件路径</param>     
 5     ///<param name="targetPath">目标文件路径</param> 
 6     ///<returns>成功返回true,失败返回false</returns> 
 7     public static bool PPTConvertToPDF(string sourcePath, string targetPath)
 8     {
 9         bool result;
10         PpSaveAsFileType ppSaveAsFileType = PpSaveAsFileType.ppSaveAsPDF;//转换成pdf
11         object missing = Type.Missing;
12         Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
13         Presentation persentation = null;
14         try
15         {
16             application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
17             persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
18             if (persentation!=null)
19             {
20                 persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue);
21             }
22             result = true;
23         }
24         catch
25         {
26             result = false;
27         }
28         finally
29         {
30             if (persentation != null)
31             {
32                 persentation.Close();
33                 persentation = null;
34             }
35             if (application != null)
36             {
37                 application.Quit();
38                 application = null;
39             }
40         }
41         return result;
42     }
方法

(3).调用

word ppt excel文档转换成pdf
1 OfficeToPdf.PPTToPDF("d:\\12345.pptx", "d:\\12345.pdf");
调用

 

 

 

 

 

 

word ppt excel文档转换成pdf

上一篇:三、Kubernetes集群的命令行工具kubectl


下一篇:PHP 函数