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

如何使用 C# 將 Microsoft Word 文件轉換為 PDF

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

DOCX 文件是使用 Microsoft Word 建立的文檔,Microsoft Word 是 Microsoft Office 套件中的文字處理程序,由 Microsoft 提供。 它採用 Office Open XML (OOXML) 標準,因此高效且與各種軟體相容。 自 Word 2007 起,它一直是 Word 文件的預設格式,取代了最初發佈時的舊版 DOC 格式。

IronPDF 能夠立即將 Word 文件轉換為 PDF 文件,並提供郵件合併功能,可為各個收件者產生個人化的文件批次。 將 DOCX 檔案轉換為 PDF 檔案可確保通用相容性,保留格式,並增加一層安全性。

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

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

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 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 框架應用程式中。

將 Microsoft Word 檔案轉換為 PDF 格式的過程很簡單。 我們首先實例化 DocxToPdfRenderer 類別。 使用 DocxToPdfRenderer 物件的 RenderDocxAsPdf 方法,並提供 DOCX 檔案的檔案路徑。此方法傳回 PdfDocument 對象,可讓您進一步自訂 PDF。 您可以下載現代時間順序履歷表 DOCX 範例檔案

Microsoft Word 預覽

Microsoft Word 預覽

程式碼範例

此外, 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");
Imports IronPdf

' Instantiate Renderer
Private renderer As New DocxToPdfRenderer()

' Render from DOCX file
Private pdf As PdfDocument = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx")

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

輸出 PDF


郵件合併範例

郵件合併功能位於 Microsoft 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; }
}
Friend Class RecipientsDataModel
	Public Property [Date]() As String
	Public Property Location() As String
	Public Property Recipients_Name() As String
	Public Property Contact_Us() As String
End Class
$vbLabelText   $csharpLabel

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

Microsoft Word 預覽

Microsoft Word 預覽

程式碼範例

: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");
Imports IronPdf
Imports System.Collections.Generic
Imports System.Linq

Private recipients = New List(Of RecipientsDataModel)() From {
	New RecipientsDataModel() With {
		.Date ="Saturday, October 15th, 2023",
		.Location="Iron Software Cafe, Chiang Mai",
		.Recipients_Name="Olivia Smith",
		.Contact_Us = "support@ironsoftware.com"
	},
	New RecipientsDataModel() With {
		.Date ="Saturday, October 15th, 2023",
		.Location="Iron Software Cafe, Chiang Mai",
		.Recipients_Name="Ethan Davis",
		.Contact_Us = "support@ironsoftware.com"
	}
}

Private docxToPdfRenderer As New DocxToPdfRenderer()

' Apply render options
Private options As New DocxPdfRenderOptions()

' Configure the output PDF to be combined into a single PDF document
options.MailMergePrintAllInOnePdfDocument = True

' Convert DOTX to PDF
Dim pdfs = docxToPdfRenderer.RenderDocxMailMergeAsPdf(Of RecipientsDataModel)(recipients, "Party-invitation.dotx", options)

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

輸出 PDF

PDF 文件建立完成後,您可以靈活地進行其他變更。 這些功能包括將其匯出為PDF/APDF/UA ,以及新增數位憑證。 您也可以透過合併或分割 PDF 、旋轉 PDF 來操作單一頁面,並且可以選擇套用註解書籤

除了這些功能外,IronPDF 還支援轉換為其他文件格式。 有關 IronPDF 提供的其他功能的更多信息,請參閱本教程

常見問題解答

如何在 C# 中将 DOCX 文件转换为 PDF?

要在 C# 中将 DOCX 文件转换为 PDF,可以使用 IronPDF 库中的 `DocxToPdfRenderer` 类。只需实例化该类并调用 `RenderDocxAsPdf` 方法,传入您的 DOCX 文件以生成 PDF。

为什么要将 DOCX 文件转换为 PDF 格式?

将 DOCX 文件转换为 PDF 格式可确保您的文档维护其在不同平台和设备上的格式,增强安全性,并提供一种通用的兼容格式。

我可以使用 IronPDF 对 DOCX 文件执行邮件合并吗?

是的,IronPDF 支持邮件合并功能,允许您通过将数据合并到 DOCX 模板中来创建个性化文档,然后将其转换为 PDF。

使用 IronPDF 进行 DOCX 到 PDF 转换的优点是什么?

IronPDF 提供了一种简化的 DOCX 转为 PDF 的流程,支持高级功能如邮件合并,并允许进一步的 PDF 自定义,例如添加数字证书和注释。

是否可以使用 IronPDF 将流中的 DOCX 数据转换为 PDF?

是的,IronPDF 的 `RenderDocxAsPdf` 方法可以处理来自流或字节数组的 DOCX 数据,使其适用于不同的输入源。

在从 DOCX 转换后,我可以进一步自定义 PDF 吗?

使用 IronPDF 将 DOCX 转换为 PDF 后,您可以通过导出为 PDF/A 或 PDF/UA 标准来自定义 PDF,添加注释、书签或数字签名。

在哪里可以找到用于测试转换为 PDF 的示例 DOCX 文件?

您可以从 IronPDF 网站上提供的链接中下载示例 DOCX 文件,例如“现代时间顺序简历”,以练习转换为 PDF。

如何下载 IronPDF 进行 C# 中的 DOCX 到 PDF 转换?

IronPDF 可以从 NuGet 包管理器下载,允许轻松集成到您的 .NET 项目中,以进行 DOCX 到 PDF 转换。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
審核人
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 下載 16,493,056 | Version: 2025.11 剛發表