asp.net把word文档转换成pdf
类别:编程学习 浏览量:2383
时间:2014-2-23 asp.net把word文档转换成pdf
asp.net把word文档转换成pdf一、添加引用
using Microsoft.Office.Interop.Word;
二、转换方法
1、方法
/// <summary>
/// 把Word文件转换成pdf文件
/// </summary>
/// <param name="sourcePath">需要转换的文件路径和文件名称</param>
/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
/// <returns>成功返回true,失败返回false</returns>
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、简洁方法
/// <summary>
/// 把Word文件转换成pdf文件
/// </summary>
/// <param name="sourcePath">需要转换的文件路径和文件名称</param>
/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
/// <returns>成功返回true,失败返回false</returns>
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");
您可能感兴趣
- python分词操作(Python英文文本分词无空格模块wordninja的使用实例)
- html转ppt(HTML里显示pdf、word、xls、ppt的方法示例)
- mysql出现的问题及解决方法(mysql升级到5.7时,wordpress导数据报错1067的问题)
- react 查看word文件(React实现导入导出Excel文件)
- asp.net把word文档转换成pdf
- python中jieba库怎么用(详解Python数据可视化编程 - 词云生成并保存jieba+WordCloud)
- asp.net如何实现word文档在线预览
- word-wrap和break-word的区别
- MVC中报get_RazorKeywords()错误的解决方法
- Asp.net操作Word文档
- wordpress如何在文章中自定义html(wordpress添加Html5的表单验证required方法小结)
- 织梦cms怎么调用(织梦CMS采集时keywords字段无法添加过滤规则的解决方法)
- 怎么把织梦的dede改掉(织梦dedecms 提示 body has not allow words 问题解决)
- letter-spacing和word-spacing区别
- python读取word的表格(Python使用reportlab模块生成PDF格式的文档)
- html基本标签大全(HTML中meta标签及Keywords)
- 杭州旅游攻略()
- 云南旅游攻略(云南旅游攻略5天攻略)
- 收藏 春节假期,这些景区巨划算(收藏春节假期这些景区巨划算)
- 景区游玩,这些安全知识要牢记(这些安全知识要牢记)
- 各地出招烘 热 旅游(各地出招烘热旅游)
- 2021款起亚霸锐到店了 更换车标,竞争宝马X5有戏吗(2021款起亚霸锐到店了)
热门推荐
- windows 安装解压版 mysql5.7.28 winx64的详细教程(windows 安装解压版 mysql5.7.28 winx64的详细教程)
- html5canvas图形怎么打印(HTML5 Canvas 实现K线图的示例代码)
- dedecms字数代码(用dedecms实现拼音显示文章标题页文件名的方法)
- 最好的虚拟主机(如何选择适合自己的香港虚拟主机?)
- 云服务选什么系统(云服务器用什么系统好)
- 使用vue组件开发项目(Vue记事本实例详解)
- 百度网和阿里云哪个好用(百度云服务器bcc有什么优势?)
- canvas如何将一组数据生成柱子图(Canvas环形饼图与手势控制的实现代码)
- vue3.0安装element(vue3+electron12+dll开发客户端配置详解)
- zabbix如何监控访问状态码(基于zabbix实现监控Jenkins过程详解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9