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

How to Convert Microsoft Word to PDF in C

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

IronPDF使 C# 開發人員能夠使用 DocxToPdfRenderer 類,透過簡單的一行方法呼叫將 DOCX 檔案轉換為 PDF 格式,保留所有格式並支援大量文件產生的郵件合併功能。

DOCX 文件是用 Microsoft Word 建立的文檔,Microsoft Word 是 Microsoft Office 套件中的文字處理程序,由 Microsoft 提供。 它採用 Office Open XML (OOXML) 標準,因此高效且與各種軟體相容。 自 Word 2007 起,它一直是 Word 文件的預設格式,取代了最初發佈時的舊版 DOC 格式。 DOCX 格式具有多項優勢,包括透過 ZIP 壓縮減小檔案大小、更好的資料復原能力以及在不同平台上的更高相容性。

IronPDF能夠立即將 Word 文件轉換為 PDF 文件,並提供郵件合併功能,可為各個收件者產生個人化的文件批次。 將 DOCX 檔案轉換為 PDF 檔案可確保通用相容性,保留格式,並增加一層安全性。 當您需要分發在不同裝置和作業系統上保持完全相同外觀的文件時,此功能尤其有用,類似於從 HTML 建立 PDF將影像轉換為 PDF

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

使用IronPDF,在 C# 中輕鬆將 DOCX 檔案轉換為 PDF。 本快速指南示範如何使用 DocxToPdfRenderer 類別和 RenderDocxAsPdf 方法,只需一行程式碼即可實現無縫轉換。 對於希望簡化文件處理的開發人員來說,這非常理想,可以確保生成的 PDF 保留所有原始格式和相容性。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 複製並運行這段程式碼。

    new IronPdf.DocxToPdfRenderer()
        .RenderDocxAsPdf("document.docx")
        .SaveAs("output.pdf");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPDF

    arrow pointer


如何將 DOCX 檔案轉換為 PDF?

IronPDF允許開發人員以程式設計方式將 Word 文件轉換為 PDF,從而提高效率,並允許他們將其整合到現有的.NET應用程式或他們可能擁有的任何跨平台.NET Framework應用程式中。 該程式庫支援多種安裝方法,包括NuGet套件,並且可以部署在包括WindowsLinuxmacOS在內的多個平台上。

將 Microsoft Word 檔案轉換為 PDF 格式的過程很簡單。 我們首先實例化 DocxToPdfRenderer 類別。 透過提供 DOCX 檔案的檔案路徑,使用 RenderDocxAsPdf 物件的 DocxToPdfRenderer 方法。此方法傳回一個 PdfDocument 對象,可讓您進一步自訂 PDF,包括新增頁首和頁尾、套用浮水印或設定安全選項。 您可以下載現代時間順序履歷表 DOCX 範例檔案

原始文件長什麼樣?

微軟 Word 文件中展示了一份現代藥劑師履歷,採用時間順序排列,並以紅色點綴。

我該如何寫C#程式碼?

此外,RenderDocxAsPdf 方法也接受以位元組和流形式儲存的 DOCX 資料。 這種靈活性使您能夠處理來自各種來源的文檔,無論這些文檔是儲存在磁碟上、記憶體中,還是從資料庫中檢索。 此方法在內部處理所有複雜的轉換邏輯,確保字體、圖像、表格和格式準確保留。

:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-from-file.cs
using IronPdf;

// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
using IronPdf;

// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
using IronPdf;

// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
$vbLabelText   $csharpLabel

對於更高級的場景,您還可以從位元組數組或串流轉換:

// Convert from byte array
byte[] docxBytes = File.ReadAllBytes("document.docx");
PdfDocument pdfFromBytes = renderer.RenderDocxAsPdf(docxBytes);

// Convert from stream
using (FileStream stream = new FileStream("document.docx", FileMode.Open))
{
    PdfDocument pdfFromStream = renderer.RenderDocxAsPdf(stream);
}
// Convert from byte array
byte[] docxBytes = File.ReadAllBytes("document.docx");
PdfDocument pdfFromBytes = renderer.RenderDocxAsPdf(docxBytes);

// Convert from stream
using (FileStream stream = new FileStream("document.docx", FileMode.Open))
{
    PdfDocument pdfFromStream = renderer.RenderDocxAsPdf(stream);
}
$vbLabelText   $csharpLabel

轉換後的PDF會是什麼樣子?

生成的 PDF 文件保留了所有原始格式,包括字體、顏色、佈局和嵌入的圖像。 這使得IronPDF成為產生需要以通用可存取格式共享或存檔的專業文件的理想選擇。


如何使用郵件合併功能批次產生PDF文件?

郵件合併功能位於 Microsoft Word 的"郵件"標籤中,可讓您建立一批文檔,其中包含針對每個收件者或資料條目的個人化資訊。 它通常用於產生個人化的信件、信封、標籤或電子郵件,例如邀請函、新聞簡報或格式信函,其中大部分內容相同,但某些細節會因收件人而異。 當與 IronPDF 的合併多個 PDF或產生具有自訂元資料的文件的功能結合使用時,此功能尤其強大。

我應該創建什麼樣的資料模型?

首先,我們建立一個模型,將要透過郵件合併的資訊儲存到對應的佔位符中。 資料模型中的屬性名稱必須與 Word 範本中的合併欄位名稱完全相符(區分大小寫)。

:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-mail-merge-model.cs
internal class RecipientsDataModel
{
    public string Date { get; set; }
    public string Location{ get; set; }
    public string Recipients_Name { get; set; }
    public string Contact_Us { get; set; }
}
internal class RecipientsDataModel
{
    public string Date { get; set; }
    public string Location{ get; set; }
    public string Recipients_Name { get; set; }
    public string Contact_Us { get; set; }
}
internal class RecipientsDataModel
{
    public string Date { get; set; }
    public string Location{ get; set; }
    public string Recipients_Name { get; set; }
    public string Contact_Us { get; set; }
}
$vbLabelText   $csharpLabel

我根據我們的需求修改了微軟 Word 提供的範本。 請下載Party Invitation DOTX 範例檔。 對於我們的用例,讓我們將 MailMergePrintAllInOnePdfDocument 屬性設為 true,這將把 PDF 合併到一個 PdfDocument 物件中。 我們將使用的合併欄位有:日期、地點、收件人姓名和聯絡我們。

郵件合併範本長什麼樣子?

帶有郵件合併工具的 Word 郵件合併派對邀請函模板

如何實作郵件合併代碼?

IronPDF中的郵件合併功能支援各種渲染選項,這些選項可以透過 DocxPdfRenderOptions 類別進行設定。 這樣,您可以控制頁面方向、頁邊距等方面,以及是否將所有合併的文件合併到一個 PDF 文件中或產生單獨的文件。

:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-mail-merge.cs
using IronPdf;
using System.Collections.Generic;
using System.Linq;

var recipients = new List<RecipientsDataModel>()
    {
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Olivia Smith",
            Contact_Us = "support@ironsoftware.com"
        },
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Ethan Davis",
            Contact_Us = "support@ironsoftware.com"
        },
    };

DocxToPdfRenderer docxToPdfRenderer = new DocxToPdfRenderer();

// Apply render options
DocxPdfRenderOptions options = new DocxPdfRenderOptions();

// Configure the output PDF to be combined into a single PDF document
options.MailMergePrintAllInOnePdfDocument = true;

// Convert DOTX to PDF
var pdfs = docxToPdfRenderer.RenderDocxMailMergeAsPdf<RecipientsDataModel>(
     recipients,
     "Party-invitation.dotx",
     options);

pdfs.First().SaveAs("mailMerge.pdf");
using IronPdf;
using System.Collections.Generic;
using System.Linq;

var recipients = new List<RecipientsDataModel>()
    {
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Olivia Smith",
            Contact_Us = "support@ironsoftware.com"
        },
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Ethan Davis",
            Contact_Us = "support@ironsoftware.com"
        },
    };

DocxToPdfRenderer docxToPdfRenderer = new DocxToPdfRenderer();

// Apply render options
DocxPdfRenderOptions options = new DocxPdfRenderOptions();

// Configure the output PDF to be combined into a single PDF document
options.MailMergePrintAllInOnePdfDocument = true;

// Convert DOTX to PDF
var pdfs = docxToPdfRenderer.RenderDocxMailMergeAsPdf<RecipientsDataModel>(
     recipients,
     "Party-invitation.dotx",
     options);

pdfs.First().SaveAs("mailMerge.pdf");
using IronPdf;
using System.Collections.Generic;
using System.Linq;

var recipients = new List<RecipientsDataModel>()
    {
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Olivia Smith",
            Contact_Us = "support@ironsoftware.com"
        },
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Ethan Davis",
            Contact_Us = "support@ironsoftware.com"
        },
    };

DocxToPdfRenderer docxToPdfRenderer = new DocxToPdfRenderer();

// Apply render options
DocxPdfRenderOptions options = new DocxPdfRenderOptions();

// Configure the output PDF to be combined into a single PDF document
options.MailMergePrintAllInOnePdfDocument = true;

// Convert DOTX to PDF
var pdfs = docxToPdfRenderer.RenderDocxMailMergeAsPdf<RecipientsDataModel>(
     recipients,
     "Party-invitation.dotx",
     options);

pdfs.First().SaveAs("mailMerge.pdf");
$vbLabelText   $csharpLabel

合併後的最終PDF檔案是什麼樣子的?

PDF 文件建立完成後,您可以靈活地進行其他變更。 這些功能包括將其匯出為PDF/APDF/UA 格式,以符合存檔和無障礙存取要求,以及新增數位憑證以進行文件認證。 您也可以透過合併或分割 PDF旋轉頁面來操作單一頁面,並且可以選擇套用註解書籤,以便更好地瀏覽文件。

除了這些功能外, IronPDF還支援轉換為其他文件格式。 例如,您可以將 Markdown 轉換為 PDF將 RTF 轉換為 PDF ,甚至可以將 PDF 匯出回 HTML 。 有關IronPDF提供的其他功能的更多信息,請參閱全面的HTML 轉 PDF 教程

常見問題解答

如何在 C# 中将 DOCX 文件轉换為 PDF?

使用 IronPDF,您只需使用 DocxToPdfRenderer 類一行代碼即可將 DOCX 轉換為 PDF。只需實體化 DocxToPdfRenderer,使用 DOCX 檔案路徑呼叫 RenderDocxAsPdf 方法,並使用 SaveAs 方法儲存輸出即可。

什麼是 DocxToPdfRenderer 類別?

DocxToPdfRenderer 是 IronPDF 將 Microsoft Word 文件轉換為 PDF 格式的專門類。它提供了一個簡單的 API,讓開發人員可以在轉換 DOCX 檔案的同時,保留所有原始格式、版面設計和文件屬性。

Word 轉換為 PDF 是否保留格式?

是的,IronPDF 的 DocxToPdfRenderer 保留了原始 Word 文件的所有格式,包括字体、样式、图像、表格和布局,确保 PDF 輸出与原始 DOCX 文件完全匹配。

將 DOCX 轉換為 PDF 時,可以執行 Mail Merge 嗎?

是的,IronPDF 支援 Mail Merge 功能,可讓您從單一 DOCX 模板產生個人化批次的 PDF 文件。這對於為個別收件人建立自訂文件,同時保持格式一致特別有用。

將 DOCX 轉換為 PDF 的主要步驟是什麼?

使用 IronPDF 的過程包含 5 個簡單步驟:1) 下載 C# 函式庫;2) 準備您的 DOCX 檔案;3) Instantiate the DocxToPdfRenderer class;4) 使用 RenderDocxAsPdf 方法與您的檔案路徑;5) 可選擇使用 Mail Merge 進行批次處理。

為什麼要將 Word 文件轉換成 PDF 格式?

使用 IronPDF 轉換為 PDF 可確保在所有設備和平台上的通用兼容性,保留精確的格式和佈局,增加文件安全功能,並防止不必要的編輯。無論檢視軟體或作業系統為何,PDF 都能維持其外觀。

DOCX 至 PDF 的轉換是否與不同平台相容?

是的,IronPDF 支援跨平台部署,可在 Windows、Linux 和 macOS 上使用。該函式庫可與各種 .NET Framework 應用程式搭配使用,並提供多種安裝方法,包括 NuGet 套件。

可以轉換哪些類型的 Word 文件?

IronPDF 可以轉換使用 Office Open XML (OOXML) 標準的 Microsoft Word 2007 或更新版本所建立的 DOCX 檔案。這包括具有複雜格式、圖片、表格和其他進階 Word 功能的文件。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

審核人
Jeff Fritz
Jeffrey T. Fritz
首席程序经理 - .NET 社群团队
Jeff 也是 .NET 和 Visual Studio 团队的首席程序经理。他是 .NET Conf 虚拟会议系列的执行制作人,并主持“Fritz 和朋友”这一每周两次的開發者的直播节目,在节目上讨论技術并与观众一起编写代碼。Jeff 撰写研讨会、主持演讲,并计划大型 Microsoft 開發者活動(包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP Summit)的內容。
準備好開始了嗎?
Nuget 下載 17,803,474 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速取得證據? PM > Install-Package IronPdf
運行範例看著你的HTML程式碼變成PDF檔。