使用IRONPDF

如何在C#中将Word转换为PDF(教程)

更新 2024年二月25日
分享:

Word 转换为 PDF 目前是一种公认的做法,可以在共享文档前使用。您可以使用任何在线工具将 Word 文档转换为 PDF 文件。Microsoft Word 有将 Word DOCX 文件转换为 PDF 的内置功能,但在某些情况下,你可能希望以编程方式将 Word 文档改写为 PDF,比如:

  • 在服务器上从 Word 文档生成 PDF。
  • Microsoft Word 在服务器上没有授权。因此,有必要使用 C# 库将 Word 转换为 PDF。
  • 使用票据、发票、管理报告等常用格式创建自定义 PDF。
  • 在 MS Word 中设计或编辑 PDF 模板,然后在网络服务器上创建带有自定义信息的自定义副本。

让我们看看如何使用 C# PDF 库将 DOCX 或 doc 转换为 PDF。

IronPDF:C# PDF Library

IronPDF 是一个 C# PDF 库,可让 .NET 开发人员轻松创建和处理 PDF。有了 IronPDF,你可以用 C# 轻松地将 HTML 内容转换成 PDF。IronPDF 库还支持数字签名、表格填写、PDF 到图片的转换等功能。

无论您是需要为您的 Web 应用程序生成 PDF,还是只是想为现有的 .NET 应用程序添加一些 PDF 功能,IronPDF 都是一个 .NET API。查看 综合文献 立即开始将 Microsoft Office Word DOCX 转换为 PDF。

本文将使用 IronPDF 来演示如何使用 C# 和 .NET 将 Word 文档转换并保存为 PDF 文档。

先决条件

将 Word 文件转换为 PDF 文档需要一些先决条件。

1.Visual Studio 2022 (推荐)

2.运行中的 .NET 应用系统,带有最新的 .NET Framework (推荐)

3.已安装 Microsoft Office

4.稳定的互联网连接,以便安装用于 PDF 转换的 IronPDF 库

让我们进入将 Word 文件转换为 PDF 文档的主要步骤。

第 1 步:将 Word 文档导出为 HTML

第一步,先将 Word 文档转换为 HTML,然后再将其转换为 PDF 文档。

要将 DOC 或 DOCX 文件导出为 HTML 格式,请执行以下操作:

1.启动 Microsoft Word 并打开 Word 文件。

![如何用 C# 将 Word 转换为 PDF(教程),图 1:加载 Word 文档示例](/static-assets/pdf/blog/word-to-pdf-csharp-tutorial/word-to-pdf-csharp-tutorial-1.webp)

**加载 Word 文档样本**

2.转到 "文件 "选项卡,从侧菜单中选择 "另存为"。

![如何用 C# 将 Word 转换为 PDF(教程),图 2:另存为选项](/static-assets/pdf/blog/word-to-pdf-csharp-tutorial/word-to-pdf-csharp-tutorial-2.webp)

**另存为选项**

3.点击浏览按钮。选择所需的位置,然后从 "文件类型 "下拉菜单中选择 "HTML 页面 "选项。

![如何用 C# 将 Word 转换为 PDF(教程),图 3:保存文件](/static-assets/pdf/blog/word-to-pdf-csharp-tutorial/word-to-pdf-csharp-tutorial-3.webp)

**保存文件**

按照上述步骤,Word 文件将转换为 HTML 文件。现在,我们将使用导出的 HTML 文件进行 PDF 转换。

第 2 步:将 IronPDF 添加到 Visual Studio 解决方案中

NuGet 软件包管理器

在 Visual Studio 中,右键单击项目的解决方案资源管理器,然后选择 Manage NuGet Packages for Solution...

如何用 C# 将 Word 转换为 PDF(教程),图 4:NuGet 包管理器

NuGet软件包管理器

在那里,只需搜索 IronPDF 并安装最新版本的库。在出现的对话框中单击 "确定"。这在 VB.NET 项目中也同样有效。

如何用 C# 将 Word 转换为 PDF(教程),图 5:搜索 IronPDF

搜索 IronPDF

软件包管理器控制台

或者,在 Visual Studio 中,导航到顶部的 "工具 "菜单,选择 "NuGet 包管理器",然后从菜单中选择 "包管理器控制台"。

如何用 C# 将 Word 转换为 PDF(教程),图 6:软件包管理器控制台

软件包管理器控制台

在打开的 shell 中,粘贴以下内容并按回车键:

Install-Package IronPdf

该命令将在项目中安装最新版本的 IronPDF。

NuGet 网站上的 IronPDF

如需全面了解 IronPDF 的功能、兼容性和下载情况,请访问 IronPDF on NuGet 官方网站.

通过 DLL 安装

另一种方法是直接安装 IronPDF 动态链接库。可以从以下网址下载 IronPDF 并手动安装到项目或 GAC 中 链接.

第 3 步:替换占位符文本、添加页眉和水印

现在是以编程方式将新文档文件转换为 PDF 的时候了。打开 Program.cs 文件并编写以下示例中的代码。使用以下代码在文档中央添加 HTML 页眉和水印图像。

包括 IronPDF

要在代码文件中包含 IronPDF,请添加命名空间 using IronPdf;,以使用它。

using IronPdf;

var ironRenderer = new ChromePdfRenderer();  
using IronPdf;

var ironRenderer = new ChromePdfRenderer();  
Imports IronPdf

Private ironRenderer = New ChromePdfRenderer()
VB   C#

添加 HTML 页眉

HTML 片段可帮助在 PDF 文件的页眉处添加 HTML 字符串。你可以设置多种渲染选项,如页眉、页脚、页边距、纸张大小等。

// Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>";  
// Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>";  
' Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>"
VB   C#

将 HTML 文件转换为 PDF

转换 渲染 HTMLFileAsPdf 函数用于将 Word 文档导出的 HMTL 文件转换为 PDF 文件,然后在该方法的参数中传递 HTML 文件路径。

// Add the Word document as the source file and apply ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  
// Add the Word document as the source file and apply ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  
' Add the Word document as the source file and apply ironRenderer settings to it  
Dim pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html")
VB   C#

在 PDF 文档中应用印章

接下来,使用样本图像在生成的 PDF 文件中应用 "印章"。

// Adds a stamp  
pdf.ApplyStamp(new ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg")); 
// Adds a stamp  
pdf.ApplyStamp(new ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg")); 
' Adds a stamp  
pdf.ApplyStamp(New ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg"))
VB   C#

保存 PDF

现在,将生成的 PDF 文件保存到磁盘中:

pdf.SaveAs("word.pdf");
pdf.SaveAs("word.pdf");
pdf.SaveAs("word.pdf")
VB   C#

下面是所用程序代码的全文:

using IronPdf;

var ironRenderer = new ChromePdfRenderer();  

// Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>";  

// Adds our Word document as the source file and applies ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  

// Adds a stamp  
pdf.ApplyStamp(new ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg"));  

// Saves the document to a PDF file
pdf.SaveAs("word.pdf");
using IronPdf;

var ironRenderer = new ChromePdfRenderer();  

// Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>";  

// Adds our Word document as the source file and applies ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  

// Adds a stamp  
pdf.ApplyStamp(new ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg"));  

// Saves the document to a PDF file
pdf.SaveAs("word.pdf");
Imports IronPdf

Private ironRenderer = New ChromePdfRenderer()

' Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>"

' Adds our Word document as the source file and applies ironRenderer settings to it  
Dim pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html")

' Adds a stamp  
pdf.ApplyStamp(New ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg"))

' Saves the document to a PDF file
pdf.SaveAs("word.pdf")
VB   C#

按照上述方法,无需 Word Interop 的帮助,就能简单地将 Word 文件成功转换为 PDF 文件。

PDF 文件的输出

从 Word 文档转换而来的输出 PDF 文件,使用 IronPDF 可完美保留所有格式和印章。

如何用 C# 将 Word 转换为 PDF(教程),图 7:IronPDF 输出

IronPDF 输出

摘要

如果您正在寻找一种使用 C# 将 HTML 文档转换为 PDF 的简单方法,我们向您推荐 IronPDF。它是一个很棒的库,能让转换过程简单明了。它的一些重要功能包括

查看更多 教程 了解如何在您的项目中使用 IronPDF。感谢阅读!

< 前一页
PDF查看器C# Windows应用程序(教程)
下一步 >
如何在C#中读取PDF文件

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >