1. 程式人生 > >C#實現Office文件轉Pdf的方法

C#實現Office文件轉Pdf的方法

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;
    }


相關推薦

C#實現OfficePdf方法

Excel.XlFixedFormatType excelType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF; PowerPoint.PpSaveAsFileType ppType = Microsoft.Office.Inter

C#實現office轉換為PDF或xps的一些方法

程式碼支援任意office格式 需要安裝office 2007 還有一個office2007的外掛OfficeSaveAsPDFandXPS 下載地址 [url]http://www.microsoft.com/downloads/details.aspx?FamilyId=

C#實現office轉換為PDF格式

需要安裝office 2007 還有一個office2007的外掛OfficeSaveAsPDFandXPS 下載地址 這是一個微軟官方出的office外掛。 office2010裡好像能直接將檔案另存為.PDF格式的 安裝好之後,開啟VS,以VS2005為例 新

java實現wordpdf

前端時間,專案需要自動生成word文件,用WordFreeMarker生成word文件後,又要求生成的文件能在瀏覽器瀏覽,思來想去,把word文件轉成pdf就好了,於是乎研究了一下。 將word文件轉化為PDF是專案中常見的需求之一,目前主流的方法可以分為兩大類,一類是利用各種Office應

java實現wordpdf線上瀏覽

現在著手做的專案是一個關於考試的,裡面有許多考生的資料資訊是word文件,在這裡耗費了不少的時間,也看了眾多的博文,在這裡發一下我的個人總結,有不對的地方還望指正 用到的jar jacob.jar 這是Maven地址http://mvnreposito

前端實現office閱覽的方法(整理)

六、第三方成熟的服務 如OfficeWeb365 優點: 1、OfficeWeb365採用適合中文排版的純Html、CSS技術。 2、介面簡單,適合PHP、JSP、ASP.net等所有的對接,省心省力。 3、費用低廉,節省投入。 4、不用關心客戶端是否安裝了Office軟體,不用在客戶端部署。 5、手機線

Android實現線上預覽office(Word,Pdf,excel,PPT.txt等格式)

1.概述 我們都知道,Android原生並沒有提供瀏覽office文件格式的相關Api,在安卓端想要實現線上預覽office文件的功能顯然很是複雜,我們手機安裝QQ瀏覽器時,在手機開啟office文件時會提示如圖, 這就是這篇文章的主角–騰訊X5核心(T

懶人福音|10秒搞定多PDF

PDF PDF編輯 PDF轉換 自從Office自帶轉換PDF功能之後,為使用者帶來不少便利。原先到處找PDF轉換器和編輯器的鬧心經歷似乎已成為歷史。但是office自帶的PDF轉換功能只能一個文件一個文件地轉,有時候望著滿屏等待轉換的文件,是不是有種心要溺斃的感覺? 這個窘迫的局面如何解決呢?不

只需一行程式碼,python實現docxhtml頁面!

說到word文件轉html的,網上一搜一大把,各種線上word轉html頁面,使用起來也方便。但是在實際專案中要使用的話,需要自己開發,這裡就提供一個簡單的方法。 學習Python中有不明白推薦加入交流群            

基於openoffice的 office化為

依賴jar 原始碼 /** * */ package com.b510.office2pdf; import java.io.File; import java.util.Date; import java.util.regex.Pattern; import org.a

PDF技術(一)-Java實現Office系列檔案PDF檔案

最近,公司要求做個檔案轉pdf的調研報告,於是在網上找了一些實現方法,現在將這些方法做個對比,並記錄下來,以後或許有用呢,哈哈。 首先說一下需求,產品要求不能使用第三方軟體實現,因為這種實現方式效率不高,所以需要使用“純Java程式碼”實現。同時也對跨平臺有要求,系統需要執行在linux系統

使用微軟提供的Office Online實現Office的線上檢視,編輯等功能

使用微軟提供的Office Online平臺只需要一個網址即可線上檢視Xls,doc,PPT等文件 詳情:http://www.cnblogs.com/huangtailang/p/76492af9d30087d8659d8d5400d20fc7.html pd

docxpdf檔案

注:這裡只能轉換docx檔案,doc不行,原始碼在文末。 可以在windows,linux上執行,增加了內容替換功能,因為有些文件內容需要我們用程式碼來動態生成。 下面是具體操作步驟: maven依賴 <!-- docx轉pdf -->

Java程式實現Word轉為pdf以及出現的問題解決

做獸醫專案需要用到這種需求,很多程式設計師都遇到過,有些word文件希望直接在瀏覽器中開啟進行預覽,但是瀏覽器往往不是很配合,直接就提示下載,不像pdf文件,瀏覽器可以直接進行預覽。1. Word文件轉為pdf直接上程式碼:import java.io.File; im

基於MVC4+EasyUI的Web開發框架經驗總結(8)--實現Office的預覽

在部落格園很多文章裡面,曾經有一些介紹Office文件預覽檢視操作的,有些通過轉為PDF進行檢視,有些通過把它轉換為Flash進行檢視,但是過程都是曲線救國,真正能夠簡潔方便的實現Office文件的預覽的還是比較少,這裡的Office文件包括了Word、Excel、PPT文件。本文介紹兩種方式,一種方式是通過

jodconverter wordPDF

1.建立maven專案 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/

Qt實現wordhtml

最近在做編輯器,要讀取Word文件,但是QT的QTextEdit沒辦法直接讀取,網上查了好多資料也沒有找到解決辦法,只好自己動手,現在發出來,給大家提供個方便.(doc和docx格式均可以)主要原理就是呼叫Word的SaveAs方法,要用到QAxWidget呼叫Word提供的

[置頂] java處理officepdf檔案(一)

需求說明: 使用者可以上傳word,excel,ppt,pdf檔案。在頁面能夠預覽該檔案,並可以通過搜尋,找到對應的檔案記錄。 使用技術: 通過jacob將office轉化為html,使用poi進行檔案提取; 在前期調研的時候,搜尋其他將office轉化為html的技術,發現其轉化之後的頁面都不怎麼逼真。但

掃描型PDF可編輯復制Word方法

shu image ans tro view sof 效果圖 文件 基礎 無論是畢業季需要參考文章寫論文,還是工作需要參考其他的電子材料。 很多時候,這類文件大多數是掃描版PDF文檔,即通過掃描儀將文本掃描為電子版PDF,我們需要是將文檔的文字可以復制或者編輯的,但使用PD

通過COM組方式實現java調用C#寫的DLL

time rar .exe 字符串 sys 還需 第一個 運行 system 最近一段時間單位在做一個Web項目,工程師用JAVA語言,需要公用人員信息,統一用戶名和密碼,原有的平臺中是用C#語言開發的,在網上查找解決方法,通過JAVA調用C#的DLL文件實現。網上