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");
您可能感兴趣
- mysql出现的问题及解决方法(mysql升级到5.7时,wordpress导数据报错1067的问题)
- 怎么把织梦的dede改掉(织梦dedecms 提示 body has not allow words 问题解决)
- 微信昵称python(Python 微信之获取好友昵称并制作wordcloud的实例)
- word-wrap和break-word的区别
- linux lnmp安装教程(LNMP系列教程之 SSL安装WordPress博客程序下载与安装)
- html基本标签大全(HTML中meta标签及Keywords)
- python中jieba库怎么用(详解Python数据可视化编程 - 词云生成并保存jieba+WordCloud)
- asp.net如何实现word文档在线预览
- letter-spacing和word-spacing区别
- 织梦cms怎么调用(织梦CMS采集时keywords字段无法添加过滤规则的解决方法)
- wordpress如何在文章中自定义html(wordpress添加Html5的表单验证required方法小结)
- wordpress的静态php页面在哪(如何让PHPnow支持wordpress静态化链接的方法)
- php把当前网页生成pdf(php转换上传word文件为PDF的方法基于COM组件)
- ASP.NET将WORD、PDF、PPT转为图片
- word-break、word-wrap、white-space的用法
- Asp.net操作Word文档
- 《金陵秘事》的剧情跌宕起伏 给观众带来的怎样的感官体验(金陵秘事的剧情跌宕起伏)
- 少儿口才表达影响未来一生,50首经典绕口令和孩子玩出聪明大脑(少儿口才表达影响未来一生)
- 玩网游居然让人更友善 很难以让人置信(玩网游居然让人更友善)
- 学好汉语拼音,从娃娃绕口令抓起,平时还是要多练 收藏好(从娃娃绕口令抓起)
- 仙女们的私藏鲜法大PK 鲜香切块牛肉(仙女们的私藏鲜法大PK)
- 天热没胃口 这道菜开胃又下饭,2个小技巧新手一学就会(这道菜开胃又下饭)
热门推荐
- mysql表结构设计分享(MySql开发之自动同步表结构)
- python怎样看字符unicode编码(Python3中编码与解码之Unicode与bytes的讲解)
- phpsession方法(PHP SESSION机制的理解与实例)
- IIS8.0安装配置完整图文教程 IIS8.0的Web Server asp设置(IIS8.0安装配置完整图文教程 IIS8.0的Web Server asp设置)
- laravel异步日志(laravel异步监控定时调度器实例详解)
- phpstudy搭建网站全过程(phpstudy在本地搭建多个网站的方法)
- 面试二叉树问的多吗(面试官:谈谈你对索引的认知系列之B-树)
- extjs anchor 锚点布局
- docker 容器怎么访问宿主机网络(Docker绑定固定IP/跨主机容器互访操作)
- mysql命令输入行在哪(MySQL命令行操作时的编码问题详解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9