跳至页脚内容
使用IRONPDF

PDF转换器.NET(开发者教程)

1.0 介绍

Adobe 的便携式文档格式 (PDF) 广泛用于文档查看和交换。 开发人员经常需要创建 PDF 以满足客户需求,而现代库简化了这一过程。 在为项目选择库时,考虑构建、读取和转换功能等特性非常重要。

2.0 IronPDF 功能

IronPDF 是一个多功能库,用于创建、读取和编辑 PDF 文档,并具备使用 Chrome 引擎将 HTML 转换为 PDF 的功能。它支持广泛的 Web 组件,可用于 ASP.NET Web 应用程序和传统 Windows 应用程序。 该库允许使用 HTML5、JavaScript、CSS 和图像创建视觉上吸引人的 PDF,并包含强大的 HTML 转 PDF 转换器。

3.0 从 URL 创建 PDF 文档

使用 IronPDF 内置的 Chrome 浏览器和 API 库从网页生成 PDF 文件很容易。 只需提供 URL 并使用 IronPDF API 库将其转换为 PDF 文件。 文档转换可以通过几行代码快速完成:

// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified URL to a PDF
var pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified URL to a PDF
var pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()

' Convert the specified URL to a PDF
Dim pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/")

' Save the PDF to the specified path
pdf.SaveAs("result.pdf")
$vbLabelText   $csharpLabel

RenderUrlAsPdf 方法可用于快速将 URL 转换为 PDF 文档。 只需提供 URL 和所需的保存位置,IronPDF 就会根据上文提供的信息生成 PDF 文件。 这使得只需几行代码即可轻松将网页转换为 PDF 文档。

PDF Converter .NET(开发者教程),图 1:从 Google URL 生成的 PDF 文件 从 Google URL 生成的 PDF 文件

4.0 从 HTML 字符串创建 PDF

使用 IronPDF API 库可以快速将 HTML 字符串转换为 PDF 文件。 以下代码片段可以将 HTML 字符串转换为文档,并且可以将任何 HTML 标签翻译成 PDF 文件。

// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified HTML string to a PDF
var pdf = renderer.RenderHtmlAsPdf("<p>Hello world!!</p>");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified HTML string to a PDF
var pdf = renderer.RenderHtmlAsPdf("<p>Hello world!!</p>");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()

' Convert the specified HTML string to a PDF
Dim pdf = renderer.RenderHtmlAsPdf("<p>Hello world!!</p>")

' Save the PDF to the specified path
pdf.SaveAs("result.pdf")
$vbLabelText   $csharpLabel

此代码片段说明了如何使用 RenderHtmlAsPdf 函数将 HTML 文本转换为 PDF。 转换 HTML 为字符串的函数可以接受尽可能多的 HTML 代码。 然后可以通过使用SaveAs功能快速轻松地完成该过程,将文档保存。

PDF Converter .NET(开发者教程),图 2:从 HTML 字符串生成的输出 PDF 文件 从 HTML 字符串生成的输出 PDF 文件

5.0 从 HTML 文件创建 PDF 文件

IronPDF API 库允许快速将 HTML 文件转换为 PDF 文件。 使用下面的示例代码可以将任何 HTML 标签转化为 PDF 文件。

// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified HTML file to a PDF
var pdf = renderer.RenderHtmlFileAsPdf("test.html");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified HTML file to a PDF
var pdf = renderer.RenderHtmlFileAsPdf("test.html");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()

' Convert the specified HTML file to a PDF
Dim pdf = renderer.RenderHtmlFileAsPdf("test.html")

' Save the PDF to the specified path
pdf.SaveAs("result.pdf")
$vbLabelText   $csharpLabel

HTML 文本如下所示:

<p style="color:red">Hello world</p>
<p style="color:red">Hello world</p>
HTML

PDF Converter .NET(开发者教程),图 3:从 HTML 文件生成的输出 PDF 文件 从 HTML 文件生成的输出 PDF 文件

6.0 从富文本文件创建 PDF 文件

IronPDF API 库还可以快速从富文本文件 (RTF) 创建 PDF。 将 RTF 转换为 PDF 的示例代码可用于将任意数量的 RTF 转换为单个 PDF 文件。代码如下。

// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified RTF file to a PDF
var pdf = renderer.RenderRtfFileAsPdf("test.rtf");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified RTF file to a PDF
var pdf = renderer.RenderRtfFileAsPdf("test.rtf");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()

' Convert the specified RTF file to a PDF
Dim pdf = renderer.RenderRtfFileAsPdf("test.rtf")

' Save the PDF to the specified path
pdf.SaveAs("result.pdf")
$vbLabelText   $csharpLabel

PDF Converter .NET(开发者教程),图 4:从 RTF 文件生成的输出 PDF 文件 从 RTF 文件生成的输出 PDF 文件

在上图中,左侧显示源文档,而右侧显示转换后的 PDF 文件。 此外,我们还可以利用 RTF 字符串通过下面提供的代码创建 PDF。

// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified RTF string to a PDF
var pdf = renderer.RenderRtfStringAsPdf("{\\rtf1...}");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified RTF string to a PDF
var pdf = renderer.RenderRtfStringAsPdf("{\\rtf1...}");

// Save the PDF to the specified path
pdf.SaveAs("result.pdf");
' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()

' Convert the specified RTF string to a PDF
Dim pdf = renderer.RenderRtfStringAsPdf("{\rtf1...}")

' Save the PDF to the specified path
pdf.SaveAs("result.pdf")
$vbLabelText   $csharpLabel

7.0 从 Markdown 文件创建 PDF 文件

得益于 IronPDF API 模块,可以快速从 Markdown 文件生成 PDF。 使用以下示例代码,任意数量的 Markdown 文件均可转换为 PDF 文件。

// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified Markdown file to a PDF
var pdf = renderer.RenderMarkdownFileAsPdf("Markdown.md");

// Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf");
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified Markdown file to a PDF
var pdf = renderer.RenderMarkdownFileAsPdf("Markdown.md");

// Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf");
' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()

' Convert the specified Markdown file to a PDF
Dim pdf = renderer.RenderMarkdownFileAsPdf("Markdown.md")

' Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf")
$vbLabelText   $csharpLabel

PDF Converter .NET(开发者教程),图 5:从 Markdown 文件生成的输出 PDF 文件 从 Markdown 文件生成的输出 PDF 文件

如上图所示,源文档在左侧,转换后的 PDF 文件在右侧。 下面提供的代码可以将 MD 字符串转换为 PDF。 有关使用 IronPDF 进行 HTML 转换的更多信息,请访问此HTML 到 PDF 转换教程

// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified Markdown string to a PDF
var pdf = renderer.RenderMarkdownStringAsPdf("# Hello world\n\nHello world");

// Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf");
// Create a new instance of the ChromePdfRenderer class
var renderer = new IronPdf.ChromePdfRenderer();

// Convert the specified Markdown string to a PDF
var pdf = renderer.RenderMarkdownStringAsPdf("# Hello world\n\nHello world");

// Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf");
Imports Microsoft.VisualBasic

' Create a new instance of the ChromePdfRenderer class
Dim renderer = New IronPdf.ChromePdfRenderer()

' Convert the specified Markdown string to a PDF
Dim pdf = renderer.RenderMarkdownStringAsPdf("# Hello world" & vbLf & vbLf & "Hello world")

' Save the PDF to the specified path
pdf.SaveAs("Markdown_result.pdf")
$vbLabelText   $csharpLabel

有关如何使用 IronPDF API 库的更多信息,请参阅开发者文档IronPDF 文档资源

8.0 总结

IronPDF 库为开发提供免费许可证,并且根据开发人员的需求,提供几种可供购买的许可证以便在生产环境中使用。 Lite 套餐的初始价格为 $799,且无后续费用。 许可证包括永久许可证、30 天退款保证、一年产品支持和升级,以及 SaaS 和 OEM 重新分发的可能性。 它们是一次性购买,可用于开发、测试和生产。 IronPDF 还提供额外的限时免费许可证和用于重新分发保护的免费许可证。 有关 IronPDF 的完整定价和许可信息,请访问 IronPDF 许可信息页面

常见问题解答

如何在 .NET 中将 HTML 转换为 PDF 而不丢失格式?

您可以使用 IronPDF 通过利用 Chrome 引擎将 HTML 转换为 PDF,而不会丢失格式。该库支持 HTML5、JavaScript 和 CSS,确保您的 PDF 保持其原始布局和设计。

转换 URL 为 PDF 有哪些方法?

IronPDF 提供 RenderUrlAsPdf 方法,允许使用 ChromePdfRenderer 类轻松将 URL 转换为 PDF 文档。

是否有办法在 C# 中将 Markdown 转换为 PDF?

是的,IronPDF 可以通过使用 RenderMarkdownFileAsPdf 方法将 Markdown 文件转换为 PDF。这使得开发人员能够高效地将 markdown 内容转换为 PDF 格式。

如何使用 .NET 将 RTF 文件转换为 PDF 文档?

IronPDF 通过 RenderRtfFileAsPdf 方法使 RTF 文件转换为 PDF,允许快速准确地将富文本文件转换为 PDF 格式。

我可以从 PDF 文件中提取文本和图像吗?

是的,IronPDF 允许您轻松从 PDF 文档中提取文本和图像,提供在 PDF 中搜索和操作文本和图像的功能。

有哪些选项可以创建交互式 PDF 表单?

IronPDF 支持创建和编辑交互式 PDF 表单,允许开发人员在其 PDF 文档中添加表单字段、图形、书签和水印。

我如何在 .NET 中合并或分割 PDF 文档?

IronPDF 提供了合并和分割 PDF 文档的功能,允许开发人员将多个 PDF 合并为一个或将一个 PDF 分割为多个文档。

IronPDF 为生产用途提供哪些许可选项?

IronPDF 提供多种生产使用的许可选项,包括不产生持续费用的 Lite 套餐。许可证提供永久使用权、30 天退款保证和一年的产品更新和支持。

IronPDF 可以用于 ASP.NET Web 和 Windows 应用程序吗?

是的,IronPDF 兼容 ASP.NET Web 应用程序和传统 Windows 应用程序,为在不同环境中工作的开发人员提供灵活性。

IronPDF 支持受密码保护的 PDF 文件吗?

IronPDF 可以处理受密码保护的 PDF 文件,允许用户提供必要的凭据来读取和编辑这些文档。

.NET 10:IronPDF 是否完全支持面向 .NET 10 的项目?

是的。IronPDF 完全兼容 .NET 10,包括新增的运行时和 C# 语言增强功能,支持桌面、Web、微服务和云/容器环境。它无需任何变通方案或自定义 API,即可在 .NET 10 项目中开箱即用。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。