C#实现office文档转换为PDF格式

1.安装组件OfficeSaveAsPDFandXPS

需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS

下载地址
OfficeSaveAsPDFandXPS  [url]http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en[/url]
office 2007     链接:http://pan.baidu.com/s/1eSeQD38密码:j5qy
这是一个微软官方出的office插件。
2.安装好之后,打开VS,以VS2013为例
2.1添加以下组件的引用
“项目”--“引用”--“添加引用..”--在程序集中搜索"Microsoft",找到如下3个dll
Microsoft.Office.Interop.Word
Microsoft.Office.Interop.PowerPoint
microsoft.office.interop.Excel

dll成功引入后,项目的引用里面则会出现相应的dll控件名称。

修改上图中3个dll的属性"嵌入互操作类型"为False

引入dll:Microsoft.Office.Core

文件版本为15.0.5031.1000

该dll的文件名称为OFFICE.DLL

即可看到如下图的dll已引入成功

2.2相应类文件顶部添加以下组件的引用

using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Word;

我们可以使用一个枚举类型来决定生成文件的类型
Word.WdExportFormat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Excel.XlFixedFormatType excelType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
PowerPoint.PpSaveAsFileType ppType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
2.3相应类文件中的转换代码

        #region office文件转换为pdf文件/// <summary>/// 将word文档转换成PDF格式/// </summary>/// <param name="sourcePath">源文件路径</param>/// <param name="targetPath">目标文件路径</param> /// <param name="exportFormat"></param>/// <returns></returns>private bool WordConvertPDF(string sourcePath, string targetPath, Word.WdExportFormat exportFormat){bool result;object paramMissing = Type.Missing;Word.ApplicationClass wordApplication = new Word.ApplicationClass();  Word.Document wordDocument = null;try{object paramSourceDocPath = sourcePath;string paramExportFilePath = targetPath;Word.WdExportFormat paramExportFormat = exportFormat;bool paramOpenAfterExport = false;Word.WdExportOptimizeFor paramExportOptimizeFor =Word.WdExportOptimizeFor.wdExportOptimizeForPrint;Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;int paramStartPage = 0;int paramEndPage = 0;Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;bool paramIncludeDocProps = true;bool paramKeepIRM = true;Word.WdExportCreateBookmarks paramCreateBookmarks =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,ref paramMissing, 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);result = true;}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;}/// <summary>/// 将excel文档转换成PDF格式/// </summary>/// <param name="sourcePath">源文件路径</param>/// <param name="targetPath">目标文件路径</param> /// <param name="targetType"></param>/// <returns></returns>private bool ExcelConvertPDF(string sourcePath, string targetPath, XlFixedFormatType targetType){bool result;object missing = Type.Missing;Excel.ApplicationClass application = null;Workbook workBook = null;try{application = new Excel.ApplicationClass();object target = targetPath;object type = targetType;workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,missing, missing, missing, missing, missing, missing, missing, missing, missing);workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);result = true;}catch{result = false;}finally{if (workBook != null){workBook.Close(true, missing, missing);workBook = null;}if (application != null){application.Quit();application = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();}return result;}/// <summary>/// 将ppt文档转换成pdf格式/// </summary>/// <param name="sourcePath">源文件路径</param>/// <param name="targetPath">目标文件路径</param> /// <param name="targetFileType"></param>/// <returns></returns>private bool PPTConvertPDF(string sourcePath, string targetPath, PpSaveAsFileType targetFileType){bool result;object missing = Type.Missing;PowerPoint.ApplicationClass application = null;Presentation persentation = null;try{application = new PowerPoint.ApplicationClass();persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);result = true;}catch{result = false;}finally{if (persentation != null){persentation.Close();persentation = null;}if (application != null){application.Quit();application = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();}return result;}#endregion

View Code

权限配置第一种方案:

在组件在服务器中添加权限

单击开始,单击运行, 然后键入 DCOMCNFG。选择要自动运行的应用程序。应用程序名称如下所示:

Microsoft Access 97 - Microsoft Access 数据库
Microsoft Access 2000/2002 - Microsoft Access 应用程序
Microsoft Excel 97/2000/2002 - Microsoft Excel 应用程序
Microsoft Word 97 - Microsoft Word Basic
Microsoft Word 2000/2002 - Microsoft Word 文档

单击属性打开此应用程序的属性对话框。

1)标示—运行此应用程序的用户账户—下列用户;然后输入Administrator用户组中的一个用户。

注:更改服务器中组件服务所用到的用户密码时,须在组件中重新输入新的密码,才能正常生成Word。

2)安全—启动和激活权限,选择“自定义”,添加IIS_WPG用户的本地启动、本地激活权限;

3)安全—访问权限,选择“自定义”,添加IIS_WPG用户的本地访问权限;

权限配置第二种方案:

对DCOM组件进行权限配置:

1、打开comexp.msc -32

2、Microsoft Excel Application、和Microsoft Word 97-2003 Document属性里面进行配置,如下:

  标识:设为“交互式用户”

  安全:启动和激活权限添加“NETWORK SERVICE”,勾选本地启动和本地激活,访问权限添加类似

以上两点设置完成后还有问题,继续以下操作:

3、应用进程池标识转换为“LocalSystem”

4、在C:/Windows/System32/config/systemprofile和C:/Windows/SysWOW64/config/systemprofile目录下创建名为Desktop目录

出现的问题:发布到服务器上后,WORD转换没问题,EXCEL转换PDF时转换卡住,只能生成temp的临时文件,以下操作解决问题:

5、在DCOM组件的Microsoft Excel Application、和Microsoft Word 97-2003 Document属性安全中额外添加“IIS_IUSRS”用户组,权限跟之前的“NETWORL SERVICE”一样

引自:http://www.cnblogs.com/louby/p/7053262.html

另一功能:word文档或者Excel文档想要转换xps格式

地址:https://blog.csdn.net/guochunyang/article/details/69549231

posted on 2018-05-10 14:25 永不言弃! 阅读(...) 评论(...) 编辑 收藏

C#实现office文档转换为PDF格式相关推荐

  1. 【软件操作】Office将Word文档转换为PDF格式

    一.问题描述 PDF格式(Portable Document Format)相比于Word文档(.doc/.docx格式)体积更大,但具有更好的文档一致性(减少排版问题),修改更困难(不易被外界篡改) ...

  2. 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf...

    第三步,使用JODConverter将office文档转换为pdf JODConverter是一个java的OpenDucument文件转换器,可以进行许多文件格式的转换,它利用 OpenOffice ...

  3. wps中将文档输出为pdf_如何将wps文档转换为pdf格式

    wps文件是工作场所中常用的文件格式.WPS工具通常用于编写,编辑和修改文件.因此,wps工具操作简单,功能齐全,非常符合日常办公的需求.但是,wps文档也存在这样的缺陷,即wps文档的文本内容太容易 ...

  4. linux ubuntu下怎样将pdf格式文件转换为doc格式文件,如何在Ubuntu命令行上将文档转换为PDF格式...

    在打印,共享和通过电子邮件发送文档(尤其是较大的文档)时,PDF或可移植文档格式通常是首选.对于Windows和macOS,您可能非常熟悉,也依赖于广泛使用的Acrobat产品进行pdf创建,查看和编 ...

  5. 简要介绍word文档转换为pdf格式文档的工具

    找了很多工具,其实都是乱七八糟的,没几个好用的,最好还是用Adobe Acrobat Pro吧,这个就很方便了,而且转换的也不错. ABC Amber PDF Converter ABC Amber ...

  6. 用ASP.Net实现将Word文档转换为PDF格式

    前言:由于一个客户的项目中需要将WORD文档转换成PDF格式,故写了本篇实站教程 需求分析:客户的项目以B/S结构为主,提供一个WORD文件在后台自动转换成PDF,经过实际测试,如果该篇WORD文档有 ...

  7. java命令行利用libreoffice将office文档转换为pdf文件失败的解决方案

    原因:libreoffice同时只支持两个文档转换的命令行,多于两个的命令行不会进行文档转换. 解决方案:在线程池的线程中,利用java阻塞队列BlockingQueue,设置容量为2,同时只允许两个 ...

  8. (java office转pdf) MS Office2010、WPS2015、OpenOffice4用Java将Office文档转换为PDF,WIN7 64位系统

    1.前言 MS Office2010是可以的,理论上2007版本也可以,博主没试过: Wps2015是可以的,理论上Wps2016也能用,Wps理论上还兼容MS Office的相关代码,有时间的可以试 ...

  9. Java 将 Word 文档转换为 PDF 的完美工具

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:为什么魂斗罗只有 128 KB却可以实现那么长的剧情?个人原创+1博客:点击前往,查看更多 来源:https:/ ...

最新文章

  1. HDU 1052 Tian Ji -- The Horse Racing
  2. C语言嵌入式系统编程修炼之六性能优化
  3. 用AsyncCtp实现一个简单的EchoServer
  4. java接口常见问题分析_常见问题 - Apache ServiceComb
  5. 监控mysql主从复制监控_shell脚本监控mysql主从同步状态
  6. unity 阳光插件_网络广告,阳光创信保驾护航
  7. Android ContentProvider支持跨进程数据共享与互斥、同步 杂谈
  8. Routerboard/DR4019S-Qualcomm-IPQ4019-2T2R-Dual-Band-2-4GHz-5GHz-support-OpenWRT-802.11ac-Wave-2.
  9. python联合vrep_python控制vrep代码实例
  10. 笔记本电脑计算机恢复出厂设置密码,笔记本电脑怎么恢复出厂设置?
  11. 【Latex系列】括号用法总结
  12. 罗格斯的计算机科学博士奖学金,本科直博如何“牛”转乾坤斩获全奖博士录取,师兄制胜申请经历大揭秘!...
  13. 机器学习(回归五)——线性回归-局部加权线性回归
  14. Geekban极客班 第三周
  15. 程序员都是这样关机的
  16. 《奇特的一生》读书笔记
  17. html拖动控件详解
  18. 王者荣耀qq区服务器位置,王者荣耀:qq区单排现状,射手可能是最难的一个位置,为什么?...
  19. 指令大全(win+r)
  20. 三菱M70仿真系统软件PLC 相似度百分之百 真机

热门文章

  1. 20214201 Python技能树及CSDN MarkDown编辑器测评
  2. 【MySQL 教程】达达集团实时计算任务SQL化实践
  3. 高考英语68分,大一通过英语四六级,考研英语80分!做到这些,你也可以!
  4. DBA的工作职责和每日工作
  5. 验证码验证不同步处理方法
  6. chrome浏览器插件程序包失效解决办法
  7. Toon Boom Storyboard Pro 20中文安装使用教程
  8. 最流行的编程语言JavaScript能做什么?
  9. 三位一体报考计算机专业自我介绍,考三位一体招生自我介绍范文
  10. 使用vagrant工具来管理和创建虚拟机