需要安裝office 2007 還有一個office2007的插件OfficeSaveAsPDFandXPS
下載地址
OfficeSaveAsPDFandXPS
[url]http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en[/url]
office 2007 鏈接:
http://pan.baidu.com/s/1eSeQD38
密碼:j5qy
這是一個微軟官方出的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; }
在組件在服務器中添加權限
單擊
開始,單擊
運行, 然后鍵入 DCOMCNFG。選擇要自動運行的應用程序。應用程序名稱如下所示:
Microsoft Access 97 - Microsoft Access 數據庫
Microsoft Access 2000/2002 - Microsoft Access 應用程序
Microsoft Excel 97/2000/2002 - Microsoft Excel 應用程序
Microsoft Word 97 - Microsoft Word Basic
Microsoft Word 2000/2002 - Microsoft Word 文檔
單擊
屬性打開此應用程序的屬性對話框。
Microsoft Access 2000/2002 - Microsoft Access 應用程序
Microsoft Excel 97/2000/2002 - Microsoft Excel 應用程序
Microsoft Word 97 - Microsoft Word Basic
Microsoft Word 2000/2002 - Microsoft Word 文檔
1)標示—運行此應用程序的用戶賬戶—下列用戶;然后輸入Administrator用戶組中的一個用戶。
注:更改服務器中組件服務所用到的用戶密碼時,須在組件中重新輸入新的密碼,才能正常生成Word。
2)安全—啟動和激活權限,選擇“自定義”,添加IIS_WPG用戶的本地啟動、本地激活權限;
3)安全—訪問權限,選擇“自定義”,添加IIS_WPG用戶的本地訪問權限;