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

下载地址
[url]http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en[/url]
这是一个微软官方出的office插件。
office2010里好像能直接将文件另存为.PDF格式的
安装好之后,打开VS,以VS2005为例
新建windows应用程序项目
添加以下com组件的引用
Microsoft Word 12.0 Object Library
Microsoft PowerPoint 12.0 Object Library
Microsoft Excel 12.0 Object Library
------------------------------------------------------
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
我们可以使用一个枚举类型来决定生成文件的类型
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;

//将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;}

C#实现Office文档转Pdf的方法相关推荐

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

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

  2. java零碎要点010---Java实现office文档与pdf文档的在线预览功能

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

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

    C#实现office文档转换为PDF格式 1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFand ...

  4. java 预览word文档_Java实现office文档与pdf文档的在线预览功能

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

  5. python将文档转换成pdf_Python实现将DOC文档转换为PDF的方法

    本文实例讲述了Python实现将DOC文档转换为PDF的方法.分享给大家供大家参考.具体实现方法如下: import sys, os from win32com.client import Dispa ...

  6. Android实现在线预览office文档(Word,Pdf,excel,PPT.txt等格式)

    1.概述 我们都知道,Android原生并没有提供浏览office文档格式的相关Api,在安卓端想要实现在线预览office文档的功能显然很是复杂,我们手机安装QQ浏览器时,在手机打开office文档 ...

  7. 永中word页码怎么从第二页开始_快捷的word文档转pdf好方法推荐

    阅读本文前,请您先点击上面的"蓝色字体",再点击"关注",这样您就可以继续免费收到文章了.每天都会有分享,都是免费订阅,请您放心关注.注图文来源网络,侵删 wo ...

  8. linux平台的office文档转pdf的实例(程序员的菜)

    需要材料: 1. Openoffice3.4(我是32位的centos,可以根据自己的系统下载指定的openoffice软件包) 下载地址:http://sourceforge.net/project ...

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

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

最新文章

  1. #418 Div2 Problem B An express train to reveries (构造 || 全排列序列特性)
  2. 实时音视频技术的演进与应用
  3. 快手搜索技术部招聘NLP算法工程师!
  4. 一次完整的http请求
  5. JDK9.0.4安装配置
  6. 八、Python 之内置函数(。。。)
  7. 《构建之法》8、9、10
  8. 【Java笔记】Java开发实战经典 - 第六章
  9. PDF Explained(翻译)第一章 简介
  10. 小米Miui线刷步骤记录
  11. java pdf转png格式_如何在Java中将PDF转换为PNG或JPG
  12. 使用echarts生成海友网企业全国分布地图
  13. AR增强现实技术解读
  14. 腾讯3轮面试都问了Android事件分发,最强技术实现
  15. 使用OBS录屏有很大的电流回声
  16. 一个使用LABVIEW将16进制字符串分割转化的办法
  17. python excel画图
  18. android CMWAP, CMNET有何区别
  19. SQL1 插入记录(一)
  20. 使用虚拟信用卡在Allegro下单测试教程

热门文章

  1. tensorflow 测试 cuda 是否安装成功,测试代码环境
  2. AcWing算法基础课 Level-2 第三讲 搜索与图论
  3. SpringBoot笔记:SpringBoot启动参数配置
  4. Commit failed with error: pathspec 'src/main/java/com/leo/demo/juctest/test.java' did not match any
  5. 读取文件夹下所有的文件
  6. 你有没有遇到要实现多种登录方式的场景丫 一起来看看咯 Spring Security 实现多种登录方式,如常规方式外的邮件、手机验证码登录
  7. ecshop 搜索热词推荐_拼多多搜索推广实操——如何选择正确的关键词实现高投产!...
  8. 手机连接不上电脑只显示充电_INNOCN N1G便携显示器,解决孩子和大人多重使用需求...
  9. 项目收获与体会_践行“十个一”在劳动中收获成长——青岛六十六中高二年级学农实践活动圆满结束...
  10. java动效_Android 界面漩涡扭曲动效实现