标签:style blog http color io os 使用 java ar
网上有很多将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