网上有很多将doc、ppt、xls等类型的文档转换成pdf的方法,目前了解到的有两大类:

1.使用虚拟打印机将doc、ppt、xls等类型的文档

2.使用OFFICE COM组件

我采用了第二种方法实现,初步测试通过,还没有放到服务器上进行批量实时转换的测试。

下面开始介绍详细步骤:

1.安装OFFICE 2007.我安装的是OFFICE 2007 Professional Plus版。安装后提示要激活,开始没有激活也能使用,只是每次一打开office软件就提示要激活,实在忍受不了,就下了一个激活破解补丁。我用的是Office 2007 最新全系列激活验证破解补丁(适用于2007任何版本)绿色免费版激活的。

2.安装"另存为PDF或XPS加载项",可以从官网下载,其他地方也有一大把下载链接。我是从这个地址下载的

3.新建项目,添加如下引用:

Microsoft PowerPoint 12.0 Object Library

Microsoft Word 12.0 Object Library

Microsoft Excel 12.0 Object Library

这三个引用在“添加引用”对话框的COM选项卡里,只有安装了OFFICE 2007后才能看到,系统了安装的是OFFICE 2003的话,看到的是11.0的。

4.添加以上COM引用后,在项目的引用目录下,会看到自动添加了“Microsoft.Office.Interop.Word”、“Microsoft.Office.Interop.Excel”、“Microsoft.Office.Interop.PowerPoint”、“Microsoft.Office.Core”四个引用项,分别右击前三个,选属性,选择”嵌入互操作类型“值为false。如不做此项操作,编译项目时会出现”无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口“的错误提示。

5.在代码中添加如下命名空间引用:

1 using Microsoft.Office.Core;
2 using Microsoft.Office.Interop.Excel;
3 using Microsoft.Office.Interop.PowerPoint;
4 using Word = Microsoft.Office.Interop.Word;
5 using Excel = Microsoft.Office.Interop.Excel;
6 using PowerPoint = Microsoft.Office.Interop.PowerPoint;

开始我以为不用第1-3行,结果发现没有这三行编译通不过。第4-6行的作用仅仅是为了在后面代码中简写命名空间。

6.添加如下三个转换函数:

  1  //将word文档转换成PDF格式
2 private bool Convert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
3 {
4 bool result;
5 object paramMissing = Type.Missing;
6 Word.ApplicationClass wordApplication = new Word.ApplicationClass();
7 Word._Document wordDocument = null;
8 try
9 {
10 object paramSourceDocPath = sourcePath;
11 string paramExportFilePath = targetPath;
12
13 Word.WdExportFormat paramExportFormat = exportFormat;
14 bool paramOpenAfterExport = false;
15 Word.WdExportOptimizeFor paramExportOptimizeFor =
16 Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
17 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
18 int paramStartPage = 0;
19 int paramEndPage = 0;
20 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
21 bool paramIncludeDocProps = true;
22 bool paramKeepIRM = true;
23 Word.WdExportCreateBookmarks paramCreateBookmarks =
24 Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
25 bool paramDocStructureTags = true;
26 bool paramBitmapMissingFonts = true;
27 bool paramUseISO19005_1 = false;
28
29 wordDocument = wordApplication.Documents.Open(
30 ref paramSourceDocPath, ref paramMissing, ref paramMissing,
31 ref paramMissing, ref paramMissing, ref paramMissing,
32 ref paramMissing, ref paramMissing, ref paramMissing,
33 ref paramMissing, ref paramMissing, ref paramMissing,
34 ref paramMissing, ref paramMissing, ref paramMissing,
35 ref paramMissing);
36
37 if (wordDocument != null)
38 wordDocument.ExportAsFixedFormat(paramExportFilePath,
39 paramExportFormat, paramOpenAfterExport,
40 paramExportOptimizeFor, paramExportRange, paramStartPage,
41 paramEndPage, paramExportItem, paramIncludeDocProps,
42 paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
43 paramBitmapMissingFonts, paramUseISO19005_1,
44 ref paramMissing);
45 result = true;
46 }
47 finally
48 {
49 if (wordDocument != null)
50 {
51 wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
52 wordDocument = null;
53 }
54 if (wordApplication != null)
55 {
56 wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
57 wordApplication = null;
58 }
59 GC.Collect();
60 GC.WaitForPendingFinalizers();
61 GC.Collect();
62 GC.WaitForPendingFinalizers();
63 }
64 return result;
65 }
66
67 //将excel文档转换成PDF格式
68 private bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType)
69 {
70 bool result;
71 object missing = Type.Missing;
72 Excel.ApplicationClass application = null;
73 Workbook workBook = null;
74 try
75 {
76 application = new Excel.ApplicationClass();
77 object target = targetPath;
78 object type = targetType;
79 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
80 missing, missing, missing, missing, missing, missing, missing, missing, missing);
81
82 workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
83 result = true;
84 }
85 catch
86 {
87 result = false;
88 }
89 finally
90 {
91 if (workBook != null)
92 {
93 workBook.Close(true, missing, missing);
94 workBook = null;
95 }
96 if (application != null)
97 {
98 application.Quit();
99 application = null;
100 }
101 GC.Collect();
102 GC.WaitForPendingFinalizers();
103 GC.Collect();
104 GC.WaitForPendingFinalizers();
105 }
106 return result;
107 }
108
109 //将ppt文档转换成PDF格式
110 private bool Convert(string sourcePath, string targetPath, PpSaveAsFileType targetFileType)
111 {
112 bool result;
113 object missing = Type.Missing;
114 PowerPoint.ApplicationClass application = null;
115 Presentation persentation = null;
116 try
117 {
118 application = new PowerPoint.ApplicationClass();
119 persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
120 persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
121
122 result = true;
123 }
124 catch
125 {
126 result = false;
127 }
128 finally
129 {
130 if (persentation != null)
131 {
132 persentation.Close();
133 persentation = null;
134 }
135 if (application != null)
136 {
137 application.Quit();
138 application = null;
139 }
140 GC.Collect();
141 GC.WaitForPendingFinalizers();
142 GC.Collect();
143 GC.WaitForPendingFinalizers();
144 }
145 return result;
146 }

7.调用相应函数进行转换:

Convert("C:\\1.doc", "C:\\1.pdf", wd);

开发工具是Visual Studio 2010,Windows XP SP3操作系统,OFFICE 2007.调试了doc,ppt,xls三种格式文件的转换,word测试了一个12页的文档,全部是中文文字。Excel测试了一个有3个sheet的文档(中英文和数字,某些行有背景色),转换后3个表格全部转到一个pdf文件中,无错误。转换速度很快,只是在转换PPT的时候出现了一个“正在发布...”的对话框,完成后对话框消失。不知道如何禁止出现提示框,优质稻的朋友看到贴后麻烦回复告知。

为方便使用,将文档转换的代码做成了一个dll,在项目中直接调用dll中的函数即可进行转换。欢迎点击下载

C#实现文档转换成PDF的更多相关文章

  1. word ppt excel文档转换成pdf

    1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...

  2. ASP.NET将word文档转换成pdf的代码

    一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...

  3. asp.net将ppt文档转换成pdf

    一.添加引用 using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint; 二.转换方法   C# 代码   复制 // ...

  4. Python将word文档转换成PDF文件

    如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...

  5. Java利用aspose-words将word文档转换成pdf(破解 无水印)

    首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...

  6. Java实现批量将word文档转换成PDF

    先导入words的jar包 需要jar包的私聊我发你 代码如下:import com.aspose.words.Document;import java.io.File; public class W ...

  7. C# word文档转换成PDF格式文档

    最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...

  8. C#调用WPS将文档转换成pdf进行预览

    引用:https://www.jianshu.com/p/445996126c75 vs启动项目可以生成wps实例 本地iis部署的站点却不行 原因是vs是管理员权限,而iis没有权限 解决方法 启动 ...

  9. java将office文档pdf文档转换成swf文件在线预览

    第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...

随机推荐

  1. 大虾翻译(一):jQuery.extend()

    本文是在JavaScript之三里面链接内容的中文翻译.我会尽可能做到信达雅且保持作者原意不变,OK,let's Go! jQuery.extend(target,[object1],[objectN ...

  2. 致网友Wonderfei的一封信(怎样选择自己主动化框架的几点拙见)

    注:本来这封信要发给Wonerfei网友的,可是由于每次仅仅能发200字,所以干脆贴到博客上,叫Wonderfei同学到这上面来看,也算是我自己的一个暂时总结吧.同一时候也希望大家给予Wonderfe ...

  3. 基于NSIS脚本开发的安装程序制作软件:易量安装

    原文 基于NSIS脚本开发的安装程序制作软件:易量安装 前几天“萝卜”给我推荐了一款安装程序制作工具——易量安装. 易量安装是一款安装程序制作软件,基于著名的NSIS(Nullsoft Scripta ...

  4. WinHEC(Windows硬件project产业创新峰会)将2015回归

    WinHEC这是Windows Hardware Engineering Cumminity,中国呼吁Windows硬件project产业创新峰会.将2015在早期的回报,2015年3月18日至19日 ...

  5. Cocos2d-x游戏开发Lua

    1.加入参考库 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2lzZG9tNjA1NzY4Mjky/font/5a6L5L2T/fontsize/400 ...

  6. Dynamics CRM2013/2015 禁止欢迎屏幕(Disable the Welcome Screen)

    首先打开Dynamic CRM  2013将有一个欢迎界面的例子,下面的图,它不会为了图检查框出现.OK然后,下一次打开就没有. 可是当我们打开F12开发者工具,清除域的缓存后再次打开CRM,这个欢迎 ...

  7. 转让lua性能executeGlobalFunction

    没有其他的,搞搞cocos2dx的lua文字,话lua这件事情在几年前学过一段时间.还曾对自己c++介面,我已经做了一些小东西.只是时间的流逝,模糊记忆. 拿起点功夫和成本.下面是我的一些经验. co ...

  8. e.target 和 e.srcElement 的使用问题

    ie 下的event.srcElement从字面上可以看出来有以下关键字:事件.源(它的意思就是:当前事件的源), 我们可以调用他的各种属性就像:document.getElementById(&qu ...

  9. linux后台server开发环境的部署配置和验证(nginx+apache+php-fpm+FASTCGI(C/C++))

    linux后台server开发环境部署配置 引言 背景 随着互联网业务的不断增多.开发环境变得越来越复杂,为了便于统一server端的开发部署环境,特制定本配置文档. 使用软件 CentOS 6.3( ...

  10. crawler_URL编码原理详解

    经常写爬虫的童鞋,难免要处理含有中文的url,大部分时间,都知道url_encode,各个语言也都有支持,今天简单整理下原理,供大家科普 1.特征: 如果URL中含有非ASCII字符的话, 浏览器会对 ...