使用 IRONWORD 如何在 C# 中將 Word 轉換為 PDF Jordi Bardia 更新:8月 31, 2025 下載 IronWord NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 在許多應用程式中,以程式設計方式轉換文件已成為一項必不可少的功能。 尤其在商業領域,將 Word 文件轉換為 PDF 文件是一項常規任務。 幸運的是,借助 C# 和 Microsoft Interop,您可以無縫地將 Word 文件轉換為 PDF。 本教程將討論如何使用 C# 將 Word 轉換為 PDF 的程式化過程。 先決條件 在深入研究使用 C# 將 .DOCX 轉換為 PDF 的程式碼之前,請確保您已設定必要的環境至關重要。 以下是您需要具備的先決條件: Microsoft Word 安裝 請確保您的電腦上已安裝 Microsoft Word。 Interop 服務將使用 Word 的內建功能來處理 Word 文件和 PDF 的轉換。 Visual Studio 若要建立、編譯和執行 C# 程序,需要安裝Visual Studio 。 如果您還沒有 Visual Studio,可以從微軟官方網站下載免費的社群版。 安裝Microsoft.Office.Interop.Word套件 該軟體包對於為您的 C# 程式提供與 Word 文件互動所需的功能至關重要。 稍後會使用 NuGet 套件管理器進行安裝,但了解它在轉換過程中的重要性是有益的。 用於轉換的 Word 文檔 準備一個要轉換的 Word 文件或.docx檔案。 請確保您知道它在您的電腦上的路徑,因為您需要在 C# 程式中指定它。 足夠的權限 請確保您能夠讀取 Word 文件並將生成的 PDF 文件寫入目標目錄。 以管理員身分執行 Visual Studio 有時可以解決權限相關的問題。 具備這些前提條件後,您就可以設定環境並將 Word 文件轉換為 PDF 文件。 設定環境 開啟 Visual Studio。 建立一個新的 C# 控制台應用程式。 前往NuGet 套件管理器>管理解決方案的 NuGet 套件。 搜尋" Microsoft.Office.Interop.Word "並安裝它。 該軟體包將允許我們的應用程式與 Word 進行通訊並轉換 Word 文件。 ![如何在 C# 中將 Word 轉換為 PDF:圖 1 - 開啟 Visual Studio 並建立新的控制台應用程式專案。 前往 NuGet 套件管理器以取得解決方案。 搜尋"Microsoft.Office.Interop.Word"並安裝。 這個軟體包將幫助我們的控制台應用程式與 Word 進行交互,並轉換 Word 文件。 將 Word 文件轉換為 PDF 的範例程式碼 若要使用 C# 將 Word 文件轉換為 PDF,我們將利用 Microsoft Interop 服務的功能。 下面的程式碼片段實現了這個功能,以下將進行詳細解釋。 using System; using Word = Microsoft.Office.Interop.Word; class Program { static void Main() { // Create an instance of Microsoft Word application var wordApp = new Word.Application(); // Open the Word document var wordDocument = wordApp.Documents.Open(@"path_to_your_word_file.docx"); // Specify the path where the PDF should be saved var outputPath = @"path_where_you_want_to_save_pdf.pdf"; // Convert the Word document to PDF wordDocument.ExportAsFixedFormat(outputPath, Word.WdExportFormat.wdExportFormatPDF); // Close the Word document and quit the Word application wordDocument.Close(); wordApp.Quit(); // Output a success message Console.WriteLine("Word document converted to PDF successfully!"); } } using System; using Word = Microsoft.Office.Interop.Word; class Program { static void Main() { // Create an instance of Microsoft Word application var wordApp = new Word.Application(); // Open the Word document var wordDocument = wordApp.Documents.Open(@"path_to_your_word_file.docx"); // Specify the path where the PDF should be saved var outputPath = @"path_where_you_want_to_save_pdf.pdf"; // Convert the Word document to PDF wordDocument.ExportAsFixedFormat(outputPath, Word.WdExportFormat.wdExportFormatPDF); // Close the Word document and quit the Word application wordDocument.Close(); wordApp.Quit(); // Output a success message Console.WriteLine("Word document converted to PDF successfully!"); } } Imports System Imports Word = Microsoft.Office.Interop.Word Friend Class Program Shared Sub Main() ' Create an instance of Microsoft Word application Dim wordApp = New Word.Application() ' Open the Word document Dim wordDocument = wordApp.Documents.Open("path_to_your_word_file.docx") ' Specify the path where the PDF should be saved Dim outputPath = "path_where_you_want_to_save_pdf.pdf" ' Convert the Word document to PDF wordDocument.ExportAsFixedFormat(outputPath, Word.WdExportFormat.wdExportFormatPDF) ' Close the Word document and quit the Word application wordDocument.Close() wordApp.Quit() ' Output a success message Console.WriteLine("Word document converted to PDF successfully!") End Sub End Class $vbLabelText $csharpLabel 程式碼解釋 我們的程式碼開頭包含一個關鍵的命名空間別名Word以便輕鬆引用Microsoft.Office.Interop.Word命名空間。 System命名空間提供了 C# 程式設計的基本類,幾乎是所有 C# 應用程式的必備元件。 真正的操作是從Main方法內部開始的。 我們首先使用new Word.Application()來建立 Word 應用程式的新實例。 這步驟類似於啟動 MS Word,但所有操作都在背景進行,使用者看不到。 應用程式實例初始化後,我們指示它使用wordApp.Documents.Open方法開啟 Word 文件。 請務必將"path_to_your_word_file.docx"替換為 Word 文件的路徑。 現在,文件已打開,我們來確定要將 PDF 文件保存到哪裡。 這是在outputPath變數中指定的。 需要注意的是,路徑應該調整為您希望轉換後的輸出 PDF 檔案所在的位置。 將 Word 文件轉換為 PDF 的神奇之處在於這行程式碼wordDocument.ExportAsFixedFormat(...) 。 Interop 服務提供了內建方法,可輕鬆實現轉換。 此方法接受兩個主要參數:PDF 的儲存路徑和匯出格式,在本例中為 PDF。 轉換完成後,關閉已使用的資源是一種良好的實踐。因此, wordDocument.Close()確保我們開啟的文件已關閉,而wordApp.Quit()確保我們在背景啟動的 Word 應用程式實例已終止。 最後,我們的程式會透過簡單的控制台訊息將結果傳達給使用者。 Console.WriteLine()方法提供回饋,表示轉換過程已成功執行。 因此, Microsoft.Office.Interop.Word為處理和轉換 Word 文件提供了一個合適的解決方案。 使用 IronPDF 和 IronWord 將 Word 轉換為 PDF(C# 程式碼) 如果你正在建立一個涉及文件自動化的 .NET 應用程序,你將面臨的一個常見任務是將 Word 文件轉換為 PDF 格式。 無論您是處理 .docx 文件範本、發票、報告還是合同,將 DOCX 文件轉換為 PDF 都能確保格式一致、安全交付和跨平台相容性。 本文介紹如何使用 IronSoftware 的兩款強大工具IronPDF和IronWord輕鬆地將Word 文件轉換為 PDF 文件。 您將學習它們之間的差異、何時使用每種工具以及如何在 C# 中實現它們,從而快速、可靠地生成 Word 到 PDF 文件。 為什麼要在 .NET 中將 Word 轉換為 PDF? 在深入探討如何使用這些程式庫來實現此任務之前,讓我們先來看看為什麼將 Word 文件轉換為 PDF 可以提升您的工作空間和文件品質。 那麼,為什麼要將 DOCX 檔案轉換為 PDF 格式呢? 它確保了各個平台上的視覺一致性 鎖定格式並防止意外編輯 它非常適合用於存檔法律或財務文件。 它使列印和共享文件變得更加容易 這通常是許多業務工作流程所需的格式。 工具概述:IronPDF 和 IronWord 鐵言 IronWord 是一個專為讀取和編輯 Word 文件而建立的 .NET 程式庫。 使用 IronWord,您可以直接載入 .docx 文件並根據需要進行編輯,無需安裝 Microsoft Office Interop 或 Microsoft Word,然後使用 IronPDF 處理 DOCX 到 PDF 的轉換。 現在讓我們透過建立一個新的文件物件並在其中加入文字來看 IronWord 的實際應用: using IronWord; using IronWord.Models; using IronWord.Models.Abstract; WordDocument doc = new WordDocument(); TextContent text = new TextContent("This is some example text."); text.Style = new TextStyle() { Color = Color.Red, TextFont = new Font() { FontFamily = "Roboto", FontSize = 72, } }; doc.AddText(text); doc.Save("example.docx"); using IronWord; using IronWord.Models; using IronWord.Models.Abstract; WordDocument doc = new WordDocument(); TextContent text = new TextContent("This is some example text."); text.Style = new TextStyle() { Color = Color.Red, TextFont = new Font() { FontFamily = "Roboto", FontSize = 72, } }; doc.AddText(text); doc.Save("example.docx"); Imports IronWord Imports IronWord.Models Imports IronWord.Models.Abstract Private doc As New WordDocument() Private text As New TextContent("This is some example text.") text.Style = New TextStyle() With { .Color = Color.Red, .TextFont = New Font() With { .FontFamily = "Roboto", .FontSize = 72 } } doc.AddText(text) doc.Save("example.docx") $vbLabelText $csharpLabel 輸出 Word 文件 ! 文字輸出 IronPDF。 IronPDF 是一個功能齊全的 .NET PDF 庫,包含豐富的 PDF 生成和操作工具。 雖然其最突出的特點之一是高品質的HTML 轉 PDF功能,但它也能處理 Word 轉 PDF 的 C# 轉換任務。 對於希望產生 PDF 或編輯現有 PDF 文件的 .NET 開發人員來說,IronPDF 可以滿足您的所有需求。 那麼 IronPDF 到底有多容易使用呢? 讓我們來看看下面的程式碼範例,了解一下將 Word 文件轉換為 PDF 時是如何運作的: using IronPdf; var renderer = new DocxToPdfRenderer(); var pdf = renderer.RenderDocxAsPdf("sample.docx"); pdf.SaveAs("example.pdf"); using IronPdf; var renderer = new DocxToPdfRenderer(); var pdf = renderer.RenderDocxAsPdf("sample.docx"); pdf.SaveAs("example.pdf"); Imports IronPdf Private renderer = New DocxToPdfRenderer() Private pdf = renderer.RenderDocxAsPdf("sample.docx") pdf.SaveAs("example.pdf") $vbLabelText $csharpLabel PDF輸出 ! PDF 輸出 整合工作流程:IronWord 和 IronPDF 雖然 IronWord 不能直接匯出為 PDF,但它非常適合以程式設計方式準備 Word 文件。 以下是一個常見的流程: C# 中的 Word 轉 PDF 管道 使用 IronWord 載入或建立Word 文檔 編輯或填寫動態資料(例如替換令牌) 儲存 .docx 文件 使用 IronPDF 將文件轉換為 PDF 格式 之後,您可以使用 IronPDF 對新渲染的 PDF 進行任何進一步的調整,例如新增浮水印或註釋。 這種混合工作流程以最小的依賴性提供最大的靈活性,非常適合 Web 應用程式、雲端服務和桌面應用程式。 那麼實際效果如何呢? 使用 IronWord 建立文檔 首先,我們將使用 IronWord 建立一個範例 Word 文件。 using IronWord; using IronWord.Models; using IronWord.Models.Abstract; WordDocument doc = new WordDocument(); TextContent title = new TextContent("Example Report."); TextContent text = new TextContent("Report created using IronWord, you can use this to create dynamic text and tables."); title.Style = new TextStyle() { TextFont = new Font() { FontSize = 72 }, IsBold = true, }; Paragraph paragraph = new Paragraph(); paragraph.AddText(title); paragraph.AddText(text); doc.AddParagraph(paragraph); doc.Save("example.docx"); using IronWord; using IronWord.Models; using IronWord.Models.Abstract; WordDocument doc = new WordDocument(); TextContent title = new TextContent("Example Report."); TextContent text = new TextContent("Report created using IronWord, you can use this to create dynamic text and tables."); title.Style = new TextStyle() { TextFont = new Font() { FontSize = 72 }, IsBold = true, }; Paragraph paragraph = new Paragraph(); paragraph.AddText(title); paragraph.AddText(text); doc.AddParagraph(paragraph); doc.Save("example.docx"); Imports IronWord Imports IronWord.Models Imports IronWord.Models.Abstract Private doc As New WordDocument() Private title As New TextContent("Example Report.") Private text As New TextContent("Report created using IronWord, you can use this to create dynamic text and tables.") title.Style = New TextStyle() With { .TextFont = New Font() With {.FontSize = 72}, .IsBold = True } Dim paragraph As New Paragraph() paragraph.AddText(title) paragraph.AddText(text) doc.AddParagraph(paragraph) doc.Save("example.docx") $vbLabelText $csharpLabel Output ! Word文件輸出 使用 IronPDF 進行 Word 到 PDF 的 C# 轉換 只需 3 行簡單的程式碼,我們就可以輕鬆地將Word 文件轉換為 PDF 。 using IronPdf; var renderer = new DocxToPdfRenderer(); var pdf = renderer.RenderDocxAsPdf("example.docx"); pdf.SaveAs("output.pdf"); using IronPdf; var renderer = new DocxToPdfRenderer(); var pdf = renderer.RenderDocxAsPdf("example.docx"); pdf.SaveAs("output.pdf"); Imports IronPdf Private renderer = New DocxToPdfRenderer() Private pdf = renderer.RenderDocxAsPdf("example.docx") pdf.SaveAs("output.pdf") $vbLabelText $csharpLabel Output ! PDF 輸出 就這樣,我們用 IronWord 建立了一個自訂 Word 文檔,然後使用 IronPDF 將其轉換為 PDF 格式,只需幾行程式碼即可完成。 即時部署:適用於任何地方 IronPDF 和 IronWord 皆可在下列環境中運作: ASP.NET Core 和 Blazor Azure 應用程式服務與 Azure 函數 相容於 .NET Framework、.NET 6、7、8、9 和 .NET Core 可輕鬆整合到 Linux、Docker 和伺服器環境中 桌面(WPF/WinForms)和控制台應用程式 無需安裝Office軟體。 不支援 COM 互通。只有簡潔、現代化的 .NET 函式庫。 常見用例 Use Case IronWord IronPDF 載入/編輯 .docx 文件 是的 No 在 Word 中取代文字/標記 是的 No 儲存 .docx 文檔 是的 No 將 HTML 轉換為 PDF 不 Yes 產生最終 PDF 輸出 不 Yes 伺服器端渲染 是的 是的 專業提示:使用 Razor 視圖輸出類似 Word 的 PDF 文檔 即使您不使用 IronWord,IronPDF 也直接支援Razor範本。 您可以使用 Razor Pages 透過 CSS 輸出 Word 樣式的佈局,然後將其渲染為 PDF: @model InvoiceModel <html> <body> <h1>Invoice for @Model.CustomerName</h1> <p>Amount Due: @Model.TotalAmount</p> </body> </html> @model InvoiceModel <html> <body> <h1>Invoice for @Model.CustomerName</h1> <p>Amount Due: @Model.TotalAmount</p> </body> </html> 'INSTANT VB TODO TASK: The following line uses invalid syntax: '@model InvoiceModel <html> <body> <h1> Invoice for @Model.CustomerName</h1> <p> Amount Due: @Model.TotalAmount</p> </body> </html> $vbLabelText $csharpLabel IronPDF 可以將其渲染成精美的、可直接用於印刷的 PDF 文件。 摘要:在 .NET 中充分發揮 Word 轉 PDF 的強大功能 透過將IronPDF和IronWord整合到您的 .NET 專案中,您可以獲得一個強大的、無需 Office 的 .NET 文件處理工作流程。 建立具有自訂樣式和表格的動態 Word 文檔,或將文檔轉換為像素級完美的 PDF 文件。 無論什麼任務,有了這些程式庫,您始終都能輕鬆使用強大的工具來處理 Word 和 PDF 任務。 這種組合非常適合產生發票、報告、合約以及任何以 Word 格式開始但需要以安全、一致的 PDF 格式結束的文件。 無論您是建置桌面軟體還是部署到 Azure,IronWord 和 IronPDF 都能讓您完全掌控文件自動化—快速、可靠且可用於生產環境。 如果您想了解這些庫的更多實際應用,請務必查看它們的文檔,您可以在其中找到IronPDF和IronWord的大量程式碼範例,以了解有關其功能和工作原理的更多資訊。 立即開始 透過 NuGet 套件管理器安裝這兩個程式庫,即可快速在 Visual Studio 專案中實現它們。 想先試用再購買嗎? 立即下載IronPDF和IronWord的免費試用版,即可開始建立功能強大的文件。 常見問題解答 如何使用 C# 將 Word 文件轉換為 PDF? 您可以使用 Microsoft.Office.Interop.Word 套件以 C# 將 Word 文件轉換為 PDF。這包括建立 Word 應用程式實例、載入文件,並使用 ExportAsFixedFormat 方法將其儲存為 PDF。 ExportAsFixedFormat 方法在 Word 到 PDF 的轉換中扮演什麼角色? Microsoft.Office.Interop.Word 中的 ExportAsFixedFormat 方法用於將 Word 文件匯出為 PDF 檔案,提供適合分享和列印的固定格式。 用 C# 將 Word 文件轉換為 PDF 的先決條件是什麼? 若要以 C# 將 Word 文件轉換為 PDF,您需要安裝 Microsoft Word、Visual Studio,以及透過 NuGet Package Manager 取得 Microsoft.Office.Interop.Word 套件。 為什麼轉換完成後必須關閉 Word 應用程式? 轉換完成後關閉 Word 應用程式對於釋放系統資源和避免記憶體洩漏非常重要。它可確保應用程式不會在背景中持續開啟,消耗不必要的資源。 在處理 Excel 檔案方面,有什麼比 Microsoft Interop 更有效率的替代方案? IronXL.Excel 是用 C# 語言處理 Excel 檔案的更有效替代方案。它不需要安裝 Microsoft Office,並提供更快的效能與廣泛的支援檔案格式。 如何安裝 IronXL 函式庫,以便在 C# 應用程式中使用? 若要在您的 C# 應用程式中安裝 IronXL,請使用 NuGet 套件管理員控制台並執行指令 Install-Package IronXL.Excel。 IronXL 在處理 Excel 檔案時,可以處理哪些檔案格式? IronXL.Excel 可以轉換和處理多種格式的 Excel 檔案,包括 XLS、XLSX、XLSM、CSV、TSV、JSON、XML、HTML,以及二進位和位元組陣列。 與 Microsoft Interop 服務相比,使用 IronXL 有哪些優勢? 與 Microsoft Interop 服務相比,IronXL 的優勢包括無須安裝 Microsoft Office、處理速度更快、易於使用,以及支援更多的檔案格式。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新9月 18, 2025 ASP .NET Core 匯入和匯出 Word 檔案 本指南探討如何匯入現有的 Word 文件、顯示其內容,以及使用 IronWord 函式庫從頭建立文件 閱讀更多 更新7月 28, 2025 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多 更新6月 22, 2025 如何使用 C# 在 Word 中對齊文字 讓我們深入瞭解 IronWord NuGet 套件,以及如何使用此套件對齊文字或段落 閱讀更多 C# 編輯 Word (程式碼範例開發人員教程)C# 開啟 Word 文件
更新7月 28, 2025 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多