1.安装组件OfficeSaveAsPDFandXPS

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

下载地址
  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相应类文件中的转换代码
  1. #region office文件转换为pdf文件
  2.  
  3. /// <summary>
  4. /// 将word文档转换成PDF格式
  5. /// </summary>
  6. /// <param name="sourcePath">源文件路径</param>
  7. /// <param name="targetPath">目标文件路径</param>
  8. /// <param name="exportFormat"></param>
  9. /// <returns></returns>
  10. private bool WordConvertPDF(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
  11. {
  12. bool result;
  13. object paramMissing = Type.Missing;
  14. Word.ApplicationClass wordApplication = new Word.ApplicationClass();
  15. Word.Document wordDocument = null;
  16. try
  17. {
  18. object paramSourceDocPath = sourcePath;
  19. string paramExportFilePath = targetPath;
  20.  
  21. Word.WdExportFormat paramExportFormat = exportFormat;
  22. bool paramOpenAfterExport = false;
  23. Word.WdExportOptimizeFor paramExportOptimizeFor =
  24. Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
  25. Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
  26. int paramStartPage = ;
  27. int paramEndPage = ;
  28. Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
  29. bool paramIncludeDocProps = true;
  30. bool paramKeepIRM = true;
  31. Word.WdExportCreateBookmarks paramCreateBookmarks =
  32. Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
  33. bool paramDocStructureTags = true;
  34. bool paramBitmapMissingFonts = true;
  35. bool paramUseISO19005_1 = false;
  36.  
  37. wordDocument = wordApplication.Documents.Open(
  38. ref paramSourceDocPath, ref paramMissing, ref paramMissing,
  39. ref paramMissing, ref paramMissing, ref paramMissing,
  40. ref paramMissing, ref paramMissing, ref paramMissing,
  41. ref paramMissing, ref paramMissing, ref paramMissing,
  42. ref paramMissing, ref paramMissing, ref paramMissing,
  43. ref paramMissing);
  44.  
  45. if (wordDocument != null)
  46. wordDocument.ExportAsFixedFormat(paramExportFilePath,
  47. paramExportFormat, paramOpenAfterExport,
  48. paramExportOptimizeFor, paramExportRange, paramStartPage,
  49. paramEndPage, paramExportItem, paramIncludeDocProps,
  50. paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
  51. paramBitmapMissingFonts, paramUseISO19005_1,
  52. ref paramMissing);
  53. result = true;
  54. }
  55. finally
  56. {
  57. if (wordDocument != null)
  58. {
  59. wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
  60. wordDocument = null;
  61. }
  62. if (wordApplication != null)
  63. {
  64. wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
  65. wordApplication = null;
  66. }
  67. GC.Collect();
  68. GC.WaitForPendingFinalizers();
  69. GC.Collect();
  70. GC.WaitForPendingFinalizers();
  71. }
  72. return result;
  73. }
  74.  
  75. /// <summary>
  76. /// 将excel文档转换成PDF格式
  77. /// </summary>
  78. /// <param name="sourcePath">源文件路径</param>
  79. /// <param name="targetPath">目标文件路径</param>
  80. /// <param name="targetType"></param>
  81. /// <returns></returns>
  82. private bool ExcelConvertPDF(string sourcePath, string targetPath, XlFixedFormatType targetType)
  83. {
  84. bool result;
  85. object missing = Type.Missing;
  86. Excel.ApplicationClass application = null;
  87. Workbook workBook = null;
  88. try
  89. {
  90. application = new Excel.ApplicationClass();
  91. object target = targetPath;
  92. object type = targetType;
  93. workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
  94. missing, missing, missing, missing, missing, missing, missing, missing, missing);
  95.  
  96. workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
  97. result = true;
  98. }
  99. catch
  100. {
  101. result = false;
  102. }
  103. finally
  104. {
  105. if (workBook != null)
  106. {
  107. workBook.Close(true, missing, missing);
  108. workBook = null;
  109. }
  110. if (application != null)
  111. {
  112. application.Quit();
  113. application = null;
  114. }
  115. GC.Collect();
  116. GC.WaitForPendingFinalizers();
  117. GC.Collect();
  118. GC.WaitForPendingFinalizers();
  119. }
  120. return result;
  121. }
  122.  
  123. /// <summary>
  124. /// 将ppt文档转换成pdf格式
  125. /// </summary>
  126. /// <param name="sourcePath">源文件路径</param>
  127. /// <param name="targetPath">目标文件路径</param>
  128. /// <param name="targetFileType"></param>
  129. /// <returns></returns>
  130. private bool PPTConvertPDF(string sourcePath, string targetPath, PpSaveAsFileType targetFileType)
  131. {
  132. bool result;
  133. object missing = Type.Missing;
  134. PowerPoint.ApplicationClass application = null;
  135. Presentation persentation = null;
  136. try
  137. {
  138. application = new PowerPoint.ApplicationClass();
  139. persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
  140. persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
  141.  
  142. result = true;
  143. }
  144. catch
  145. {
  146. result = false;
  147. }
  148. finally
  149. {
  150. if (persentation != null)
  151. {
  152. persentation.Close();
  153. persentation = null;
  154. }
  155. if (application != null)
  156. {
  157. application.Quit();
  158. application = null;
  159. }
  160. GC.Collect();
  161. GC.WaitForPendingFinalizers();
  162. GC.Collect();
  163. GC.WaitForPendingFinalizers();
  164. }
  165. return result;
  166. }
  167.  
  168. #endregion

权限配置第一种方案:

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

单击开始,单击运行, 然后键入 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

C#实现office文档转换为PDF格式的更多相关文章

  1. java使用jacob将office文档转换为PDF格式

    jacob 包下载地址: http://sourceforge.net/projects/jacob-project/ 下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录 ...

  2. 转:C#实现office文档转换为PDF或xps的一些方法

    代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 [url]http://www.microsoft ...

  3. C#实现office文档转换为PDF或xps的一些方法( 转)

    源博客http://blog.csdn.net/kable999/article/details/4786654 代码支持任意office格式 需要安装office 2007 还有一个office20 ...

  4. 在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览

    条件: 安装好禅道的服务器 能直接浏览PDF的浏览器(或通过 安装插件实现 ) 文档转换服务程序(建议部署在另一台服务器上)     实现 原理: 修改禅道的文件预览功能(OFFICE文档其使用的是下 ...

  5. 文档转换为pdf格式帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Word = M ...

  6. office文档转pdf

    这里贴下代码吧,没啥好说的. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  7. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...

  8. OFFICE 文档转换为html在线预览

    OFFICE 文档在线预览方案很多: 服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览,比如flexpaper Office文档直接转换为SWF,通过网页加载Flash预览 微软的 ...

  9. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

随机推荐

  1. hdu 6319 Problem A. Ascending Rating (2018 Multi-University Training Contest 3 A)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=6319 思路: 单调队列倒着维护,队列里面剩下的值的数量就是这一段区间的count值,如样例第一个区间:3 ...

  2. Python爬虫:HTTP协议、Requests库

    HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...

  3. BZOJ 1195: [HNOI2006]最短母串

    1195: [HNOI2006]最短母串 Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 1346  Solved: 450[Submit][Status ...

  4. [2019/03/17#杭师大ACM]赛后总结(被吊锤记)

    前言 和扬子曰大佬和慕容宝宝大佬一组,我压力巨大,而且掌机,累死我了,敲了一个下午的代码,他们两个人因为比我巨就欺负我QwQ. 依旧被二中学军爆锤,我真的好菜,慕容宝宝或者是扬子曰大佬来掌机一定成绩比 ...

  5. Shell基础知识(二)

    对于一个shell脚本来说,第一行是 "#!/bin/bash",这条命令中的 "#!" 告诉系统该用哪一款解释器来对该脚本进行解释,后面的"/bin ...

  6. service的生命周期

    Managing the Lifecycle of a Service service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的s ...

  7. Luogu 1312 【NOIP2011】玛雅游戏 (搜索)

    Luogu 1312 [NOIP2011]玛雅游戏 (搜索) Description Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空 ...

  8. [NOI2009]二叉查找树

    题目大意: 给定一棵严格的treap,父亲节点的优先级必然小于儿子节点的.权值按照二叉树的定义,左儿子小于父亲小于右儿子. 深度从1开始定义,每个点除优先级.数值之外,还有一个访问频度. 访问频度所产 ...

  9. A1079. Total Sales of Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  10. mysql数据库user表host字段的%问题

    搜索: mysql数据库user表host字段的%问题 连接:http://blog.csdn.net/xiaomengh/article/details/48706149 在mysql数据库中,使用 ...