.Net 调用wordCOM组件转PDF
生活随笔
收集整理的這篇文章主要介紹了
.Net 调用wordCOM组件转PDF
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
運行環境:office2010或office2007+SaveAsPDFandXPS.exe補丁 x86x64系統環境均可。
class Program { static void Main(string[] args) { DOCConvertToPDF(AppDomain.CurrentDomain.BaseDirectory + "/b.html", AppDomain.CurrentDomain.BaseDirectory + "/aa.pdf"); Console.ReadKey(); }
//Word轉換成pdf /// <summary> /// 把Word文件轉換成為PDF格式文件 /// </summary> /// <param name="sourcePath">源文件路徑</param> /// <param name="targetPath">目標文件路徑</param>? /// <returns>true=轉換成功</returns> public static bool DOCConvertToPDF(string sourcePath, string targetPath) { bool result = false; Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF; object paramMissing = Type.Missing; Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document wordDocument = null; try { object paramSourceDocPath = sourcePath; string paramExportFilePath = targetPath; Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat; bool paramOpenAfterExport = false; Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint; Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument; int paramStartPage = 0; int paramEndPage = 0; Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent; bool paramIncludeDocProps = true; bool paramKeepIRM = true; Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; bool paramDocStructureTags = true; bool paramBitmapMissingFonts = true; bool paramUseISO19005_1 = false; wordDocument = wordApplication.Documents.Open( ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, WdOpenFormat.wdOpenFormatWebPages , ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);
if (wordDocument != null) wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); Console.WriteLine("success!"); result = true; } catch (Exception ex) { Console.WriteLine(ex.StackTrace); result = false; } finally { if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null; } if (wordApplication != null) { wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; }
}
class Program { static void Main(string[] args) { DOCConvertToPDF(AppDomain.CurrentDomain.BaseDirectory + "/b.html", AppDomain.CurrentDomain.BaseDirectory + "/aa.pdf"); Console.ReadKey(); }
//Word轉換成pdf /// <summary> /// 把Word文件轉換成為PDF格式文件 /// </summary> /// <param name="sourcePath">源文件路徑</param> /// <param name="targetPath">目標文件路徑</param>? /// <returns>true=轉換成功</returns> public static bool DOCConvertToPDF(string sourcePath, string targetPath) { bool result = false; Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF; object paramMissing = Type.Missing; Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document wordDocument = null; try { object paramSourceDocPath = sourcePath; string paramExportFilePath = targetPath; Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat; bool paramOpenAfterExport = false; Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint; Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument; int paramStartPage = 0; int paramEndPage = 0; Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent; bool paramIncludeDocProps = true; bool paramKeepIRM = true; Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; bool paramDocStructureTags = true; bool paramBitmapMissingFonts = true; bool paramUseISO19005_1 = false; wordDocument = wordApplication.Documents.Open( ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, WdOpenFormat.wdOpenFormatWebPages , ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);
if (wordDocument != null) wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); Console.WriteLine("success!"); result = true; } catch (Exception ex) { Console.WriteLine(ex.StackTrace); result = false; } finally { if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null; } if (wordApplication != null) { wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; }
}
總結
以上是生活随笔為你收集整理的.Net 调用wordCOM组件转PDF的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mssql sqlserver 不固定行
- 下一篇: centos 6.x 64位 运行32位