C#實現office文檔轉換為PDF格式

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   使用   for   strong   sp   

需要安裝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 LibraryMicrosoft PowerPoint 12.0 Object LibraryMicrosoft 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 文檔單擊 屬性開啟此應用程式的屬性對話方塊。 

1)標示—運行此應用程式的使用者賬戶—下列使用者;然後輸入Administrator使用者組中的一個使用者。

註:更改伺服器中元件服務所用到的使用者密碼時,須在組件中重新輸入新的密碼,才能正常產生Word。

2)安全—啟動和啟用許可權,選擇“自訂”,添加IIS_WPG使用者的本地啟動、本地啟用許可權;

3)安全—存取權限,選擇“自訂”,添加IIS_WPG使用者的本地存取權限;


C#實現office文檔轉換為PDF格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.