[轉貼] Word附檔轉成PDF檔案

2012031509:20

出處:http://www.dotblogs.com.tw/shadow/archive/2011/11/29/60081.aspx

Word文件要轉成PDF要Office 2007版本以上才能實現

 

Step 1.在Server上已裝好Office Word 2007後到2007 Microsoft Office 增益集:Microsoft 另存 PDF 或 XPS 檔

下載安裝此外掛包

image

image

image

 

Step 2.安裝完後,對著Web Site專案右鍵>加入參考

image

在COM頁籤中,把「Microsoft Word 12.0 Object Library 」加入參考

image

另外也把.NET頁籤中的「Microsoft.Office.Interop.Word」版本12.0.0.0也加入參考

image

 

Step 3. 接下來開始寫程式,在.aspx設計畫面拉出兩個控制項

 <asp:FileUpload ID="fu_upload" runat="server" />
    <asp:Button ID="btn_execute" Text="請按我" runat="server" OnClick="btn_execute_Click" />

 

Step 4. Code-Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/*要引用以下的命名空間*/
using Microsoft.Office.Interop.Word;
using System.IO;


//程式參考MSDN:Saving Word 2007 Documents to PDF and XPS Formats
//http://msdn.microsoft.com/en-us/library/bb412305(v=office.12).aspx#Y1124
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    ApplicationClass wordApplication = new ApplicationClass();
    Document wordDocument = null;
    object paramSourceDocPath = @"C:\Temp\Test.docx";//Sample Path
    object paramMissing = Type.Missing;

    //Button Click事件
    protected void btn_execute_Click(object sender, EventArgs e)
    {
        if (fu_upload.HasFile)//有選擇檔案的話,就做上傳檔案
        {
            string newFileName = Guid.NewGuid().ToString() + Path.GetExtension(fu_upload.FileName);
            fu_upload.SaveAs(Server.MapPath("~/upload/" + newFileName));//已把Word附檔上傳至Server


            //原始Word附檔在Server上的路徑
            this.paramSourceDocPath = Server.MapPath("~/upload/" + newFileName);
            string paramExportFilePath = @"D:\" + Guid.NewGuid().ToString() + ".pdf";//要匯出的Server路徑
            WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;//要匯出為PDF格式,也可以選擇wdExportFormatXPS要匯出為XPS
            bool paramOpenAfterExport = false;//轉換完後是否要開啟完成檔,這要選false,不然檔案開在Server端幹嘛?
            WdExportOptimizeFor paramExportOptimizeFor =
                WdExportOptimizeFor.wdExportOptimizeForPrint;//wdExportOptimizeForPrint較高品質,wdExportOptimizeForOnScreen,較低品質
            WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;//轉換範圍為全部頁數
            int paramStartPage = 0;
            int paramEndPage = 0;
            WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            WdExportCreateBookmarks paramCreateBookmarks =
                WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;





            try
            {
                // Open the source document.
                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);

                // Export it in the specified format.
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            }
            catch (Exception ex)
            {
                // Respond to the error
            }
            finally
            {
                // Close and release the Document object.
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    wordDocument = null;
                }

                // Quit Word and release the ApplicationClass object.
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    wordApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }


        }
    }


}

此程式碼可以轉*.doc和*.docx,轉換出來的效果:

image

有圖有超連結,效果不差~

懶人研究包

參考資料:Saving Word 2007 Documents to PDF and XPS Formats (MSDN)

其他衍伸閱讀:

How do I convert Word files to PDF programmatically? (這個是老外寫的,非官方版本,應該也可以Run,只是我測試的時候,不知道哪個地方漏掉,所以還是MSDN安定)

論壇討論:c#中把Doc文件或html文件生成pdf文件的类库和方法?

Word 、Excel 2003 2007转换到HTML