How to Convert Microsoft Word to PDF in C
IronPDF使 C# 开发人员能够使用 DocxToPdfRenderer 类,通过简单的一行方法调用将 DOCX 文件转换为 PDF 格式,保留所有格式并支持批量文档生成的邮件合并功能。
DOCX 文件是在 Microsoft Word 中创建的文档,Microsoft Word 是微软提供的 Microsoft Office 套件中的一种文字处理程序。 它使用 Office Open XML (OOXML) 标准,使其高效且与各种软件兼容。 自 Word 2007 以来,它是 Word 文档的默认格式,取代了最初出版时的旧 DOC 格式。 DOCX 格式具有多种优势,包括通过 ZIP 压缩实现更小的文件大小、更好的数据恢复能力以及提高不同平台的兼容性。
IronPdf 能够将 Word 文档即时转换为 PDF 文件,并提供邮件合并功能,可为单个收件人生成个性化批次文档。 从 DOCX 到 PDF 的转换确保了通用兼容性,保留了格式,并增加了一层安全性。 当您需要分发应在不同设备和操作系统上保持准确外观的文档时,此功能尤其有用,类似于从 HTML 创建 PDF(HTML转PDF) 或图片转PDF 。
快速入门:使用IronPDF将 DOCX 转换为 PDF
使用 IronPDF 能够在 C# 中轻松地将 DOCX 文件转换为 PDF。 本快速指南演示了如何使用 DocxToPdfRenderer 类和 RenderDocxAsPdf 方法,只需一行代码即可实现无缝转换。 这对于希望简化文档处理的开发人员来说是完美的,确保生成的 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 并进行 PDF 编辑?
IronPDF 允许开发人员以编程方式将 Word 文档转换为 PDF,从而简化效率,并允许他们集成到现有的 .NET 应用程序或可能拥有的任何跨平台 .NET Framework 应用程序中。 该库支持各种安装方法,包括 NuGet 包,并可在多个平台上部署,包括Windows、Linux和macOS。
将 Microsoft Word 文件转换为 PDF 格式的过程很简单。 我们首先实例化 DocxToPdfRenderer 类。 通过提供 DOCX 文件的文件路径,使用 RenderDocxAsPdf 对象的 DocxToPdfRenderer 方法。此方法返回一个 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?
邮件合并位于 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; }
}
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
我修改了 Microsoft Word 提供的模板以供我们的目的。 请下载 派对邀请 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");
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 编辑操作。 其中包括将其导出为PDF/A或PDF/UA,以符合存档和可访问性要求,以及添加数字证书以进行文档验证。 您还可以通过合并PDF、PDF拆分或旋转页面来操作单个页面,您还可以选择应用注释和书签,以便更好地进行文档导航。
除了这些功能外,IronPDF 还支持转换为其他文档格式。 例如,您可以将 Markdown 转换为 PDF,将 RTF 转换为 PDF,甚至将 PDF 导出回 HTML。 有关 IronPdf 提供的其他功能的更多信息,请参阅全面的 HTML to PDF 教程。
常见问题解答
如何在C#中将DOCX文件转换为PDF?
有了 IronPDF,只需一行代码,您就可以使用 DocxToPdfRenderer 类将 DOCX 转换为 PDF。只需实例化 DocxToPdfRenderer,使用 DOCX 文件路径调用 RenderDocxAsPdf 方法,然后使用 SaveAs 方法保存输出即可。
什么是 DocxToPdfRenderer 类?
DocxToPdfRenderer 是 IronPDF 的专用类,用于将 Microsoft Word 文档转换为 PDF 格式。它提供了一个简单的应用程序接口,允许开发人员转换 DOCX 文件,同时保留所有原始格式、布局和文档属性。
Word 转换为 PDF 是否保留格式?
是的,IronPDF 的 DocxToPdfRenderer 保留了原始 Word 文档的所有格式,包括字体、样式、图像、表格和布局,确保 PDF 输出与原始 DOCX 文件完全匹配。
将 DOCX 转换为 PDF 时能否执行邮件合并?
是的,IronPDF 支持邮件合并功能,允许您从单个 DOCX 模板生成个性化批次的 PDF 文档。这对于在保持格式一致的同时为单个收件人创建定制文档特别有用。
将 DOCX 转换为 PDF 的主要步骤是什么?
使用 IronPDF 的过程包括 5 个简单的步骤:1)下载 C# 库;2)准备 DOCX 文件;3)实例化 DocxToPdfRenderer 类;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 功能的文档。

