用Word写博客园文章
?
http://www.cnblogs.com/dunitian/services/metablogapi.aspx
?
效果如下:
啦啦啦啦啦啦我是賣報的小行家
| /// <summary> /// Word轉換成PDF /// </summary> /// <param name="inputPath">載入路徑</param> /// <param name="outputPath">保存路徑</param> /// <param name="startPage">初始頁碼(默認為第一頁[0])</param> /// <param name="endPage">結束頁碼(默認為最后一頁)</param> public static bool WordToPDF(string inputPath, string outputPath, int startPage = 0, int endPage = 0) { bool b = true; ? #region初始化 //初始化一個application Application wordApplication = new Application(); //初始化一個document Document wordDocument = null; #endregion ? #region參數設置~~我去累死寶寶了~~ //word路徑 object wordPath = Path.GetFullPath(inputPath); ? //輸出路徑 string pdfPath = Path.GetFullPath(outputPath); ? //導出格式為PDF WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF; ? //導出大文件 WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; ? //導出整個文檔 WdExportRange wdExportRange = WdExportRange.wdExportAllDocument; ? //開始頁碼 int startIndex = startPage; ? //結束頁碼 int endIndex = endPage; ? //導出不帶標記的文檔(這個可以改) WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent; ? //包含word屬性 bool includeDocProps = true; ? //導出書簽 WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; ? //默認值 object paramMissing = Type.Missing; ? #endregion ? #region轉換 try { //打開word wordDocument = wordApplication.Documents.Open(ref wordPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); //轉換成指定格式 if (wordDocument != null) { wordDocument.ExportAsFixedFormat(pdfPath, wdExportFormat, false, wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true, paramCreateBookmarks, true, true, false, ref paramMissing); } } catch (Exception ex) { b = 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; } } ? return b; #endregion } |
轉載于:https://www.cnblogs.com/dunitian/p/5041062.html
總結
以上是生活随笔為你收集整理的用Word写博客园文章的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3.7 代码注释和编码规范
- 下一篇: 打通两台机器的ssh功能