1.把word文檔轉(zhuǎn)換成pdf
(1).添加引用
1 using Microsoft.Office.Interop.Word;添加引用
(2).轉(zhuǎn)換方法
1 /// <summary> 2 /// 把Word文件轉(zhuǎn)換成pdf文件 3 /// </summary> 4 /// <param name="sourcePath">需要轉(zhuǎn)換的文件路徑和文件名稱</param> 5 /// <param name="targetPath">轉(zhuǎn)換完成后的文件的路徑和文件名名稱</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;//轉(zhuǎn)換格式1.wdExportFormatPDF轉(zhuǎn)換成pdf格式 2.wdExportFormatXPS轉(zhuǎn)換成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;//需要轉(zhuǎn)格式的文件路徑18 string outputFileName = targetPath;//轉(zhuǎn)換完成后PDF或XPS文件的路徑和文件名名稱19 WdExportFormat exportFormat = wdExportFormatPDF;//導(dǎo)出文件所使用的格式20 bool openAfterExport = false;//轉(zhuǎn)換完成后是否打開21 WdExportOptimizeFor wdExportOptimizeForPRint = WdExportOptimizeFor.wdExportOptimizeForPrint;//導(dǎo)出方式1.wdExportOptimizeForPrint針對打印進(jìn)行導(dǎo)出,質(zhì)量較高,生成的文件大小較大。2.wdExportOptimizeForOnScreen 針對屏幕顯示進(jìn)行導(dǎo)出,質(zhì)量較差,生成的文件大小較小。22 WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//導(dǎo)出全部內(nèi)容(枚舉)23 int from = 0;//起始頁碼24 int to = 0;//結(jié)束頁碼25 WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定導(dǎo)出過程中是否只包含文本或包含文本的標(biāo)記.1.wdExportDocumentContent:導(dǎo)出文件沒有標(biāo)記,2.導(dǎo)出文件有標(biāo)記26 bool includeDocProps = true;//指定是否包含新導(dǎo)出的文件在文檔屬性27 bool keepIRM = true;//28 WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在導(dǎo)出文件中創(chuàng)建書簽,2.wdExportCreateHeadingBookmarks:標(biāo)題和文本框?qū)С龅奈募袆?chuàng)建一個書簽,3.wdExportCreateWordBookmarks每個字的書簽,其中包括除包含頁眉和頁腳中的所有書簽導(dǎo)出的文件中創(chuàng)建一個書簽。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文件轉(zhuǎn)換成pdf文件 3 /// </summary> 4 /// <param name="sourcePath">需要轉(zhuǎn)換的文件路徑和文件名稱</param> 5 /// <param name="targetPath">轉(zhuǎn)換完成后的文件的路徑和文件名名稱</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).調(diào)用
1 OfficeToPdf.WordToPdf("d://1234.doc", "d://1234.pdf");調(diào)用
2.把Excel文檔轉(zhuǎn)換成pdf
(1).添加引用
1 using Microsoft.Office.Interop.Excel;添加引用
(2).轉(zhuǎn)換方法
1 /// <summary> 2 /// 把Excel文件轉(zhuǎn)換成pdf文件 3 /// </summary> 4 /// <param name="sourcePath">需要轉(zhuǎn)換的文件路徑和文件名稱<
新聞熱點(diǎn)
疑難解答
圖片精選