How to Convert Microsoft Word to PDF in C
IronPDF 讓 C# 開發人員能夠透過 DocxToPdfRenderer 類別,僅需一行簡單的呼叫方法,即可將 DOCX 檔案轉換為 PDF 格式,同時保留所有格式設定,並支援"郵件合併"功能以進行批次文件生成。
DOCX 檔案是由 Microsoft WORD 所建立的文件,Microsoft WORD 是微軟提供的 Microsoft Office Suite 中的文字處理程式。 它採用 Office Open XML (OOXML) 標準,使其運作高效且與各類軟體相容。 這是自 WORD 2007 以來 WORD 文件的預設格式,在最初發布時便取代了舊有的 DOC 格式。 DOCX 格式具備多項優勢,包括透過 ZIP 壓縮實現更小的檔案大小、更佳的資料復原能力,以及跨不同平台的相容性提升。
IronPDF 具備將 WORD 文件即時轉換為 PDF 檔案的能力,並提供"郵件合併"功能,可為個別收件者生成個人化的批次文件。 將 DOCX 轉換為 PDF 可確保廣泛的相容性、保留格式,並增添一層安全性。 此功能在您需要分發文件時特別有用,這些文件必須在不同裝置和作業系統上保持完全一致的外觀,類似於從 HTML 建立 PDF 或將圖片轉換為 PDF。
快速入門:使用 IronPDF 將 DOCX 轉換為 PDF
using IronPDF,在 C# 中輕鬆將 DOCX 檔案轉換為 PDF。 本快速指南將示範如何使用 DocxToPdfRenderer 類別與 RenderDocxAsPdf 方法,僅需一行程式碼即可實現無縫轉換。 非常適合希望簡化文件處理流程的開發人員,確保生成的 PDF 能完整保留原始格式與相容性。
簡化工作流程(5 個步驟)
- 下載用於將 DOCX 轉換為 PDF 的 C# 函式庫
- 請準備您要轉換的 DOCX 檔案
- 實例化 DocxToPdfRenderer 類別,以將 DOCX 檔案渲染為 PDF
- 請使用
clear方法並提供 DOCX 檔案路徑 - 利用"郵件合併"功能來生成一批文件
如何將 DOCX 檔案轉換為 PDF?
IronPDF 讓開發人員能夠透過程式化方式將 WORD 文件轉換為 PDF,不僅能提升工作效率,還能讓開發人員將其整合至現有的 .NET 應用程式,或任何他們可能擁有的跨平台 .NET Framework 應用程式中。 此函式庫支援多種安裝方式,包括 NuGet 套件,並可部署於 Windows、Linux 和 macOS 等多個平台。
將 WORD 檔案轉換為 PDF 格式的流程相當簡單。 我們首先實例化 DocxToPdfRenderer 類別。 透過提供 DOCX 檔案的路徑,呼叫 DocxToPdfRenderer 物件的 RenderDocxAsPdf 方法。此方法會傳回一個 PdfDocument 物件,讓您能進一步自訂 PDF,包括新增頁首與頁尾、套用浮水印,或設定安全性選項。 您可以下載"現代時間順序履歷"DOCX範例檔案。
原始文件的外觀為何?
如何撰寫 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");
Imports IronPdf
' Instantiate Renderer
Dim renderer As New DocxToPdfRenderer()
' Render from DOCX file
Dim pdf As PdfDocument = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx")
' Save the PDF
pdf.SaveAs("pdfFromDocx.pdf")
針對更進階的應用情境,您亦可從位元組陣列或資料流進行轉換:
// 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);
}
Imports System.IO
' Convert from byte array
Dim docxBytes As Byte() = File.ReadAllBytes("document.docx")
Dim pdfFromBytes As PdfDocument = renderer.RenderDocxAsPdf(docxBytes)
' Convert from stream
Using stream As New FileStream("document.docx", FileMode.Open)
Dim pdfFromStream As PdfDocument = renderer.RenderDocxAsPdf(stream)
End Using
轉換後的 PDF 檔案會是什麼樣子?
生成的 PDF 將完整保留原始格式,包括字型、顏色、版面配置及嵌入的圖片。 這使得 IronPDF 成為生成專業文件的理想選擇,這些文件需要以通用且可存取的格式進行分享或歸檔。
如何使用郵件合併功能來批次產生 PDF 檔案?
位於 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; }
}
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
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
我已根據需求修改了 WORD 提供的範本。 請下載"派對邀請函"DOTX範例檔案。 針對本使用案例,請將 MailMergePrintAllInOnePdfDocument 屬性設為 true,此設定會將 PDF 文件合併為單一 PdfDocument 物件。 我們將使用的合併欄位為 Recipient's Name 以及 Contact Us。
郵件合併範本的樣式為何?
如何實作郵件合併程式碼?
IronPDF 中的"郵件合併"功能支援多種渲染選項,可透過 DocxPdfRenderOptions 類別進行設定。 這讓您能夠控制諸如頁面方向、邊距,以及是否將所有合併文件整合為單一 PDF 檔案,或產生獨立檔案等設定。
:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-mail-merge.cs
using IronPdf;
using System.Co/llections.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.Co/llections.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
Dim recipients As 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"
}
}
Dim docxToPdfRenderer As New DocxToPdfRenderer()
' Apply render options
Dim 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")
最終合併後的 PDF 檔案會是什麼樣子?
PDF 文件建立後,您可彈性進行後續修改。 這些功能包括將文件匯出為 PDF/A 或 PDF/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 時,是否可以執行郵件合併?
是的,IronPDF 支援「郵件合併」功能,讓您能從單一 DOCX 範本生成批次個人化的 PDF 文件。此功能特別適用於為個別收件者建立客製化文件,同時維持格式的一致性。
將 DOCX 轉換為 PDF 的主要步驟有哪些?
using IronPDF 的流程包含 5 個簡單步驟:1) 下載 C# 函式庫,2) 準備您的 DOCX 檔案,3) 建立 DocxToPdfRenderer 類別的實例,4) 使用 RenderDocxAsPdf 方法並指定檔案路徑,以及 5) 可選用「郵件合併」功能進行批次處理。
為什麼要將 WORD 文件轉換為 PDF 格式?
using IronPDF 轉換為 PDF 格式,可確保在所有裝置和平台上皆具普遍相容性,精準保留原始格式與版面配置,並增添文件安全功能,同時防止未經授權的編輯。無論使用何種檢視軟體或作業系統,PDF 文件皆能維持其原始外觀。
DOCX 轉 PDF 的轉換功能是否相容於不同平台?
是的,IronPDF 支援跨平台部署,可在 Windows、Linux 和 macOS 上使用。此函式庫可與各種 .NET Framework 應用程式配合使用,並提供多種安裝方式,包括 NuGet 套件。
哪些類型的 WORD 文件可以轉換?
IronPDF 可轉換使用 Office Open XML (OOXML) 標準、於 Microsoft WORD 2007 或更新版本中建立的 DOCX 檔案。這包含具有複雜格式、圖片、表格及其他進階 WORD 功能的文件。

