如何使用 C# 將 Microsoft Word 文件轉換為 PDF
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 保留所有原始格式和相容性。
立即開始使用 NuGet 建立 PDF 檔案:
使用 NuGet 套件管理器安裝 IronPDF
複製並運行這段程式碼。
new IronPdf.DocxToPdfRenderer() .RenderDocxAsPdf("document.docx") .SaveAs("output.pdf");部署到您的生產環境進行測試
最簡工作流程(5個步驟)
- 下載用於將 DOCX 轉換為 PDF 的 C# 程式庫
- 準備好要轉換的 DOCX 文件
- Instantiate the `DocxToPdfRenderer` class to render PDF from DOCX file
- Use the
`RenderDocxAsPdf`method and provide the DOCX filepath - 利用郵件合併功能產生一批文檔
如何將 DOCX 檔案轉換為 PDF?
IronPDF 讓開發人員以程式設計方式將 Word 文件轉換為 PDF,從而提高效率,並允許他們將其整合到現有的 .NET 應用程式或他們可能擁有的任何跨平台 .NET 框架應用程式中。 該程式庫支援多種安裝方法,包括 NuGet 套件,並且可以部署在包括Windows 、 Linux和macOS在內的多個平台上。
將 Microsoft Word 檔案轉換為 PDF 格式的過程很簡單。 我們首先實例化DocxToPdfRenderer類別。 使用DocxToPdfRenderer物件的RenderDocxAsPdf方法,並提供 DOCX 檔案的檔案路徑。此方法傳回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");對於更高級的場景,您還可以從位元組數組或串流轉換:
// 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);
}轉換後的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; }
}我根據我們的需求修改了微軟 Word 提供的範本。 請下載Party Invitation DOTX 範例檔。 對於我們的用例,我們將MailMergePrintAllInOnePdfDocument屬性設為 true,這將把 PDF 合併到一個PdfDocument物件中。 我們將使用的合併欄位有:日期、地點、收件人姓名和聯絡我們。
郵件合併範本長什麼樣子?

如何實作郵件合併代碼?
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");合併後的最終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 時,可以執行 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 功能的文件。







