20171228(WordToPdf_byDotNet)

基于Office的实现步骤

主要是利用了 office com 组件中的 Microsoft.Office.Interop.word.dll 动态链接库文件可以通过 c# 代码实现对 word 文件实现另存为 pdf,xml或者其他格式文件一起对word 文件的修改等等。

相关 API 文档可以参考 microsoft 官方 msdn

一、如何 引入 Microsoft.Office.Interop.word.dll 文件

首先本机安装的是 office 2016 版本,找了好久都没有找到 这个动态链接库文件,这一步挺耗费时间的。

具体查找步骤:

  1. 我使用的是 Everything 软件搜索 Microsoft.Office.Interop.word.dll 才找到这个文件
  2. 找到后拷贝到 自己的project 下或者直接引用到项目中

二、 转换代码实现如下

using Word = Microsoft.Office.Interop.Wordstring WordFilePath = @"D:\attatch\product\demo.doc";
object paramMissing = Type.Missing;
Word.Application application = new Word Application();
application.Visible = false;
Word.Document document = null;
wordDocument = wordApplication.Documents.Open(WordFilePath, ref paramMissing, true,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);document.ExportAsFixedFormat(@"D:\attatch\product\demo.pdf",Word.WdExportFormat.wdExportFormatPDF);
// 关闭进程
application.Quit(ref paramMissing, ref paramMissing, ref paramMissing); 

补充

  1. 基于 .net framework 下 word 转换 除了使用office com组件还可以使用 spire.doc,这是收费软件,前三页转换免费的,或者使用 EVOpdf,EVOpdf 中也有word to pdf 插件,不过这是一个收费的应用。

  2. Everything以及 word.dll 文件下载链接

  3. 基于 office com 组件,我们可以实现很多的 关于word,excel的操作

  4. 原理: 我认为后台还是调用了 office 或者 wps 程序,应是 headless 程序,无界面程序。

  5. 其他blog copy的代码


public bool WordToPDF(string sourcePath, string targetPath){bool result = false;Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();Document document = null;try{application.Visible = false;document = application.Documents.Open(sourcePath);document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);result = true;}catch (Exception e){Console.WriteLine(e.Message);result = false;}finally{document.Close();}return result;}//将word文档转换成PDF格式private bool Convert(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;}//将excel文档转换成PDF格式private bool Convert(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;}//将ppt文档转换成PDF格式private bool Convert(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;}

dotNet基于office实现word转pdf相关推荐

  1. 【技术】基于 SpringBoot实现 Word 和 PDF 识别

    基于 SpringBoot实现 Word 和 PDF 识别 基于 SpringBoot 实现 Word 和 PDF 识别 识别 Word 识别 PDF 基于 SpringBoot 实现 Word 和 ...

  2. .Net 依赖Office将Word转Pdf的实现

    开发环境:VS2017..Net Core2.1 需求问题:在使用 Aspose.Words 将Word转为pdf时发现,如果是带有图片的word文档,转换Pdf时,会出现错误,转换失败 一.准备工作 ...

  3. C# word 转 pdf

    将Word转换为带目录书签的PDF,待转换Word中应该有目录或书签,可以用Word中的标题来自动生成目录 office.interop.word 转pdf public bool WordToPDF ...

  4. 基于java的格式转换,word 转 pdf、word 转图片、office 格式转换、在线文件预览

    一.项目简介 不管你是java程序员.c++程序员,python程序员,在开发项目中肯定遇到过格式转换的问题,如何轻松搞定格式转换的问题呢?当然是百度啦!面向百度编程已经成为当下程序员的日常操作. 基 ...

  5. C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

    有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...

  6. Office系列---将Office文件(Word、PPT、Excel)转换为PDF文件,提取Office文件(Word、PPT)中的所有图片

    将Office文件转换为PDF文件,提取Office文件中的所有图片 1.Office系列---将Office文件(Word.PPT.Excel)转换为PDF文件 1.1 基于Office实现的解决方 ...

  7. 基于freemarker ,xdocreport生成word,pdf

    解决freemarkder 处理 docx 后生成的是xml问题 xdocreport docx 转pdf title: 基于freemarker ,xdocreport生成word,pdf cate ...

  8. php word excel转pdf文件怎么打开,php office文件(word/excel/ppt)转pdf文件,pptpdf

    php office文件(word/excel/ppt)转pdf文件,pptpdf 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, 关于预览office文件实现核心就是,把o ...

  9. C# web实现word 转Html、office转Html、pdf转图片 在线预览文件

    改篇 pdf 预览再本机没问题,发布再iis中 不行 ,(使用剪贴板的问题..excel和word 可以,) pdf解决:请看我的博文 ----最终解决篇 详细配置及代码 word 转Html 1 / ...

  10. Web方式预览Office/Word/Excel/pdf文件解决方案

    Web方式预览Office/Word/Excel/pdf文件解决方案 参考文章: (1)Web方式预览Office/Word/Excel/pdf文件解决方案 (2)https://www.cnblog ...

最新文章

  1. shell的各种运行模式?
  2. 一文看懂集成学习(详解 bagging、boosting 以及他们的4点区别)
  3. 机器学习模型建立的几点建议
  4. linux显示进程的h开头的,Linux上进程的开始时间
  5. IT兄弟连 Java语法教程 变量1
  6. C++线程同步之临界区
  7. EOS 智能合约源代码解读 (10)token合约“几种关键操作”
  8. Eclipse在过去十年中的主要成就
  9. 卸载cuda_NVIDIA驱动和CUDA安装
  10. Java LinkedList公共布尔提供(对象o)方法(带示例)
  11. mvc ajax提交html标签,Mvc提交表单的四种方法全程详解
  12. 关于错误:不能将licenses.licx文件转换成二进制,error lc0003 !
  13. python如何使用sdk_如何通过Python访问Kvaser CANlib 软件开发包|Kvaser CANlib SDK的应用...
  14. AutoCAD Civil 3D 中缓和曲线的定义
  15. Untiy Shader - Metallic vs Specular Workflow 金属 vs 高光的工作流
  16. 郁亮的“权力游戏”,万科的“内外战争”
  17. 记录阿里云虚拟主机FTP连接不上的解决办法
  18. 全套BAT大厂面试题整理锦集
  19. 图像处理 - 平滑处理(cvSmooth)
  20. 2021网安保研---武汉大学网络安全学院

热门文章

  1. 学术壁报模板_电子壁报 | 中华医学会第十四次全国妇产科学学术会议
  2. 读《人月神话》的感想(一)——关于组织结构沟通能力优劣的量化
  3. linux监控进程挂死,linux系统下实时监控进程以及定位杀死挂起的进程
  4. 免费获得卡巴斯基key的方法
  5. 软件测试用例(全面)
  6. 最新支持android的手机型号,Andorid10支持手机型号有哪些 安卓10适配机型介绍
  7. 51单片机入门——安装keil5及烧录下载器
  8. 学习了Python大数据之后,将来主要做什么
  9. c语言fread参数,C语言fread函数
  10. GsonFormat的使用