C#实现文档转换成PDF
網(wǎng)上有很多將doc、ppt、xls等類(lèi)型的文檔轉(zhuǎn)換成pdf的方法,目前了解到的有兩大類(lèi):
1.使用虛擬打印機(jī)將doc、ppt、xls等類(lèi)型的文檔
2.使用OFFICE COM組件
我采用了第二種方法實(shí)現(xiàn),初步測(cè)試通過(guò),還沒(méi)有放到服務(wù)器上進(jìn)行批量實(shí)時(shí)轉(zhuǎn)換的測(cè)試。
下面開(kāi)始介紹詳細(xì)步驟:
1.安裝OFFICE 2007.我安裝的是OFFICE 2007 Professional Plus版。安裝后提示要激活,開(kāi)始沒(méi)有激活也能使用,只是每次一打開(kāi)office軟件就提示要激活,實(shí)在忍受不了,就下了一個(gè)激活破解補(bǔ)丁。我用的是Office 2007 最新全系列激活驗(yàn)證破解補(bǔ)丁(適用于2007任何版本)綠色免費(fèi)版激活的。
2.安裝"另存為PDF或XPS加載項(xiàng)",可以從官網(wǎng)下載,其他地方也有一大把下載鏈接。我是從這個(gè)地址下載的
3.新建項(xiàng)目,添加如下引用:
Microsoft PowerPoint 12.0 Object Library
Microsoft Word 12.0 Object Library
Microsoft Excel 12.0 Object Library
這三個(gè)引用在“添加引用”對(duì)話框的COM選項(xiàng)卡里,只有安裝了OFFICE 2007后才能看到,系統(tǒng)了安裝的是OFFICE 2003的話,看到的是11.0的。
4.添加以上COM引用后,在項(xiàng)目的引用目錄下,會(huì)看到自動(dòng)添加了“Microsoft.Office.Interop.Word”、“Microsoft.Office.Interop.Excel”、“Microsoft.Office.Interop.PowerPoint”、“Microsoft.Office.Core”四個(gè)引用項(xiàng),分別右擊前三個(gè),選屬性,選擇”嵌入互操作類(lèi)型“值為false。如不做此項(xiàng)操作,編譯項(xiàng)目時(shí)會(huì)出現(xiàn)”無(wú)法嵌入互操作類(lèi)型“Microsoft.Office.Interop.Excel.ApplicationClass”。請(qǐng)改用適用的接口“的錯(cuò)誤提示。
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;
開(kāi)始我以為不用第1-3行,結(jié)果發(fā)現(xiàn)沒(méi)有這三行編譯通不過(guò)。第4-6行的作用僅僅是為了在后面代碼中簡(jiǎn)寫(xiě)命名空間。
6.添加如下三個(gè)轉(zhuǎn)換函數(shù):
View Code
1 //將word文檔轉(zhuǎn)換成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文檔轉(zhuǎn)換成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文檔轉(zhuǎn)換成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.調(diào)用相應(yīng)函數(shù)進(jìn)行轉(zhuǎn)換:
Convert("C:\\1.doc", "C:\\1.pdf", wd);
開(kāi)發(fā)工具是Visual Studio 2010,Windows XP SP3操作系統(tǒng),OFFICE 2007.調(diào)試了doc,ppt,xls三種格式文件的轉(zhuǎn)換,word測(cè)試了一個(gè)12頁(yè)的文檔,全部是中文文字。Excel測(cè)試了一個(gè)有3個(gè)sheet的文檔(中英文和數(shù)字,某些行有背景色),轉(zhuǎn)換后3個(gè)表格全部轉(zhuǎn)到一個(gè)pdf文件中,無(wú)錯(cuò)誤。轉(zhuǎn)換速度很快,只是在轉(zhuǎn)換PPT的時(shí)候出現(xiàn)了一個(gè)“正在發(fā)布...”的對(duì)話框,完成后對(duì)話框消失。不知道如何禁止出現(xiàn)提示框,優(yōu)質(zhì)稻的朋友看到貼后麻煩回復(fù)告知。
為方便使用,將文檔轉(zhuǎn)換的代碼做成了一個(gè)dll,在項(xiàng)目中直接調(diào)用dll中的函數(shù)即可進(jìn)行轉(zhuǎn)換。歡迎點(diǎn)擊下載。
總結(jié)
以上是生活随笔為你收集整理的C#实现文档转换成PDF的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IIS,apche,nginx,301域
- 下一篇: 丢了好几年的 Auto CAD又拿起来