1.把word文檔轉換成pdfapp
(1).添加引用ide
1 using Microsoft.Office.Interop.Word;
(2).轉換方法ui
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 }
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).調用spa
1 OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");
2.把Excel文檔轉換成pdfcode
(1).添加引用orm
1 using Microsoft.Office.Interop.Excel;
(2).轉換方法blog
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 }
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).調用文檔
1 OfficeToPdf.ExcelToPdf("d:\\1234.xls", "d:\\1234.pdf");
3.把ppt轉換成pdfget
(1).添加引用input
1 using Microsoft.Office.Core; 2 using Microsoft.Office.Interop.PowerPoint;
(2).轉換方法
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).調用
1 OfficeToPdf.PPTToPDF("d:\\12345.pptx", "d:\\12345.pdf");