C# 创建 PDF - 使用 IronPDF 的完整指南
简介
从HTML内容创建PDF文档已经成为各种应用程序的重要要求,从生成发票到存档网页内容不等。 IronPDF 是一个强大且多功能的 .NET 库,它简化了将 HTML 转换为 PDF 的过程,使开发人员在 C# 中轻松创建高质量的 PDF 文档。 在本文中,我们将探讨 IronPDF 的功能,并提供一个使用它来创建 PDF 的分步指南。
如何在 C# 中使用 IronPDF 库创建 PDF 文件
- 创建一个新的 Visual Studio 项目
- 从 NuGet 包管理器中添加 IronPDF 库
- 创建一个简单的PDF文件。
- 修改PDF文件。
- 添加页眉和页脚
- 包含外部样式表和脚本
- 包含图像和位图。
- HTML 文件到 PDF 的转换。
- URL 到 PDF 的转换。
IronPDF简介
IronPDF是一个.NET库,允许开发人员轻松将HTML转换为PDF。 它支持广泛的功能,包括CSS、JavaScript,甚至嵌入的图像。 使用IronPDF,您可以创建与您的HTML网页完全相同的PDF,确保格式之间的无缝转换。 该库对于需要即时生成动态PDF文档的Web应用程序特别有用。
IronPDF允许开发人员无缝将PDF功能集成到.NET应用程序中,而无需手动管理PDF文件结构。 IronPDF 利用基于 Chrome 的渲染引擎将 HTML 页面(包括复杂的 CSS、JavaScript 和图像)转换为结构良好的 PDF 文档。 它可以用于生成报告、发票、电子书或任何需要以PDF格式展示的文档。
IronPDF功能多样,提供不仅仅渲染PDF的功能,还提供广泛的PDF操作选项,如编辑、表单处理、加密等。
IronPDF C# 创建 PDF 文件的关键功能
- HTML到PDF转换
HTML 渲染:IronPDF 可以只用几行代码直接将 HTML 文档或网页(包括带 CSS、图像和 JavaScript 的 HTML)转换为 PDF 文档,非常适合从动态网页内容生成 PDF。
支持现代 HTML/CSS:IronPDF 处理现代 HTML5、CSS3 和 JavaScript,确保您的基于网页的内容以 PDF 准确呈现,保留布局、字体和交互元素。
- 高级渲染:它使用 Chrome 的渲染引擎(通过 Chromium)进行准确和高质量的 PDF 生成,使其比许多其他 HTML 到 PDF 库更可靠。
- 网站 URL 到 PDF:IronPDF 可以将网站的字符串 URL 作为输入并转换为 PDF。
- 自定义页眉和页脚
IronPDF 允许开发人员添加自定义页眉和页脚到 PDF 文档中,这些页眉和页脚可以包括动态内容如页码、文档标题或自定义文本。
- 页眉和页脚可以添加到单个页面或整个文档中作为一致的元素。
支持PDF中的JavaScript
- IronPDF 允许在 PDF 生成之前在 HTML 内容中执行 JavaScript。 这允许动态内容呈现,例如在生成的PDF中进行表单计算或交互。
- 编辑现有PDFs
IronPDF 提供编辑现有 PDF 的能力。 您可以修改文本、图像,并向现有PDF文件添加注释。 该功能对于为文档添加水印、签名或更新PDF文件内的内容非常有用。
- 文本提取和修改允许您以编程方式操作 PDF 文档中的内容。
- 合并和拆分PDFs
- IronPDF 允许您将多个 PDF 文件合并为一个文档,或将大 PDF 拆分为较小的文件,非常适合需要将文档合并或分解为更易管理部分的工作流。
支持交互式表单
- 您可以使用 IronPDF 创建、填写和操作 PDF 表单。 它全面支持交互式表单(如文本字段、复选框和单选按钮),并允许您使用数据预先填写表单。
- 页面操作
- IronPDF 提供操控 PDF 文档中的各个页面的方法,如旋转页面、删除页面或重新排序。
- 安全和加密
- IronPDF 允许您对 PDF 应用密码保护和加密,确保您的文档安全。 您可以设置用户权限以防止打印、复制或编辑该 PDF。
加水印和品牌化
- 使用 IronPDF 为 PDF 文档添加水印很简单。 您可以将文本或图像叠加为页面上的水印,防止您的文档被未经授权的复制或分发。
- 提取文本和图像
- IronPDF 允许从 PDF 文档中提取文本和图像,使开发人员能够提取数据以进行处理或重用。
- Unicode和多语言支持
- IronPDF 具有强大的 Unicode 支持,这意味着它可以处理国际字符和字体,非常适合生成多语种 PDF。
- 针对性能优化
- IronPDF 针对性能进行了优化,能够处理大 PDF 文档和高数量的请求。 该库确保即使在处理大量数据集或图像时也能快速高效地生成 PDF。
- API和开发者友好工具
- IronPDF 附带全面且易于使用的 API。 开发人员可以通过简单的方法调用快速开始执行复杂任务。
- 跨平台支持
- IronPDF 是跨平台兼容的,这意味着它可以在 Windows 和 Linux 环境中使用,允许您在不同操作系统中生成和操作 PDF。
步骤 1:创建一个新的 Visual Studio 项目
现在,让我们开始创建一个新项目,打开 Visual Studio 并创建一个新项目,如下所示。

选择创建控制台应用程序。

提供项目名称和位置。

选择 .NET 版本

创建一个新项目。
步骤 2:从 NuGet 包管理器中添加 IronPDF 库
使用 Visual Studio 控制台应用程序中的 NuGet 包管理器,您可以使用以下命令添加 IronPDF NuGet 库。
此外,可以使用 Visual Studio 包管理器安装 IronPDF。

步骤 3:创建一个简单的PDF文件。
使用 IronPDF 库轻松生成 PDF 文档。 现在,让我们开始创建一个简单的空白 PDF 文件。
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a new PDF document with specific dimensions (270x270 points)
PdfDocument pdf = new PdfDocument(270, 270);
// Save the blank PDF document to disk
pdf.SaveAs("simple.pdf");
}
}using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a new PDF document with specific dimensions (270x270 points)
PdfDocument pdf = new PdfDocument(270, 270);
// Save the blank PDF document to disk
pdf.SaveAs("simple.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Create a new PDF document with specific dimensions (270x270 points)
Dim pdf As New PdfDocument(270, 270)
' Save the blank PDF document to disk
pdf.SaveAs("simple.pdf")
End Sub
End Class代码解释
该程序演示了如何在 C# 中使用 IronPDF 库创建 PDF 文档。 以下是代码中发生的事情:
许可证密钥设置:程序首先设置 IronPDF 库的许可证密钥。 这是使用库完整功能所必需的,因为许可证密钥确保您可以访问完整功能(而不只是受限于试用版)。
创建 PDF 文档:然后,程序创建一个大小为270x270 点的新PDF 文档。 一个点是打印中的计量单位,相当于1/72英寸。 因此,这会创建一个大约3.75英寸 x 3.75英寸的正方形文档。
- 保存PDF:创建完空白文档后,程序将 PDF 保存为"simple.pdf"。 由于没有向文档中添加任何内容,输出将是完全空白(黑色) 的 PDF。
步骤 4:修改 PDF 文件
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Load the existing PDF document
var pdf = new PdfDocument("simple.pdf");
// Create a renderer for converting HTML to PDF
var renderer = new ChromePdfRenderer();
// Render HTML content as a PDF
var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");
// Prepend the rendered page as the first page of the existing PDF
pdf.PrependPdf(pagePdf);
// Save the modified PDF with a new filename
pdf.SaveAs("simple_WithTitle.pdf");
}
}using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Load the existing PDF document
var pdf = new PdfDocument("simple.pdf");
// Create a renderer for converting HTML to PDF
var renderer = new ChromePdfRenderer();
// Render HTML content as a PDF
var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");
// Prepend the rendered page as the first page of the existing PDF
pdf.PrependPdf(pagePdf);
// Save the modified PDF with a new filename
pdf.SaveAs("simple_WithTitle.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Load the existing PDF document
Dim pdf = New PdfDocument("simple.pdf")
' Create a renderer for converting HTML to PDF
Dim renderer = New ChromePdfRenderer()
' Render HTML content as a PDF
Dim pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>")
' Prepend the rendered page as the first page of the existing PDF
pdf.PrependPdf(pagePdf)
' Save the modified PDF with a new filename
pdf.SaveAs("simple_WithTitle.pdf")
End Sub
End Class代码解释
许可证密钥设置:设置 IronPDF 许可证密钥以启用库的完整功能。
加载现有 PDF:将名为"simple.pdf"的现有 PDF 文件加载到一个新的 PdfDocument 对象中。 这是将被新封面页添加到前面的 PDF。
将HTML渲染为PDF: 使用 ChromePdfRenderer 对象将 HTML 内容渲染为 PDF。 在此示例中,HTML 内容是一个简单的
<h1>标签,其中包含文本 "Awesome IronPDF Library",通过RenderHtmlAsPdf方法将其转换为封面页 PDF。添加封面页:使用
PrependPdf方法将生成的封面 PDF(来自 HTML) 插入到现有 PDF 文档的开头。- 保存修改后的 PDF:将修改后的 PDF(带有新封面页)保存为"simple_WithTitle.pdf"。 原始内容《simple.pdf》已保留,但现在以新添加的标题页开头。
输出PDF

步骤 5:添加页眉和页脚
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a new HtmlToPdf renderer
var renderer = new ChromePdfRenderer();
// Define the HTML content
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>";
// Define headers and footers as HTML
string headerHtml = "<div style='text-align: right;'>Page {page} of {total-pages}</div>";
string footerHtml = "<div style='text-align: center;'>Confidential</div>";
// Convert the HTML content to a PDF document
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader = new HtmlHeaderFooter() { HtmlFragment = headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment = footerHtml }
});
// Save the PDF document with headers and footers
pdfDocument.SaveAs("report.pdf");
}
}using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a new HtmlToPdf renderer
var renderer = new ChromePdfRenderer();
// Define the HTML content
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>";
// Define headers and footers as HTML
string headerHtml = "<div style='text-align: right;'>Page {page} of {total-pages}</div>";
string footerHtml = "<div style='text-align: center;'>Confidential</div>";
// Convert the HTML content to a PDF document
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader = new HtmlHeaderFooter() { HtmlFragment = headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment = footerHtml }
});
// Save the PDF document with headers and footers
pdfDocument.SaveAs("report.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Create a new HtmlToPdf renderer
Dim renderer = New ChromePdfRenderer()
' Define the HTML content
Dim htmlContent As String = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>"
' Define headers and footers as HTML
Dim headerHtml As String = "<div style='text-align: right;'>Page {page} of {total-pages}</div>"
Dim footerHtml As String = "<div style='text-align: center;'>Confidential</div>"
' Convert the HTML content to a PDF document
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlHeader = New HtmlHeaderFooter() With {.HtmlFragment = headerHtml},
.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment = footerHtml}
})
' Save the PDF document with headers and footers
pdfDocument.SaveAs("report.pdf")
End Sub
End Class代码片段说明
许可证密钥设置:设置 IronPDF 许可证密钥以启用完整功能。
创建 PDF 渲染器:创建 ChromePdfRenderer 实例以将 HTML 内容渲染为 PDF 格式。
定义 HTML 内容:创建一个包括标题、标题和段落的简单 HTML 字符串。
定义页眉和页脚 HTML:
- 页眉 包含格式为 "Page {page} of {total-pages}" 的右对齐页码。
- 页脚 包括文本 "机密",居中对齐到每一页的底部。
HTML 转 PDF 转换:使用 RenderHtmlAsPdf 方法将 HTML 内容转换为 PDF 文档。
添加页眉和页脚:使用 AddHtmlHeadersAndFooters 方法向 PDF 添加定义的页眉和页脚。
- 保存PDF:将生成的 PDF 保存为名为"report.pdf"的文件。
输出PDF

步骤 6:包含外部样式表和脚本
using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Define the HTML content with links to external CSS and JS files
string htmlContent = @"
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h2>Styled Content</h2>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
// Create a PDF renderer instance
var renderer = new ChromePdfRenderer();
// Render HTML content to a PDF
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to disk
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}using IronPdf;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Define the HTML content with links to external CSS and JS files
string htmlContent = @"
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h2>Styled Content</h2>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
// Create a PDF renderer instance
var renderer = new ChromePdfRenderer();
// Render HTML content to a PDF
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to disk
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Define the HTML content with links to external CSS and JS files
Dim htmlContent As String = "
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h2>Styled Content</h2>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>"
' Create a PDF renderer instance
Dim renderer = New ChromePdfRenderer()
' Render HTML content to a PDF
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF to disk
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf")
End Sub
End Classstyles.css
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #007BFF;
}
p {
font-size: 14px;
line-height: 1.6;
}script.js
// script.js
document.addEventListener('DOMContentLoaded', function() {
var dynamicText = document.getElementById('dynamic-text');
dynamicText.textContent = "This content has been modified by JavaScript.";
});// script.js
document.addEventListener('DOMContentLoaded', function() {
var dynamicText = document.getElementById('dynamic-text');
dynamicText.textContent = "This content has been modified by JavaScript.";
});代码解释
此代码演示了如何在 C# 中使用 IronPDF 从包含指向外部 CSS 和 JavaScript 文件的链接的 HTML 内容生成 PDF。
许可证密钥设置:设置 IronPDF 许可证密钥以启用完整功能。
定义具有外部资源的 HTML 内容:
- 使用指向外部 CSS 文件(styles.css)的链接来为内容设置样式。
- 使用指向外部 JavaScript 文件(script.js)的链接来添加动态功能。
将HTML渲染为PDF:使用 RenderHtmlAsPdf 方法将 HTML 内容(包括链接的 CSS 和 JavaScript)转换为 PDF 文档。
- 保存 PDF:将生成的 PDF 文档保存为名为"awesomeIronPDF_styled_content.pdf"的文件。
笔记
- 外部 CSS 和 JS 文件:确保代码运行环境中可访问链接的styles.css和script.js文件。
- PDF中的JavaScript:IronPDF 执行 JavaScript 以在生成 PDF 之前渲染内容。
输出PDF

步骤 7:包含图像和位图。
using IronPdf;
using System;
using System.IO;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a PDF renderer instance
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Import image file as bytes
byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own image file here
// Convert image bytes to Base64 string
string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);
// Create HTML content with the embedded Base64 image
string imgHtml = $"<img src='{imgDataUri}'>";
// Render HTML content to a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml);
// Save the PDF with the embedded image
pdf.SaveAs("embedded_sample.pdf");
}
}using IronPdf;
using System;
using System.IO;
class Program
{
static void Main()
{
// Set your IronPDF license key
IronPdf.License.LicenseKey = "your key";
// Create a PDF renderer instance
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Import image file as bytes
byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own image file here
// Convert image bytes to Base64 string
string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);
// Create HTML content with the embedded Base64 image
string imgHtml = $"<img src='{imgDataUri}'>";
// Render HTML content to a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml);
// Save the PDF with the embedded image
pdf.SaveAs("embedded_sample.pdf");
}
}Imports IronPdf
Imports System
Imports System.IO
Friend Class Program
Shared Sub Main()
' Set your IronPDF license key
IronPdf.License.LicenseKey = "your key"
' Create a PDF renderer instance
Dim renderer As New ChromePdfRenderer()
' Import image file as bytes
Dim base64Bytes() As Byte = File.ReadAllBytes("image.jpg") ' Use your own image file here
' Convert image bytes to Base64 string
Dim imgDataUri As String = "data:image/png;base64," & Convert.ToBase64String(base64Bytes)
' Create HTML content with the embedded Base64 image
Dim imgHtml As String = $"<img src='{imgDataUri}'>"
' Render HTML content to a PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(imgHtml)
' Save the PDF with the embedded image
pdf.SaveAs("embedded_sample.pdf")
End Sub
End Class代码解释
该C#程序演示了如何使用IronPDF将图像嵌入到PDF文档中,通过将图像转换为Base64字符串并将其嵌入到HTML内容中。
许可证密钥设置:设置 IronPDF 许可证密钥以启用完整功能。
图像导入与转换为Base64:
- 将图像文件(例如:image.jpg)读取为字节数组。
- 将该字节数组转换为Base64字符串,并添加适当的图像类型数据前缀。
嵌入到HTML中的图像:包含该图像的Base64字符串被嵌入到HTML
<img>标签中。渲染HTML为PDF:使用 IronPDF 的 ChromePdfRenderer 将带有嵌入图像的 HTML 渲染为PDF文档。
- 保存PDF:将生成的PDF保存为"embedded_sample.pdf",其中现在包括嵌入的图像。
输出PDF

步骤 8:HTML 文件到 PDF 转换。
using IronPdf;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from an existing HTML file using C#
var pdf = renderer.RenderHtmlFileAsPdf("sample.html");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
}
}using IronPdf;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from an existing HTML file using C#
var pdf = renderer.RenderHtmlFileAsPdf("sample.html");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Instantiate Renderer
Dim renderer = New ChromePdfRenderer()
' Create a PDF from an existing HTML file using C#
Dim pdf = renderer.RenderHtmlFileAsPdf("sample.html")
' Export to a file or Stream
pdf.SaveAs("output.pdf")
End Sub
End Class代码解释
此程序演示了如何使用 IronPDF 库在 C# 中将现有 HTML 文件转换为 PDF 文档。
实例化渲染器:创建 ChromePdfRenderer 实例,负责将 HTML 内容渲染为 PDF。
将 HTML 转换为 PDF:使用
RenderHtmlFileAsPdf方法将 HTML 文件(sample.html)转换为 PDF 文档。- 保存PDF:使用
SaveAs方法将 PDF 文档保存为 "output.pdf"。
步骤 9:URL 到 PDF 转换。
以下代码片段演示了如何使用 IronPDF 将 URL 转换为 PDF。
using IronPdf;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a URL or local file path
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Export to a file or Stream
pdf.SaveAs("url.pdf");
}
}using IronPdf;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a URL or local file path
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Export to a file or Stream
pdf.SaveAs("url.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Instantiate Renderer
Dim renderer = New ChromePdfRenderer()
' Create a PDF from a URL or local file path
Dim pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")
' Export to a file or Stream
pdf.SaveAs("url.pdf")
End Sub
End Class代码解释
实例化渲染器:创建
ChromePdfRenderer的实例。将 URL 或本地文件转换为 PDF:使用
RenderUrlAsPdf方法从给定的 URL 或本地文件路径创建 PDF。- 保存PDF:使用
SaveAs方法将结果 PDF 文档保存为 "url.pdf"。
IronPDF 的使用场景
IronPDF 是一个用于在 C# 中处理 PDF 的强大库。 它允许开发人员轻松生成、修改和操作 PDF 文档。 以下是 IronPDF 在 C# 应用程序中的一些常见使用场景:
1. 从 HTML 内容生成 PDF 报告
- 用例:从动态或静态 HTML 内容(例如发票、财务报表和产品目录)创建 PDF 报告。
- 示例:一种业务应用程序生成每周销售报告并通过电子邮件将其作为 PDF 附件发送。
2. 将网页转换为 PDF
- 用例:将网页或 URL 转换为 PDF 文档,以保存文章、整个网站或特定网页。
- 示例:一款允许用户将任何网页转换为 PDF 的工具。
3. 在 PDF 中嵌入图像、表格和图表
- 用例:将图像、图表和复杂表格嵌入到 PDF 文件中。
- 示例:一款企业应用程序生成个性化发票并嵌入公司徽标。
4. 将 HTML 表单转换为 PDFs
- 用例:将 HTML 表单转换为可填写或不可填写的 PDF 表单。
- 示例:一款表单管理应用程序允许用户在线填写表单并下载或通过电子邮件发送表单为 PDF。
5. 编辑和修改现有 PDFs
- 用例:在现有 PDF 中添加、删除或修改元素。
- 示例:一个文档管理系统对 PDF 添加水印以防止未经授权的分发。
6. 合并多个 PDFs 为一个
- 用例:将多个 PDF 合并为一个文档。
- 示例:一款为法律事务所提供的工具,将分开的页面合并为一个 PDF 以便存储和检索。
7. 从 PDFs 中提取文本和数据
- 用例:从现有的 PDF 文件中提取文本、表格和其他数据。
- 示例:一种 OCR 应用程序可从扫描表单中提取数据。
8. 使用模板创建pdfs
- 用例:使用预定义的模板生成一致且品牌化的 PDF 文档。
- 示例:一个可以生成可下载的 PDF 证书的网络服务。
9. PDF文档的数字签名
- 用例:为 PDF 添加数字签名以进行安全的文档签署。
- 示例:一种解决方案,合同可以通过电子方式进行签署。
10. 自动生成 PDF 文档
- 用例:自动生成大量的 PDF 文档。
- 示例:一个生成月度 PDF 发票的网络应用程序。
11. 创建交互式 PDFs
- 用例:创建具有交互元素的 PDF。
- 示例:一个表单提交应用程序。
12. 将 PDF 转换为其他格式
- 用例:将 PDF 转换为其他格式,如 HTML 或 Word。
- 示例:一种文档转换工具。
13. 拆分 PDF 文档
- 用例:将大型 PDF 拆分为较小的文件。
- 示例:一种扫描应用程序将文档拆分为单独的 PDF。
14. 密码保护 PDF
- 用例:加密和密码保护 PDF 文件。
- 示例:一款财务报告应用程序。
许可证信息(提供试用)
IronPDF 提供免费试用。 将其放置在使用库之前,如下所示:
IronPdf.License.LicenseKey = "your key";IronPdf.License.LicenseKey = "your key";IronPdf.License.LicenseKey = "your key"结论
IronPDF,.NET PDF库,使 在 C# 中生成 PDF 变得简单且强大。 无论您是生成发票、报告或其他类型的文档,IronPDF 都提供各种强大的功能,如 HTML 到 PDF 转换,自定义页眉和页脚,PDF 编辑,表单处理等。 它提供了与 PDF 工作的无缝方式。
通过 IronPDF,您可以在 C# 中轻松创建 高质量的PDF,让您专注于为用户提供良好的功能,而不是担心文档格式化的复杂性。 无论是处理动态网页内容还是创建静态报告,IronPDF 都是您PDF需求的可靠解决方案。