博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET将word文档转换成pdf的代码
阅读量:7247 次
发布时间:2019-06-29

本文共 4522 字,大约阅读时间需要 15 分钟。

一、添加引用

using Microsoft.Office.Interop.Word;

 

二、转换方法

 

1、方法

 
C# 代码   复制
///     /// 把Word文件转换成pdf文件    ///     /// 需要转换的文件路径和文件名称    /// 转换完成后的文件的路径和文件名名称    /// 
成功返回true,失败返回false
public static bool WordToPdf(string sourcePath, string targetPath){ bool result = false; WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式 object missing = Type.Missing; Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null; Document document = null; try{ applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass(); object inputfileName = sourcePath;//需要转格式的文件路径 string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称 WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式 bool openAfterExport = false;//转换完成后是否打开 WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,质量较差,生成的文件大小较小。 WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全部内容(枚举) int from = 0;//起始页码 int to = 0;//结束页码 WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记 bool includeDocProps = true;//指定是否包含新导出的文件在文档属性 bool keepIRM = true;// WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。 bool docStructureTags = true; bool bitmapMissingFonts = true; bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A) 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); if (document != null){ document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing); } result = true; } catch{ result = false; } finally{ if (document != null) { document.Close(ref missing, ref missing, ref missing); document = null; } if (applicationClass != null) { applicationClass.Quit(ref missing, ref missing, ref missing); applicationClass = null; } } return result; }

 

2、简洁方法

 

C# 代码   
复制
///     /// 把Word文件转换成pdf文件    ///     /// 需要转换的文件路径和文件名称    /// 转换完成后的文件的路径和文件名名称    /// 
成功返回true,失败返回false
public static bool WordToPdf(object sourcePath, string targetPath){ bool result = false; WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF; object missing = Type.Missing; Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null; Document document = null; try{ applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass(); 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); if (document != null){ document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing); } result = true; } catch{ result = false; } finally{ if (document != null) { document.Close(ref missing, ref missing, ref missing); document = null; } if (applicationClass != null) { applicationClass.Quit(ref missing, ref missing, ref missing); applicationClass = null; } } return result; }

 

三、调用

 OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");

转载地址:http://funbm.baihongyu.com/

你可能感兴趣的文章
openssl rand 指令解析
查看>>
ubuntu下minicom超级终端的使用方法
查看>>
迅为iTOP-4412核心板调整电压
查看>>
求两个数的最大公约数(辗转相除法)
查看>>
Linux 中gdb调试工具的使用
查看>>
设计模式系列 - 策略模式
查看>>
Windows 2012R2安装KB2919355失败
查看>>
系统集成网络工程师所具备的知识
查看>>
正则表达式
查看>>
Vue.js学习笔记: 插值
查看>>
linux常用命令
查看>>
WooCommerce 支付宝扫码支付与银行直连
查看>>
mysql慢查询日志
查看>>
Office 365系列之九:使用Windows PowerShell管理O365平台
查看>>
CenOS 6.0配置本地yum源
查看>>
小作文_通知和备忘录
查看>>
06-Windows Server 2012 R2 会话远程桌面-标准部署-RD网关(RemoteApp)
查看>>
Alcatel 7750 常用维护命令
查看>>
mysql AUTO_INCREMENT 字符 删除后重1开始
查看>>
IOS应用从容地崩溃
查看>>