如何使用 IronWord 在 C# 中將 DOCX 轉換為 PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronWord 透過其 ToPdf 方法,實現無縫的 DOCX 轉 PDF 轉換,支援需要標準化 PDF 輸出格式的自動化文件工作流程、報告分發及歸檔流程。

快速入門:使用 C# 將 DOCX 轉換為 PDF

使用 IronWord,透過單一方法呼叫即可載入 DOCX 檔案並將其轉換為 PDF。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronWord

    PM > Install-Package IronWord
  2. 請複製並執行此程式碼片段。

    using IronWord;
    
    WordDocument doc = new WordDocument("report.docx");
    doc.ToPdf("report.pdf");
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronWord

    arrow pointer

如何將 DOCX 檔案轉換為 PDF?

ToPdf 方法在保留文件格式、圖片、表格及文字樣式的同時,處理了所有 PDF 生成的複雜性。 載入您的 DOCX 檔案並加入 new WordDocument("file.docx"),呼叫 ToPdf() 並指定輸出路徑,轉換程序將自動完成。

:path=/static-assets/word/content-code-examples/how-to/word-to-pdf-basic.cs
using IronWord;

// Load existing DOCX file
WordDocument doc = new WordDocument("input.docx");

// Convert to PDF
doc.ToPdf("output.pdf");
Imports IronWord

' Load existing DOCX file
Dim doc As New WordDocument("input.docx")

' Convert to PDF
doc.ToPdf("output.pdf")
$vbLabelText   $csharpLabel

提示 ToPdf 方法在轉換過程中能完整保留文件的原始樣式,包括字型、顏色、圖片及版面配置。

此方法非常適用於自動化報告分發、發票生成,或任何需要標準化 PDF 輸出的工作流程。 轉換後的 PDF 在所有平台和裝置上皆能呈現完全一致的樣式,非常適合用於官方文件、法律歸檔,以及對客戶交付的成果物——這些場合中,外觀的一致性至關重要。


如何將多個 DOCX 檔案轉換為 PDF?

針對大量文件處理,批次轉換功能可自動處理整個目錄。 請使用 Directory.GetFiles() 擷取所有 DOCX 檔案,然後遍歷每個檔案並套用相同的轉換方法。 透過 try-catch 區塊進行錯誤處理,可確保損壞的檔案不會導致整個批次作業中斷。

:path=/static-assets/word/content-code-examples/how-to/word-to-pdf-batch.cs
using IronWord;
using System;
using System.IO;

// Get all DOCX files from input directory
string[] docxFiles = Directory.GetFiles("input-folder", "*.docx");

foreach (string filePath in docxFiles)
{
    try
    {
        // Load DOCX file
        WordDocument doc = new WordDocument(filePath);

        // Generate output PDF path
        string fileName = Path.GetFileNameWithoutExtension(filePath);
        string outputPath = $"output-folder/{fileName}.pdf";

        // Convert to PDF
        doc.ToPdf(outputPath);

        Console.WriteLine($"Converted: {fileName}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Failed: {Path.GetFileName(filePath)} - {ex.Message}");
    }
}
Imports IronWord
Imports System
Imports System.IO

' Get all DOCX files from input directory
Dim docxFiles As String() = Directory.GetFiles("input-folder", "*.docx")

For Each filePath As String In docxFiles
    Try
        ' Load DOCX file
        Dim doc As New WordDocument(filePath)

        ' Generate output PDF path
        Dim fileName As String = Path.GetFileNameWithoutExtension(filePath)
        Dim outputPath As String = $"output-folder/{fileName}.pdf"

        ' Convert to PDF
        doc.ToPdf(outputPath)

        Console.WriteLine($"Converted: {fileName}")
    Catch ex As Exception
        Console.WriteLine($"Failed: {Path.GetFileName(filePath)} - {ex.Message}")
    End Try
Next
$vbLabelText   $csharpLabel

上述範例展示了實際的批次處理流程,並透過控制台記錄來追蹤進度。 此模式特別適用於檔案庫遷移專案、自動化工作流程管道,或需定期生成報告等情境,在這些情況下,往往需要將數百個 DOCX 檔案進行轉換,且無需人工介入。

處理大量批次時,請在轉換前確認原始檔案存在,並維持輸出檔名的命名規範一致。 可考慮採用並行處理以提升效能,但需監控系統資源,以防止在並發操作期間發生記憶體耗盡的情況。

常見問題

在 IronWord 中,將 DOCX 轉換為 PDF 的主要方法是什麼?

IronWord 中將 DOCX 轉換為 PDF 的主要方法是 `ToPdf()` 方法。它負責處理整個轉換流程,確保文件的完整性,包括字型、顏色、圖片及版面配置。

如何使用 IronWord 將單一 DOCX 檔案轉換為 PDF?

若要使用 IronWord 將單一 DOCX 檔案轉換為 PDF,請透過 `new WordDocument("file.docx")` 載入 DOCX 檔案,然後傳入所需的輸出路徑來呼叫 `ToPdf()` 方法。

IronWord 能否處理多個 DOCX 檔案批次轉換為 PDF 的作業?

是的,IronWord 支援批次轉換,可透過 `Directory.GetFiles()` 擷取 DOCX 檔案,並逐一迭代處理每個檔案以套用 `ToPdf()` 方法。此功能對於大量文件處理非常實用。

使用 IronWord 將 DOCX 轉換為 PDF 有什麼好處?

使用 IronWord 將 DOCX 轉換為 PDF,可確保跨平台的外觀一致性,使其成為官方文件、法律歸檔及客戶交付成果的理想選擇。

IronWord 在轉換過程中會保留文件格式嗎?

是的,IronWord 能保留文件格式,包括文字樣式、表格和圖片,確保轉換後的 PDF 完全忠實呈現原始文件內容。

是否可以透過 IronWord 以不同方式儲存轉換後的 PDF 檔案?

是的,轉換後可透過 `SaveAs()` 儲存 PDF 檔案,或以位元組陣列形式取得,為文件的儲存或分發方式提供靈活性。

如何確保批次轉換流程能透過 IronWord 有效處理錯誤?

為有效處理批次轉換過程中的錯誤,請使用 try-catch 區塊,以確保損壞的檔案不會中斷整個批次操作。

使用 IronWord 進行批次處理時,應考量哪些系統因素?

使用 IronWord 進行批次處理時,可考慮採用並行處理以提升效能,但需監控系統資源,以防在並行操作期間發生記憶體耗盡的情況。

哪些工作流程能從使用 IronWord 進行 DOCX 轉 PDF 轉換中受益?

透過 IronWord 進行 DOCX 轉 PDF 轉換,能有效提升自動化報告分發、發票生成,以及任何需要標準化 PDF 輸出的工作流程。

我該去哪裡下載適用於 C# 的 IronWord 程式庫?

C# 版的 IronWord 程式庫可從 NuGet 套件管理員下載,網址為 https://nuget.org/packages/IronWord/。

Ahmad Sohail
全端開發者

Ahmad 是一位全端開發者,具備扎實的 C#、Python 及網頁技術基礎。他對建構可擴展的軟體解決方案深感興趣,並樂於探索設計與功能如何在實際應用中完美結合。

在加入 Iron Software 團隊之前,Ahmad 曾參與自動化專案與 API 整合工作,專注於提升效能與開發者體驗。

閒暇之餘,他喜歡嘗試 UI/UX 創意、為開源工具貢獻心力,並偶爾投入技術寫作與文件編寫,致力於將複雜的主題轉化為淺顯易懂的內容。

準備開始了嗎?
Nuget 下載 44,829 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronWord
執行範例 觀看您的資料轉為 WORD 文件。