跳至页脚内容
使用IRONPDF
如何在 .NET 6 中生成 PDF 文件

如何将多页扫描成一个PDF文件

在C#中创建PDF是现代.NET开发人员的基本技能,无论你是构建财务报告、生成医疗文件,还是制作电子商务收据。使用合适的.NET PDF库,你可以借助几行代码将HTML内容转换为专业的PDF文档,全面掌控文档的结构和外观。

IronPDF无疑是最简单、最实用的.NET PDF创建库,学习曲线极其平缓,几分钟内就能生成PDF,而不是几个小时。 本综合指南将向你展示如何使用IronPDF在C#中创建PDF文档——一个强大的C# PDF生成器,能够生成像素完美的结果,并支持包括2025年11月即将发布的.NET 10在内的每个现代.NET平台。虽然本教程涵盖了从最简单的用例到最先进的PDF生成场景的内容,但不要害怕——从头开始逐步推进。 你将学习多种方法来生成PDF,从简单的HTML字符串到复杂的多页报告,还包括如何解决常见问题和优化各种PDF生成任务的性能。

您将学到什么

1.快速入门:2 分钟内创建您的第一个 PDF 文件 -如何使用 C# 创建 PDF:快速概述 2.为什么开发人员需要使用 C# 创建 PDF? -金融、医疗保健和电子商务应用案例 3.在 C# 项目中设置 IronPDF 方法一:Visual Studio 包管理器 方法二:软件包管理器控制台 方法三:.NET CLI(跨平台) -选择合适的软件包(精简版、Linux 版、MacOs 版) -验证您的安装 4.在 C# 中生成 PDF 的不同方法?

快速入门:在 C# 中创建您的第一个 PDF(不到 2 分钟)

想要立即生成 PDF 吗? 让我们创建一个简单但功能齐全的 PDF 文档,演示 .NET 中现代 PDF 生成的强大功能。 首先 通过 NuGet 包管理器安装 IronPDF——这个单一的软件包包含开始立即创建 PDF 所需的一切。 IronPDF在开发阶段是免费的,因此你可以在决定许可证之前尝试所有功能。

Install-Package IronPdf

现在让我们使用 C# 创建 PDF 内容:

using IronPdf;

// Instantiate the PDF generator - this is your gateway to PDF creation
var renderer = new ChromePdfRenderer();

// Create a PDF from HTML string - yes, it's really this simple!
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>PDF generated successfully!</p>");

// Save your newly created PDF document
pdf.SaveAs("my-first-pdf.pdf");

Console.WriteLine("PDF generated successfully!");
using IronPdf;

// Instantiate the PDF generator - this is your gateway to PDF creation
var renderer = new ChromePdfRenderer();

// Create a PDF from HTML string - yes, it's really this simple!
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>PDF generated successfully!</p>");

// Save your newly created PDF document
pdf.SaveAs("my-first-pdf.pdf");

Console.WriteLine("PDF generated successfully!");
Imports IronPdf

' Instantiate the PDF generator - this is your gateway to PDF creation
Private renderer = New ChromePdfRenderer()

' Create a PDF from HTML string - yes, it's really this simple!
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>PDF generated successfully!</p>")

' Save your newly created PDF document
pdf.SaveAs("my-first-pdf.pdf")

Console.WriteLine("PDF generated successfully!")
$vbLabelText   $csharpLabel

就是这样! 您刚刚 在 C# 中创建了第一个 PDF 文档。 无需学习复杂的 PDF API,无需安装服务器依赖项,无需掌握低级 PDF 命令。 只需 HTML 输入,PDF 输出 - 就是 PDF 生成的方式。 但这只是开始 - 让我们探索为何这种方法如此强大以及您如何创建更复杂的 PDF 文档。

如何在C♯中创建PDF:快速概述

要在C#中创建PDF,您可以使用第三方库,如IronPDF、QuestPDF或PDFSharp。这些库提供了各种功能,包括从头创建PDF、将HTML转换为PDF等。

以下是该过程的一般概述:

  1. 安装 PDF 库:在 Visual Studio 中使用 NuGet 包管理器安装合适的库。
  2. 创建新 PDF 文档:实例化 PDF 文档对象。
  3. 添加内容:向文档中添加页面、文本、图像和其他元素。
  4. 保存文档:指定文件路径并保存 PDF。

以下是使用 IronPDF 的示例:

using IronPdf;

public class Example
{
    public static void CreatePdf()
    {
        // Create a new PDF document
        var pdf = new ChromePdfRenderer();

        // Add some text
        string htmlContent = "<h1>Hello, PDF!</h1><p>This is a dynamically created PDF.</p>";

        // Render HTML to PDF
        var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent);

        // Save the PDF
        pdfDocument.SaveAs("MyDynamicPdf.pdf");
    }
}
using IronPdf;

public class Example
{
    public static void CreatePdf()
    {
        // Create a new PDF document
        var pdf = new ChromePdfRenderer();

        // Add some text
        string htmlContent = "<h1>Hello, PDF!</h1><p>This is a dynamically created PDF.</p>";

        // Render HTML to PDF
        var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent);

        // Save the PDF
        pdfDocument.SaveAs("MyDynamicPdf.pdf");
    }
}
Imports IronPdf

Public Class Example
	Public Shared Sub CreatePdf()
		' Create a new PDF document
		Dim pdf = New ChromePdfRenderer()

		' Add some text
		Dim htmlContent As String = "<h1>Hello, PDF!</h1><p>This is a dynamically created PDF.</p>"

		' Render HTML to PDF
		Dim pdfDocument = pdf.RenderHtmlAsPdf(htmlContent)

		' Save the PDF
		pdfDocument.SaveAs("MyDynamicPdf.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

现在,让我们更深入地探讨为什么开发人员需要创建 PDF 以及 IronPDF 如何让这个过程简单而强大。

开发人员为什么需要在 C# 中创建 PDF?

以编程方式创建 PDF 可以让 C# 打开自动化文档生成和简化业务流程的无限可能。 IronPDF已被全球超过1400万的开发者信赖来完成这些任务,因为它在.NET中构建PDF方面提供了无与伦比的可靠性和易用性。 在金融领域,开发人员使用 C# 创建 PDF 发票、报表和需要精准格式化和安全功能的合规报告。 医疗机构 生成患者记录、实验室结果和保险表单为PDF,以确保文档的完整性和符合HIPAA要求。 电子商务平台生成 PDF 收据,带有 QR 码的运输标签和门票(使用 IronQR 等工具)直接嵌入 PDF 中。 能够以编程方式操作PDF文档意味着您可以大规模地创建、修改和保护文档,而无需人工干预。

使用像 IronPDF 这样的现代 .NET PDF 库 的好处是它可以 无缝集成 到您组织现有工作流程中。 无论您是在转换业务用户的 Word 文档、从开发团队的 Markdown 文档 转换,还是创建从基于 Web 的报告中生成 PDF,IronPDF 都能处理一切。 这种组织灵活性是各公司选择在.NET中程序化PDF 生成的原因 - 它消除了手动创建文档、减少了错误、确保了所有生成的 PDF的一致性,并节省了员工的大量时间。您无需学习专有的 PDF 语法或手动定位每个元素,可以使用 HTML 和 CSS 来设计您的文档。 这种方法大大缩短了开发时间,确保您的生成的 PDF在所有方面保持一致的品牌形象。 无论您是创建单页发票还是复杂的多章节报告,原则保持不变 - 设计 HTML,用 C# 生成 PDF

在你的 C# 项目中设置 IronPDF

在深入创建 PDF 之前,让我们确保您的开发环境 恰当地配置 以获得最佳的PDF生成效果。 IronPDF 支持所有现代 .NET 版本,包括 .NET 8、.NET 9,并且已经符合将在 2025 年 11 月发布的 .NET 10 版(Iron Software 与 .NET Foundation 和 Microsoft 紧密合作,确保第一天兼容性)。 设置过程很简单,但了解选项有助于您为您的特定需求选择 最佳方法

安装方法

方法 1:Visual Studio 包管理器(初学者推荐)

开始使用 IronPDF 的最简单方式是通过 Visual Studio 的内置 NuGet 包管理器。 这个图形界面让您可以轻松浏览、安装和管理您的 PDF 生成依赖项:

1.在解决方案资源管理器中右键单击您的项目 2.选择 "管理 NuGet 软件包"。 3.点击 "浏览 "并搜索 "IronPDF"

  1. 选择Iron Software的IronPDF包
  2. 点击安装并接受许可协议
包管理器控制台

方法 2:软件包管理器控制台

对于喜欢命令行工具的开发者,包管理器控制台提供了快速安装 IronPDF 的方法:

Install-Package IronPdf

方法 3:.NET CLI(用于跨平台开发)

如果您正在 macOS、Linux 上工作,或者更喜欢 .NET CLI,请在您的项目目录下使用此命令:

dotnet add package IronPdf

选择合适的软件包

IronPDF 提供了适用于不同部署场景的各种 NuGet 包。 了解这些选项有助于您最小化部署规模优化性能

  • IronPdf: 包括您在Windows、macOS和Linux上所需的一切的标准包。 适合大多数应用。
  • IronPdf.Slim: 一个轻量级的基础包,运行时下载特定于平台的组件。适用于包大小重要的云部署
  • IronPdf.Linux: 专为Linux部署优化,预先打包了所有必需的依赖项。
  • IronPdf.MacOs: 针对macOS环境定制,支持原生苹果硅芯片。

对于 Azure、AWS 或 Docker 等云部署环境,建议使用 IronPdf.Slim 来减小容器大小。首次使用时,所需组件将自动下载。

验证您的安装

安装后,通过这个简单的测试来验证一切是否正常工作,该测试创建一个新文档:

using IronPdf;
using System.IO;

// Test your IronPDF installation
var renderer = new ChromePdfRenderer();
var testPdf = renderer.RenderHtmlAsPdf("<p>Installation test successful!</p>");
testPdf.SaveAs("test.pdf");

if (File.Exists("test.pdf"))
{
    Console.WriteLine("IronPDF installed and working correctly!");
}
using IronPdf;
using System.IO;

// Test your IronPDF installation
var renderer = new ChromePdfRenderer();
var testPdf = renderer.RenderHtmlAsPdf("<p>Installation test successful!</p>");
testPdf.SaveAs("test.pdf");

if (File.Exists("test.pdf"))
{
    Console.WriteLine("IronPDF installed and working correctly!");
}
Imports IronPdf
Imports System.IO

' Test your IronPDF installation
Private renderer = New ChromePdfRenderer()
Private testPdf = renderer.RenderHtmlAsPdf("<p>Installation test successful!</p>")
testPdf.SaveAs("test.pdf")

If File.Exists("test.pdf") Then
	Console.WriteLine("IronPDF installed and working correctly!")
End If
$vbLabelText   $csharpLabel

在 C# 中生成 PDF 的不同方法是什么?

IronPDF 提供了多种途径构建 PDF 文档,适用于不同的场景和需求。 理解这些方法可以帮助您为特定用例选择 最有效的方案,当您需要 在 .NET 中生成 PDF 时。 无论您是在从头开始创建 PDF,转换现有文件,还是捕获实时的在线内容,IronPDF 作为全面的 C# PDF 生成器 都能满足您的需求。 从 HTML 字符串创建 PDF 可以让您 完全控制 最终文档的内容和样式,当您需要 将 HTML 内容 转换为 PDF 格式时。

1. 从HTML字符串创建PDF(最灵活)

从 HTML 字符串创建 PDF 可以让您 完全控制 最终文档的内容和样式,当您需要 将 HTML 内容 转换为 PDF 格式时,这种方法最适合生成动态报告、发票或任何根据数据变动的文档。 您可以使用包括 flexbox 和 grid 布局在内的现代 HTML5 和 CSS3 特性将 HTML 内容转换为专业 PDF 文件。 动态转换 HTML 内容的能力使这是 C# 中 PDF 创建 的最通用方法: 能够动态转换 HTML 内容,使得这种方法成为C# 中创建 PDF 最通用的方法:

using IronPdf;
using System;
using System.Linq;

var renderer = new ChromePdfRenderer();

// Build dynamic content with data
var customerName = "Acme Corporation";
var orderDate = DateTime.Now;
var items = new[] { 
    new { Name = "Widget Pro", Price = 99.99m },
    new { Name = "Gadget Plus", Price = 149.99m }
};

// Create HTML with embedded data and modern CSS
var html = $@"
    <html>
    <head>
        <style>
            body {{font-family: 'Segoe UI', Arial, sans-serif; 
                margin: 40px;
                color: #333;}}
            .invoice-header {{display: flex;
                justify-content: space-between;
                border-bottom: 2px solid #0066cc;
                padding-bottom: 20px;}}
            .items-table {{width: 100%;
                margin-top: 30px;
                border-collapse: collapse;}}
            .items-table th {{background: #f0f0f0;
                padding: 10px;
                text-align: left;}}
        </style>
    </head>
    <body>
        <div class='invoice-header'>
            <div>
                <h1>Invoice</h1>
                <p>Customer: {customerName}</p>
            </div>
            <div>
                <p>Date: {orderDate:yyyy-MM-dd}</p>
                <p>Invoice #: INV-{orderDate:yyyyMMdd}-001</p>
            </div>
        </div>

        <table class='items-table'>
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>";

foreach (var item in items)
{
    html += $@"
                <tr>
                    <td>{item.Name}</td>
                    <td>${item.Price:F2}</td>
                </tr>";
}

html += @"
            </tbody>
        </table>
    </body>
    </html>";

// Generate the PDF document
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs($"invoice-{orderDate:yyyyMMdd}.pdf");
using IronPdf;
using System;
using System.Linq;

var renderer = new ChromePdfRenderer();

// Build dynamic content with data
var customerName = "Acme Corporation";
var orderDate = DateTime.Now;
var items = new[] { 
    new { Name = "Widget Pro", Price = 99.99m },
    new { Name = "Gadget Plus", Price = 149.99m }
};

// Create HTML with embedded data and modern CSS
var html = $@"
    <html>
    <head>
        <style>
            body {{font-family: 'Segoe UI', Arial, sans-serif; 
                margin: 40px;
                color: #333;}}
            .invoice-header {{display: flex;
                justify-content: space-between;
                border-bottom: 2px solid #0066cc;
                padding-bottom: 20px;}}
            .items-table {{width: 100%;
                margin-top: 30px;
                border-collapse: collapse;}}
            .items-table th {{background: #f0f0f0;
                padding: 10px;
                text-align: left;}}
        </style>
    </head>
    <body>
        <div class='invoice-header'>
            <div>
                <h1>Invoice</h1>
                <p>Customer: {customerName}</p>
            </div>
            <div>
                <p>Date: {orderDate:yyyy-MM-dd}</p>
                <p>Invoice #: INV-{orderDate:yyyyMMdd}-001</p>
            </div>
        </div>

        <table class='items-table'>
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>";

foreach (var item in items)
{
    html += $@"
                <tr>
                    <td>{item.Name}</td>
                    <td>${item.Price:F2}</td>
                </tr>";
}

html += @"
            </tbody>
        </table>
    </body>
    </html>";

// Generate the PDF document
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs($"invoice-{orderDate:yyyyMMdd}.pdf");
Imports IronPdf
Imports System
Imports System.Linq

Dim renderer = New ChromePdfRenderer()

' Build dynamic content with data
Dim customerName = "Acme Corporation"
Dim orderDate = DateTime.Now
Dim items = New() {
    New With {.Name = "Widget Pro", .Price = 99.99D},
    New With {.Name = "Gadget Plus", .Price = 149.99D}
}

' Create HTML with embedded data and modern CSS
Dim html = $"
    <html>
    <head>
        <style>
            body {{font-family: 'Segoe UI', Arial, sans-serif; 
                margin: 40px;
                color: #333;}}
            .invoice-header {{display: flex;
                justify-content: space-between;
                border-bottom: 2px solid #0066cc;
                padding-bottom: 20px;}}
            .items-table {{width: 100%;
                margin-top: 30px;
                border-collapse: collapse;}}
            .items-table th {{background: #f0f0f0;
                padding: 10px;
                text-align: left;}}
        </style>
    </head>
    <body>
        <div class='invoice-header'>
            <div>
                <h1>Invoice</h1>
                <p>Customer: {customerName}</p>
            </div>
            <div>
                <p>Date: {orderDate:yyyy-MM-dd}</p>
                <p>Invoice #: INV-{orderDate:yyyyMMdd}-001</p>
            </div>
        </div>

        <table class='items-table'>
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>"

For Each item In items
    html += $"
                <tr>
                    <td>{item.Name}</td>
                    <td>${item.Price:F2}</td>
                </tr>"
Next

html += "
            </tbody>
        </table>
    </body>
    </html>"

' Generate the PDF document
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs($"invoice-{orderDate:yyyyMMdd}.pdf")
$vbLabelText   $csharpLabel

2. 从URL生成PDF(网页捕获)

IronPDF 的 URL 到 PDF 转换使用了 真实的 Chromium 引擎,确保复杂、JavaScript 重度的站点正确渲染。 IronPDF的URL到PDF转换使用真正的Chromium引擎,确保复杂的JavaScript重型网站正确呈现。

URL 转 PDF 示例
using IronPdf;

var renderer = new ChromePdfRenderer();

// Configure rendering options for optimal capture
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;

// Wait for JavaScript to fully load (important for SPAs)
renderer.RenderingOptions.RenderDelay = 2000; // 2 seconds

// Enable JavaScript execution
renderer.RenderingOptions.EnableJavaScript = true;

// Capture a web page as PDF
var pdf = renderer.RenderUrlAsPdf("https://example.com/dashboard");
pdf.SaveAs("dashboard-capture.pdf");

// For authenticated pages, you can set cookies
var cookieManager = renderer.RenderingOptions.CustomCookies;
cookieManager["session_id"] = "your-session-token";

// Capture authenticated content
var securePdf = renderer.RenderUrlAsPdf("https://app.example.com/private/report");
securePdf.SaveAs("private-report.pdf");
using IronPdf;

var renderer = new ChromePdfRenderer();

// Configure rendering options for optimal capture
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;

// Wait for JavaScript to fully load (important for SPAs)
renderer.RenderingOptions.RenderDelay = 2000; // 2 seconds

// Enable JavaScript execution
renderer.RenderingOptions.EnableJavaScript = true;

// Capture a web page as PDF
var pdf = renderer.RenderUrlAsPdf("https://example.com/dashboard");
pdf.SaveAs("dashboard-capture.pdf");

// For authenticated pages, you can set cookies
var cookieManager = renderer.RenderingOptions.CustomCookies;
cookieManager["session_id"] = "your-session-token";

// Capture authenticated content
var securePdf = renderer.RenderUrlAsPdf("https://app.example.com/private/report");
securePdf.SaveAs("private-report.pdf");
Imports IronPdf

Private renderer = New ChromePdfRenderer()

' Configure rendering options for optimal capture
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.MarginTop = 25
renderer.RenderingOptions.MarginBottom = 25

' Wait for JavaScript to fully load (important for SPAs)
renderer.RenderingOptions.RenderDelay = 2000 ' 2 seconds

' Enable JavaScript execution
renderer.RenderingOptions.EnableJavaScript = True

' Capture a web page as PDF
Dim pdf = renderer.RenderUrlAsPdf("https://example.com/dashboard")
pdf.SaveAs("dashboard-capture.pdf")

' For authenticated pages, you can set cookies
Dim cookieManager = renderer.RenderingOptions.CustomCookies
cookieManager("session_id") = "your-session-token"

' Capture authenticated content
Dim securePdf = renderer.RenderUrlAsPdf("https://app.example.com/private/report")
securePdf.SaveAs("private-report.pdf")
$vbLabelText   $csharpLabel

3. 从HTML文件创建PDF(基于模板的生成)

通过将 HTML 模板存储为文件,您可以使设计与逻辑之间 清洁分隔。 通过将HTML模板存储为文件,您可以实现设计与逻辑之间的清晰分离

HTML 文件到 PDF
using IronPdf;
using System.IO;
using System;

var renderer = new ChromePdfRenderer();

// Basic file conversion
var pdf = renderer.RenderHtmlFileAsPdf("Templates/certificate-template.html");
pdf.SaveAs("certificate.pdf");

// Advanced: Using templates with asset directories
// Perfect when your HTML references images, CSS, or JavaScript files
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "Templates", "Assets");
var pdfWithAssets = renderer.RenderHtmlFileAsPdf(
    "Templates/report-template.html", 
    basePath  // IronPDF will resolve relative paths from here
);

// Even better: Template with placeholders
var templateHtml = File.ReadAllText("Templates/contract-template.html");
templateHtml = templateHtml
    .Replace("{{ClientName}}", "Tech Innovations Inc.")
    .Replace("{{ContractDate}}", DateTime.Now.ToString("MMMM dd, yyyy"))
    .Replace("{{ContractValue}}", "$50,000");

var contractPdf = renderer.RenderHtmlAsPdf(templateHtml);
contractPdf.SaveAs("contract-final.pdf");
using IronPdf;
using System.IO;
using System;

var renderer = new ChromePdfRenderer();

// Basic file conversion
var pdf = renderer.RenderHtmlFileAsPdf("Templates/certificate-template.html");
pdf.SaveAs("certificate.pdf");

// Advanced: Using templates with asset directories
// Perfect when your HTML references images, CSS, or JavaScript files
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "Templates", "Assets");
var pdfWithAssets = renderer.RenderHtmlFileAsPdf(
    "Templates/report-template.html", 
    basePath  // IronPDF will resolve relative paths from here
);

// Even better: Template with placeholders
var templateHtml = File.ReadAllText("Templates/contract-template.html");
templateHtml = templateHtml
    .Replace("{{ClientName}}", "Tech Innovations Inc.")
    .Replace("{{ContractDate}}", DateTime.Now.ToString("MMMM dd, yyyy"))
    .Replace("{{ContractValue}}", "$50,000");

var contractPdf = renderer.RenderHtmlAsPdf(templateHtml);
contractPdf.SaveAs("contract-final.pdf");
Imports IronPdf
Imports System.IO
Imports System

Private renderer = New ChromePdfRenderer()

' Basic file conversion
Private pdf = renderer.RenderHtmlFileAsPdf("Templates/certificate-template.html")
pdf.SaveAs("certificate.pdf")

' Advanced: Using templates with asset directories
' Perfect when your HTML references images, CSS, or JavaScript files
Dim basePath = Path.Combine(Directory.GetCurrentDirectory(), "Templates", "Assets")
Dim pdfWithAssets = renderer.RenderHtmlFileAsPdf("Templates/report-template.html", basePath)

' Even better: Template with placeholders
Dim templateHtml = File.ReadAllText("Templates/contract-template.html")
templateHtml = templateHtml.Replace("{{ClientName}}", "Tech Innovations Inc.").Replace("{{ContractDate}}", DateTime.Now.ToString("MMMM dd, yyyy")).Replace("{{ContractValue}}", "$50,000")

Dim contractPdf = renderer.RenderHtmlAsPdf(templateHtml)
contractPdf.SaveAs("contract-final.pdf")
$vbLabelText   $csharpLabel

4. 将Markdown转换为PDF

IronPDF 使其易于将Markdown 内容直接转换为 PDF,在创建专业外观的文件的同时保留格式。 IronPDF使得将Markdown内容直接转换为PDF变得容易,同时保持格式,创建专业外观的文档。 这个特性对于以Markdown格式维护文档的组织尤为有价值-开发人员可以使用他们首选的格式编写文档,系统可以自动生成PDF以分发给客户或利益相关者。

using IronPdf;

var renderer = new ChromePdfRenderer();

// Convert Markdown string to PDF
string markdownContent = @"
# Project Documentation

## Overview
This project demonstrates **PDF generation** from _Markdown_ content.

### Features
- Easy conversion
- Preserves formatting
- Supports lists and tables

| Feature | Status |
|---------|--------|
| Markdown Support | |
| Table Rendering | |
| Code Blocks | |

```csharp
// Code blocks are preserved
var pdf = RenderMarkdownAsPdf(markdown);
\`\`\`
";

// Render Markdown as PDF
var pdfFromMarkdown = renderer.RenderMarkdownStringAsPdf(markdownContent);
pdfFromMarkdown.SaveAs("documentation.pdf");

// Convert Markdown file to PDF
var pdfFromFile = renderer.RenderMarkdownFileAsPdf("README.md");
pdfFromFile.SaveAs("readme-pdf.pdf");
using IronPdf;

var renderer = new ChromePdfRenderer();

// Convert Markdown string to PDF
string markdownContent = @"
# Project Documentation

## Overview
This project demonstrates **PDF generation** from _Markdown_ content.

### Features
- Easy conversion
- Preserves formatting
- Supports lists and tables

| Feature | Status |
|---------|--------|
| Markdown Support | |
| Table Rendering | |
| Code Blocks | |

```csharp
// Code blocks are preserved
var pdf = RenderMarkdownAsPdf(markdown);
\`\`\`
";

// Render Markdown as PDF
var pdfFromMarkdown = renderer.RenderMarkdownStringAsPdf(markdownContent);
pdfFromMarkdown.SaveAs("documentation.pdf");

// Convert Markdown file to PDF
var pdfFromFile = renderer.RenderMarkdownFileAsPdf("README.md");
pdfFromFile.SaveAs("readme-pdf.pdf");
Imports IronPdf

Dim renderer As New ChromePdfRenderer()

' Convert Markdown string to PDF
Dim markdownContent As String = "
# Project Documentation

## Overview
This project demonstrates **PDF generation** from _Markdown_ content.

### Features
- Easy conversion
- Preserves formatting
- Supports lists and tables

| Feature | Status |
|---------|--------|
| Markdown Support | |
| Table Rendering | |
| Code Blocks | |

```csharp
// Code blocks are preserved
var pdf = RenderMarkdownAsPdf(markdown);
\`\`\`
"

' Render Markdown as PDF
Dim pdfFromMarkdown = renderer.RenderMarkdownStringAsPdf(markdownContent)
pdfFromMarkdown.SaveAs("documentation.pdf")

' Convert Markdown file to PDF
Dim pdfFromFile = renderer.RenderMarkdownFileAsPdf("README.md")
pdfFromFile.SaveAs("readme-pdf.pdf")
$vbLabelText   $csharpLabel

Markdown到PDF的转换对于使用版本控制系统如Git的组织是特别有用的。 您的整个文档工作流程可以自动化 - 开发人员更新Markdown文件,CI/CD管道自动生成PDF文档,利益相关者在没有任何人工干预的情况下接收到专业格式的文档。 ### 5. 将 Word 文档(DOCX)转换为 PDF

许多企业已有的 Word 文档需要转换为 PDF 以便分发或归档。

许多企业有现有的Word文档需要转换为PDF以用于分发或归档。 IronPDF提供无缝的DOCX到PDF转换,保持格式、图像,甚至是邮件合并等复杂功能。 DOCX 到 PDF 转换 功能弥合了偏好 Word 的业务用户与需要安全、不可编辑 PDF 文件之间的差距。 DOCX到PDF转换功能弥补了商务用户偏好Word与对安全、不可编辑的PDF文档的需求之间的空白。

using IronPDF;
using System.Collections.Generic;

// Simple DOCX to PDF conversion
var docxRenderer = new DocxToPdfRenderer();
var pdfFromDocx = docxRenderer.RenderDocxAsPdf("proposal.docx");
pdfFromDocx.SaveAs("proposal.pdf");

// Advanced: Mail merge functionality for mass document generation
var recipients = new List<Dictionary<string, string>>
{
    new() { ["Name"] = "John Smith", ["Company"] = "Tech Corp", ["Date"] = "March 15, 2024" },
    new() { ["Name"] = "Jane Doe", ["Company"] = "Innovation Inc", ["Date"] = "March 15, 2024" }
};

// Configure mail merge options
var options = new DocxPdfRenderOptions
{
    MailMergeDataSource = recipients,
    MailMergePrintAllInOnePdfDocument = false // Creates separate PDFs
};

// Generate personalized PDFs from template
foreach (var recipient in recipients)
{
    var personalizedPdf = docxRenderer.RenderDocxAsPdf("letter-template.docx", options);
    personalizedPdf.SaveAs($"letter-{recipient["Name"].Replace(" ", "-")}.pdf");
}
using IronPDF;
using System.Collections.Generic;

// Simple DOCX to PDF conversion
var docxRenderer = new DocxToPdfRenderer();
var pdfFromDocx = docxRenderer.RenderDocxAsPdf("proposal.docx");
pdfFromDocx.SaveAs("proposal.pdf");

// Advanced: Mail merge functionality for mass document generation
var recipients = new List<Dictionary<string, string>>
{
    new() { ["Name"] = "John Smith", ["Company"] = "Tech Corp", ["Date"] = "March 15, 2024" },
    new() { ["Name"] = "Jane Doe", ["Company"] = "Innovation Inc", ["Date"] = "March 15, 2024" }
};

// Configure mail merge options
var options = new DocxPdfRenderOptions
{
    MailMergeDataSource = recipients,
    MailMergePrintAllInOnePdfDocument = false // Creates separate PDFs
};

// Generate personalized PDFs from template
foreach (var recipient in recipients)
{
    var personalizedPdf = docxRenderer.RenderDocxAsPdf("letter-template.docx", options);
    personalizedPdf.SaveAs($"letter-{recipient["Name"].Replace(" ", "-")}.pdf");
}
Imports IronPDF
Imports System.Collections.Generic

' Simple DOCX to PDF conversion
Dim docxRenderer As New DocxToPdfRenderer()
Dim pdfFromDocx = docxRenderer.RenderDocxAsPdf("proposal.docx")
pdfFromDocx.SaveAs("proposal.pdf")

' Advanced: Mail merge functionality for mass document generation
Dim recipients As New List(Of Dictionary(Of String, String)) From {
    New Dictionary(Of String, String) From {{"Name", "John Smith"}, {"Company", "Tech Corp"}, {"Date", "March 15, 2024"}},
    New Dictionary(Of String, String) From {{"Name", "Jane Doe"}, {"Company", "Innovation Inc"}, {"Date", "March 15, 2024"}}
}

' Configure mail merge options
Dim options As New DocxPdfRenderOptions With {
    .MailMergeDataSource = recipients,
    .MailMergePrintAllInOnePdfDocument = False ' Creates separate PDFs
}

' Generate personalized PDFs from template
For Each recipient In recipients
    Dim personalizedPdf = docxRenderer.RenderDocxAsPdf("letter-template.docx", options)
    personalizedPdf.SaveAs($"letter-{recipient("Name").Replace(" ", "-")}.pdf")
Next
$vbLabelText   $csharpLabel

这个DOCX转换功能对于企业内部自动化文档工作流程是不可或缺的。 考虑一个使用Word创建提案的销售团队 - 借助IronPDF,这些提案可以自动转换为带有水印、安全设置和数字签名的PDF,所有这些都可以以编程方式应用。 邮件合并功能使得大规模生成个性化PDF文档成为可能-非常适合创建成千上万封定制的信件、证书或合同,而无需人工干预。 ### 6. 将图像转换为 PDF

6. 将图像转换为PDF

IronPDF 支持所有主要图像格式并提供控制布局和质量的选项: ### 7. 从 ASP.NET 页面生成 PDF

using IronPDF;
using IronPdf.Imaging; // `Install-Package IronPdf`
using System.IO;

var renderer = new ChromePdfRenderer();

// Convert single image to PDF
var imagePath = "product-photo.jpg";
var imageHtml = @"
    <html>
    <body style='margin: 0; padding: 0;'>
        <img src='{imagePath}' style='width: 100%; height: auto;' />
    </body>
    </html>";

var imagePdf = renderer.RenderHtmlAsPdf(imageHtml, Path.GetDirectoryName(imagePath));
imagePdf.SaveAs("product-catalog-page.pdf");

// Create multi-page PDF from multiple images
var imageFiles = Directory.GetFiles("ProductImages", "*.jpg");
var catalogHtml = "<html><body style='margin: 0;'>";

foreach (var image in imageFiles)
{
    catalogHtml += @"
        <div style='page-break-after: always;'>
            <img src='{Path.GetFileName(image)}' style='width: 100%; height: auto;' />
            <p style='text-align: center;'>{Path.GetFileNameWithoutExtension(image)}</p>
        </div>";
}

catalogHtml += "</body></html>";

var catalogPdf = renderer.RenderHtmlAsPdf(catalogHtml, "ProductImages");
catalogPdf.SaveAs("product-catalog.pdf");
using IronPDF;
using IronPdf.Imaging; // `Install-Package IronPdf`
using System.IO;

var renderer = new ChromePdfRenderer();

// Convert single image to PDF
var imagePath = "product-photo.jpg";
var imageHtml = @"
    <html>
    <body style='margin: 0; padding: 0;'>
        <img src='{imagePath}' style='width: 100%; height: auto;' />
    </body>
    </html>";

var imagePdf = renderer.RenderHtmlAsPdf(imageHtml, Path.GetDirectoryName(imagePath));
imagePdf.SaveAs("product-catalog-page.pdf");

// Create multi-page PDF from multiple images
var imageFiles = Directory.GetFiles("ProductImages", "*.jpg");
var catalogHtml = "<html><body style='margin: 0;'>";

foreach (var image in imageFiles)
{
    catalogHtml += @"
        <div style='page-break-after: always;'>
            <img src='{Path.GetFileName(image)}' style='width: 100%; height: auto;' />
            <p style='text-align: center;'>{Path.GetFileNameWithoutExtension(image)}</p>
        </div>";
}

catalogHtml += "</body></html>";

var catalogPdf = renderer.RenderHtmlAsPdf(catalogHtml, "ProductImages");
catalogPdf.SaveAs("product-catalog.pdf");
Imports IronPDF
Imports IronPdf.Imaging ' `Install-Package IronPdf`
Imports System.IO

Dim renderer As New ChromePdfRenderer()

' Convert single image to PDF
Dim imagePath As String = "product-photo.jpg"
Dim imageHtml As String = "
    <html>
    <body style='margin: 0; padding: 0;'>
        <img src='" & imagePath & "' style='width: 100%; height: auto;' />
    </body>
    </html>"

Dim imagePdf = renderer.RenderHtmlAsPdf(imageHtml, Path.GetDirectoryName(imagePath))
imagePdf.SaveAs("product-catalog-page.pdf")

' Create multi-page PDF from multiple images
Dim imageFiles = Directory.GetFiles("ProductImages", "*.jpg")
Dim catalogHtml As String = "<html><body style='margin: 0;'>"

For Each image In imageFiles
    catalogHtml &= "
        <div style='page-break-after: always;'>
            <img src='" & Path.GetFileName(image) & "' style='width: 100%; height: auto;' />
            <p style='text-align: center;'>" & Path.GetFileNameWithoutExtension(image) & "</p>
        </div>"
Next

catalogHtml &= "</body></html>"

Dim catalogPdf = renderer.RenderHtmlAsPdf(catalogHtml, "ProductImages")
catalogPdf.SaveAs("product-catalog.pdf")
$vbLabelText   $csharpLabel

7. 从ASP.NET页面生成PDF

对于Web应用程序而言,从现有视图生成PDF提供了一种无缝方式来创建可下载的网页内容版本。 这种集成功能对于需要从其Web应用程序创建PDF的组织至关重要 - 无论是生成报表的客户门户、生成报告的管理员仪表板,还是创建证书的电子学习平台。 ## 我如何使我的 PDF 看起来专业?

// Namespace: Microsoft.AspNetCore.Mvc
using Microsoft.AspNetCore.Mvc;
// Namespace: IronPDF
using IronPDF;
// Namespace: System.Threading.Tasks
using System.Threading.Tasks;
// Namespace: System.IO
using System.IO;
// Namespace: System
using System;

// ASP.NET Core MVC Controller
public class ReportController : Controller
{
    private readonly ChromePdfRenderer _pdfRenderer;

    public ReportController()
    {
        _pdfRenderer = new ChromePdfRenderer();
    }

    public async Task<IActionResult> DownloadReport(int reportId)
    {
        // Get your report data
        var reportData = await GetReportData(reportId);

        // Render view to HTML string
        var html = await RenderViewToStringAsync("Reports/MonthlyReport", reportData);

        // Convert HTML content to PDF
        var pdf = _pdfRenderer.RenderHtmlAsPdf(html);

        // Return as file download
        return File(
            pdf.BinaryData,
            "application/pdf",
            $"report-{reportId}-{DateTime.Now:yyyy-MM}.pdf"
        );
    }

    private async Task<string> RenderViewToStringAsync(string viewName, object model)
    {
        ViewData.Model = model;
        using var sw = new StringWriter();
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(
            ControllerContext,
            viewResult.View,
            ViewData,
            TempData,
            sw,
            new HtmlHelperOptions()
        );
        viewResult.View.Render(viewContext, sw);
        return sw.GetStringBuilder().ToString();
    }
}
// Namespace: Microsoft.AspNetCore.Mvc
using Microsoft.AspNetCore.Mvc;
// Namespace: IronPDF
using IronPDF;
// Namespace: System.Threading.Tasks
using System.Threading.Tasks;
// Namespace: System.IO
using System.IO;
// Namespace: System
using System;

// ASP.NET Core MVC Controller
public class ReportController : Controller
{
    private readonly ChromePdfRenderer _pdfRenderer;

    public ReportController()
    {
        _pdfRenderer = new ChromePdfRenderer();
    }

    public async Task<IActionResult> DownloadReport(int reportId)
    {
        // Get your report data
        var reportData = await GetReportData(reportId);

        // Render view to HTML string
        var html = await RenderViewToStringAsync("Reports/MonthlyReport", reportData);

        // Convert HTML content to PDF
        var pdf = _pdfRenderer.RenderHtmlAsPdf(html);

        // Return as file download
        return File(
            pdf.BinaryData,
            "application/pdf",
            $"report-{reportId}-{DateTime.Now:yyyy-MM}.pdf"
        );
    }

    private async Task<string> RenderViewToStringAsync(string viewName, object model)
    {
        ViewData.Model = model;
        using var sw = new StringWriter();
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(
            ControllerContext,
            viewResult.View,
            ViewData,
            TempData,
            sw,
            new HtmlHelperOptions()
        );
        viewResult.View.Render(viewContext, sw);
        return sw.GetStringBuilder().ToString();
    }
}
Imports Microsoft.AspNetCore.Mvc
Imports IronPDF
Imports System.Threading.Tasks
Imports System.IO
Imports System

Public Class ReportController
    Inherits Controller

    Private ReadOnly _pdfRenderer As ChromePdfRenderer

    Public Sub New()
        _pdfRenderer = New ChromePdfRenderer()
    End Sub

    Public Async Function DownloadReport(reportId As Integer) As Task(Of IActionResult)
        ' Get your report data
        Dim reportData = Await GetReportData(reportId)

        ' Render view to HTML string
        Dim html = Await RenderViewToStringAsync("Reports/MonthlyReport", reportData)

        ' Convert HTML content to PDF
        Dim pdf = _pdfRenderer.RenderHtmlAsPdf(html)

        ' Return as file download
        Return File(pdf.BinaryData, "application/pdf", $"report-{reportId}-{DateTime.Now:yyyy-MM}.pdf")
    End Function

    Private Async Function RenderViewToStringAsync(viewName As String, model As Object) As Task(Of String)
        ViewData.Model = model
        Using sw As New StringWriter()
            Dim viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName)
            Dim viewContext = New ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw, New HtmlHelperOptions())
            viewResult.View.Render(viewContext, sw)
            Return sw.GetStringBuilder().ToString()
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

如何让我的PDF看起来更专业?

创建PDF是一回事 - 让它们看起来专业是使您的应用程序在.NET中生成PDF时脱颖而出的关键。 借助 IronPDF 的全面样式选项和 先进的 PDF 功能,您可以使用这个强大的 C# PDF 生成器 创建完美匹配您的公司形象的文件。 HTML 到 PDF 转换 功能可以确保您的样式化文档在作为 PDF 生成时保持其视觉吸引力。 HTML到PDF转换功能确保您的样式文档在生成PDF时保持视觉吸引力。 让我们探索将基本PDF转变为令人印象深刻的专业文档的功能,以便给客户和利益相关者留下深刻印象。

专业文件需要提供上下文和导航的一致的页眉和页脚

专业文档需要一致的页眉和页脚,提供上下文和导航。 IronPDF提供简单的文本基础和复杂的HTML基础的页眉和页脚选项。 您可以在生成的 PDF 中包含动态内容,如页码、日期和文档标题,确保每个生成的 PDF维护专业标准: 这些页眉和页脚选项使组织能够在所有生成的 PDF中保持品牌一致性。

using IronPDF;
using System;

var renderer = new ChromePdfRenderer();

// Simple text header and footer with page numbers
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
    Text = "Confidential Report - {date}",
    DrawDividerLine = true,
    Font = "Arial",
    FontSize = 12
};

renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
    Text = "Page {page} of {total-pages}",
    DrawDividerLine = true,
    Font = "Arial",
    FontSize = 10,
    CenterText = true
};

// HTML headers for complex layouts with logos
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    Html = @"
        <div style='display: flex; justify-content: space-between; align-items: center; padding: 10px 40px;'>
            <img src='logo.png' style='height: 40px;' />
            <div style='text-align: center;'>
                <h2 style='margin: 0; color: #333;'>Annual Report 2024</h2>
                <p style='margin: 0; font-size: 12px; color: #666;'>Confidential</p>
            </div>
            <div style='text-align: right; font-size: 11px; color: #666;'>
                Generated: {date}<br/>
                Department: Finance
            </div>
        </div>",
    Height = 80,
    LoadStylesAndCSSFromMainHtmlDocument = true
};

// Create your PDF with professional headers/footers
var html = @"<h1>Financial Overview</h1><p>Report content here...</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("professional-report.pdf");
using IronPDF;
using System;

var renderer = new ChromePdfRenderer();

// Simple text header and footer with page numbers
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
    Text = "Confidential Report - {date}",
    DrawDividerLine = true,
    Font = "Arial",
    FontSize = 12
};

renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
    Text = "Page {page} of {total-pages}",
    DrawDividerLine = true,
    Font = "Arial",
    FontSize = 10,
    CenterText = true
};

// HTML headers for complex layouts with logos
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    Html = @"
        <div style='display: flex; justify-content: space-between; align-items: center; padding: 10px 40px;'>
            <img src='logo.png' style='height: 40px;' />
            <div style='text-align: center;'>
                <h2 style='margin: 0; color: #333;'>Annual Report 2024</h2>
                <p style='margin: 0; font-size: 12px; color: #666;'>Confidential</p>
            </div>
            <div style='text-align: right; font-size: 11px; color: #666;'>
                Generated: {date}<br/>
                Department: Finance
            </div>
        </div>",
    Height = 80,
    LoadStylesAndCSSFromMainHtmlDocument = true
};

// Create your PDF with professional headers/footers
var html = @"<h1>Financial Overview</h1><p>Report content here...</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("professional-report.pdf");
Imports IronPDF
Imports System

Dim renderer As New ChromePdfRenderer()

' Simple text header and footer with page numbers
renderer.RenderingOptions.TextHeader = New TextHeaderFooter With {
    .Text = "Confidential Report - {date}",
    .DrawDividerLine = True,
    .Font = "Arial",
    .FontSize = 12
}

renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {
    .Text = "Page {page} of {total-pages}",
    .DrawDividerLine = True,
    .Font = "Arial",
    .FontSize = 10,
    .CenterText = True
}

' HTML headers for complex layouts with logos
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {
    .Html = "
        <div style='display: flex; justify-content: space-between; align-items: center; padding: 10px 40px;'>
            <img src='logo.png' style='height: 40px;' />
            <div style='text-align: center;'>
                <h2 style='margin: 0; color: #333;'>Annual Report 2024</h2>
                <p style='margin: 0; font-size: 12px; color: #666;'>Confidential</p>
            </div>
            <div style='text-align: right; font-size: 11px; color: #666;'>
                Generated: {date}<br/>
                Department: Finance
            </div>
        </div>",
    .Height = 80,
    .LoadStylesAndCSSFromMainHtmlDocument = True
}

' Create your PDF with professional headers/footers
Dim html As String = "<h1>Financial Overview</h1><p>Report content here...</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("professional-report.pdf")
$vbLabelText   $csharpLabel

这些页眉和页脚选项使组织能够在所有生成的PDF中保持品牌一致性。 ### 高级页面设置和布局控制

对页面布局的控制对于创建打印正确并在所有设备上看起來专业的文档至关重要。

IronPDF 提供广泛的页面设置选项,包括自定义尺寸、方向和边距: IronPDF提供广泛的页面设置选项,包括自定义大小、方向和边距:

var renderer = new ChromePdfRenderer();

// Configure page setup for professional printing
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;

// Set margins in millimeters for precise control
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;

// Enable background colors and images (important for branding)
renderer.RenderingOptions.PrintHtmlBackgrounds = true;

// Use screen media type for vibrant colors
renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Screen;

// Custom page size for special documents
renderer.RenderingOptions.SetCustomPaperSizeinMilimeters(210, 297); // A4

// Enable high-quality rendering
renderer.RenderingOptions.RenderQuality = 100; // 0-100 scale
var renderer = new ChromePdfRenderer();

// Configure page setup for professional printing
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;

// Set margins in millimeters for precise control
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;

// Enable background colors and images (important for branding)
renderer.RenderingOptions.PrintHtmlBackgrounds = true;

// Use screen media type for vibrant colors
renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Screen;

// Custom page size for special documents
renderer.RenderingOptions.SetCustomPaperSizeinMilimeters(210, 297); // A4

// Enable high-quality rendering
renderer.RenderingOptions.RenderQuality = 100; // 0-100 scale
Dim renderer = New ChromePdfRenderer()

' Configure page setup for professional printing
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait

' Set margins in millimeters for precise control
renderer.RenderingOptions.MarginTop = 25
renderer.RenderingOptions.MarginBottom = 25
renderer.RenderingOptions.MarginLeft = 20
renderer.RenderingOptions.MarginRight = 20

' Enable background colors and images (important for branding)
renderer.RenderingOptions.PrintHtmlBackgrounds = True

' Use screen media type for vibrant colors
renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Screen

' Custom page size for special documents
renderer.RenderingOptions.SetCustomPaperSizeinMilimeters(210, 297) ' A4

' Enable high-quality rendering
renderer.RenderingOptions.RenderQuality = 100 ' 0-100 scale
$vbLabelText   $csharpLabel

处理字体和排版

排版在文档专业性中扮演着关键角色。 IronPDF支持网络字体、自定义字体和高级排版功能:

var renderer = new ChromePdfRenderer();

// HTML with custom fonts and typography
var html = @"
    <html>
    <head>
        <link href='https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap' rel='stylesheet'>
        <style>
            @font-face {
                font-family: 'CustomBrand';
                src: url('BrandFont.ttf') format('truetype');
            }

            body {
                font-family: 'Roboto', Arial, sans-serif;
                font-size: 11pt;
                line-height: 1.6;
                color: #333;
            }

            h1 {
                font-family: 'CustomBrand', Georgia, serif;
                font-size: 28pt;
                color: #0066cc;
                letter-spacing: -0.5px;
            }

            .quote {
                font-style: italic;
                font-size: 14pt;
                color: #666;
                border-left: 4px solid #0066cc;
                padding-left: 20px;
                margin: 20px 0;
            }
        </style>
    </head>
    <body>
        <h1>Professional Document</h1>
        <p>This document demonstrates professional typography.</p>
        <div class='quote'>
            "Excellence in typography enhances readability and professionalism."
        </div>
    </body>
    </html>";

var pdf = renderer.RenderHtmlAsPdf(html, "Assets/Fonts");
pdf.SaveAs("typography-demo.pdf");
var renderer = new ChromePdfRenderer();

// HTML with custom fonts and typography
var html = @"
    <html>
    <head>
        <link href='https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap' rel='stylesheet'>
        <style>
            @font-face {
                font-family: 'CustomBrand';
                src: url('BrandFont.ttf') format('truetype');
            }

            body {
                font-family: 'Roboto', Arial, sans-serif;
                font-size: 11pt;
                line-height: 1.6;
                color: #333;
            }

            h1 {
                font-family: 'CustomBrand', Georgia, serif;
                font-size: 28pt;
                color: #0066cc;
                letter-spacing: -0.5px;
            }

            .quote {
                font-style: italic;
                font-size: 14pt;
                color: #666;
                border-left: 4px solid #0066cc;
                padding-left: 20px;
                margin: 20px 0;
            }
        </style>
    </head>
    <body>
        <h1>Professional Document</h1>
        <p>This document demonstrates professional typography.</p>
        <div class='quote'>
            "Excellence in typography enhances readability and professionalism."
        </div>
    </body>
    </html>";

var pdf = renderer.RenderHtmlAsPdf(html, "Assets/Fonts");
pdf.SaveAs("typography-demo.pdf");
Dim renderer = New ChromePdfRenderer()

' HTML with custom fonts and typography
Dim html = "
    <html>
    <head>
        <link href='https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap' rel='stylesheet'>
        <style>
            @font-face {
                font-family: 'CustomBrand';
                src: url('BrandFont.ttf') format('truetype');
            }

            body {
                font-family: 'Roboto', Arial, sans-serif;
                font-size: 11pt;
                line-height: 1.6;
                color: #333;
            }

            h1 {
                font-family: 'CustomBrand', Georgia, serif;
                font-size: 28pt;
                color: #0066cc;
                letter-spacing: -0.5px;
            }

            .quote {
                font-style: italic;
                font-size: 14pt;
                color: #666;
                border-left: 4px solid #0066cc;
                padding-left: 20px;
                margin: 20px 0;
            }
        </style>
    </head>
    <body>
        <h1>Professional Document</h1>
        <p>This document demonstrates professional typography.</p>
        <div class='quote'>
            "Excellence in typography enhances readability [and] professionalism." </div> </body> </html>"

Dim pdf = renderer.RenderHtmlAsPdf(html, "Assets/Fonts")
pdf.SaveAs("typography-demo.pdf")
$vbLabelText   $csharpLabel

现实世界示例:我如何生成发票PDF?

让我们创建一个完整的、可投入生产的发票生成器,展示在现实应用中创建PDF文档的最佳实践。 这个示例展示了为什么成千上万的企业选择IronPDF作为他们的C# PDF生成器来满足发票生成的需求 - 它以强大且可维护的方式结合了数据绑定专业样式正确的文档结构。 您可以将此代码自定义为您自己的PDF 创建任务: ## IronPDF 提供那些高级 PDF 功能?

using IronPDF;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

public class InvoiceGenerator
{
    private readonly ChromePdfRenderer _renderer;

    public InvoiceGenerator()
    {
        _renderer = new ChromePdfRenderer();
        ConfigureRenderer();
    }

    private void ConfigureRenderer()
    {
        // Professional page setup
        _renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
        _renderer.RenderingOptions.MarginTop = 25;
        _renderer.RenderingOptions.MarginBottom = 25;
        _renderer.RenderingOptions.MarginLeft = 25;
        _renderer.RenderingOptions.MarginRight = 25;
        _renderer.RenderingOptions.PrintHtmlBackgrounds = true;

        // Add footer with page numbers
        _renderer.RenderingOptions.TextFooter = new TextHeaderFooter
        {
            Text = "Page {page} of {total-pages} | Invoice generated on {date}",
            FontSize = 9,
            Font = "Arial",
            CenterText = true
        };
    }

    public void CreateInvoice(Invoice invoice)
    {
        var html = GenerateInvoiceHtml(invoice);
        var pdf = _renderer.RenderHtmlAsPdf(html);

        // Add metadata to the final document
        pdf.MetaData.Title = $"Invoice {invoice.Number}";
        pdf.MetaData.Author = "Your Company Name";
        pdf.MetaData.Subject = $"Invoice for {invoice.CustomerName}";
        pdf.MetaData.Keywords = "invoice, billing, payment";
        pdf.MetaData.CreationDate = DateTime.Now;

        // Save the PDF document
        var fileName = $"Invoice-{invoice.Number}.pdf";
        pdf.SaveAs(fileName);

        Console.WriteLine($"PDF generated successfully: {fileName}");
    }

    private string GenerateInvoiceHtml(Invoice invoice)
    {
        var itemsHtml = string.Join("", invoice.Items.Select(item => @"
            <tr>
                <td style='padding: 12px; border-bottom: 1px solid #eee;'>{item.Description}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: center;'>{item.Quantity}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: right;'>${item.UnitPrice:F2}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: right;'>${item.Total:F2}</td>
            </tr>"));

        return @"
            <html>
            <head>
                <style>
                    * {{box-sizing: border-box;}}
                    body {{font-family: 'Segoe UI', Arial, sans-serif; 
                        line-height: 1.6;
                        color: #333;
                        margin: 0;
                        padding: 0;}}
                    .invoice-container {{max-width: 800px;
                        margin: 0 auto;
                        padding: 40px;}}
                    .invoice-header {{display: flex;
                        justify-content: space-between;
                        margin-bottom: 40px;
                        padding-bottom: 20px;
                        border-bottom: 3px solid #0066cc;}}
                    .company-details {{flex: 1;}}
                    .company-details h1 {{color: #0066cc;
                        margin: 0 0 10px 0;
                        font-size: 28px;}}
                    .invoice-details {{flex: 1;
                        text-align: right;}}
                    .invoice-details h2 {{margin: 0 0 10px 0;
                        color: #666;
                        font-size: 24px;}}
                    .invoice-number {{font-size: 18px;
                        color: #0066cc;
                        font-weight: bold;}}
                    .billing-section {{display: flex;
                        justify-content: space-between;
                        margin-bottom: 40px;}}
                    .billing-box {{flex: 1;
                        padding: 20px;
                        background: #f8f9fa;
                        border-radius: 8px;
                        margin-right: 20px;}}
                    .billing-box:last-child {{margin-right: 0;}}
                    .billing-box h3 {{margin: 0 0 15px 0;
                        color: #0066cc;
                        font-size: 16px;
                        text-transform: uppercase;
                        letter-spacing: 1px;}}
                    .items-table {{width: 100%;
                        border-collapse: collapse;
                        margin-bottom: 40px;}}
                    .items-table th {{background: #0066cc;
                        color: white;
                        padding: 12px;
                        text-align: left;
                        font-weight: 600;}}
                    .items-table th:last-child {{text-align: right;}}
                    .totals-section {{display: flex;
                        justify-content: flex-end;
                        margin-bottom: 40px;}}
                    .totals-box {{width: 300px;}}
                    .total-row {{display: flex;
                        justify-content: space-between;
                        padding: 8px 0;
                        border-bottom: 1px solid #eee;}}
                    .total-row.final {{border-bottom: none;
                        border-top: 2px solid #0066cc;
                        margin-top: 10px;
                        padding-top: 15px;
                        font-size: 20px;
                        font-weight: bold;
                        color: #0066cc;}}
                    .payment-terms {{background: #f8f9fa;
                        padding: 20px;
                        border-radius: 8px;
                        margin-bottom: 30px;}}
                    .payment-terms h3 {{margin: 0 0 10px 0;
                        color: #0066cc;}}
                    .footer-note {{text-align: center;
                        color: #666;
                        font-size: 14px;
                        margin-top: 40px;
                        padding-top: 20px;
                        border-top: 1px solid #eee;}}
                </style>
            </head>
            <body>
                <div class='invoice-container'>
                    <div class='invoice-header'>
                        <div class='company-details'>
                            <h1>{invoice.CompanyName}</h1>
                            <p>{invoice.CompanyAddress}<br>
                            {invoice.CompanyCity}, {invoice.CompanyState} {invoice.CompanyZip}<br>
                            Phone: {invoice.CompanyPhone}<br>
                            Email: {invoice.CompanyEmail}</p>
                        </div>
                        <div class='invoice-details'>
                            <h2>INVOICE</h2>
                            <p class='invoice-number'>#{invoice.Number}</p>
                            <p><strong>Date:</strong> {invoice.Date:MMMM dd, yyyy}<br>
                            <strong>Due Date:</strong> {invoice.DueDate:MMMM dd, yyyy}</p>
                        </div>
                    </div>

                    <div class='billing-section'>
                        <div class='billing-box'>
                            <h3>Bill To</h3>
                            <p><strong>{invoice.CustomerName}</strong><br>
                            {invoice.CustomerAddress}<br>
                            {invoice.CustomerCity}, {invoice.CustomerState} {invoice.CustomerZip}<br>
                            {invoice.CustomerEmail}</p>
                        </div>
                        <div class='billing-box'>
                            <h3>Payment Information</h3>
                            <p><strong>Payment Terms:</strong> {invoice.PaymentTerms}<br>
                            <strong>Invoice Status:</strong> <span style='color: #ff6b6b;'>Unpaid</span><br>
                            <strong>Amount Due:</strong> ${invoice.Total:F2}</p>
                        </div>
                    </div>

                    <table class='items-table'>
                        <thead>
                            <tr>
                                <th>Description</th>
                                <th style='text-align: center;'>Quantity</th>
                                <th style='text-align: right;'>Unit Price</th>
                                <th style='text-align: right;'>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            {itemsHtml}
                        </tbody>
                    </table>

                    <div class='totals-section'>
                        <div class='totals-box'>
                            <div class='total-row'>
                                <span>Subtotal:</span>
                                <span>${invoice.Subtotal:F2}</span>
                            </div>
                            <div class='total-row'>
                                <span>Tax ({invoice.TaxRate:F0}%):</span>
                                <span>${invoice.Tax:F2}</span>
                            </div>
                            <div class='total-row final'>
                                <span>Total Due:</span>
                                <span>${invoice.Total:F2}</span>
                            </div>
                        </div>
                    </div>

                    <div class='payment-terms'>
                        <h3>Payment Terms & Conditions</h3>
                        <p>Payment is due within {invoice.PaymentTerms}. Late payments are subject to a 1.5% monthly service charge.
                        Please make checks payable to {invoice.CompanyName} or pay online at {invoice.CompanyWebsite}.</p>
                    </div>

                    <div class='footer-note'>
                        <p>Thank you for your business! This invoice was generated automatically using our C# PDF generation system.</p>
                        <p>Questions? Contact us at {invoice.CompanyEmail} or {invoice.CompanyPhone}</p>
                    </div>
                </div>
            </body>
            </html>";
    }
}

// Invoice model classes
public class Invoice
{
    public string Number { get; set; }
    public DateTime Date { get; set; }
    public DateTime DueDate { get; set; }
    public string CompanyName { get; set; }
    public string CompanyAddress { get; set; }
    public string CompanyCity { get; set; }
    public string CompanyState { get; set; }
    public string CompanyZip { get; set; }
    public string CompanyPhone { get; set; }
    public string CompanyEmail { get; set; }
    public string CompanyWebsite { get; set; }
    public string CustomerName { get; set; }
    public string CustomerAddress { get; set; }
    public string CustomerCity { get; set; }
    public string CustomerState { get; set; }
    public string CustomerZip { get; set; }
    public string CustomerEmail { get; set; }
    public string PaymentTerms { get; set; }
    public List<InvoiceItem> Items { get; set; }
    public decimal Subtotal => Items.Sum(i => i.Total);
    public decimal TaxRate { get; set; }
    public decimal Tax => Subtotal * (TaxRate / 100);
    public decimal Total => Subtotal + Tax;
}

public class InvoiceItem
{
    public string Description { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Total => Quantity * UnitPrice;
}

// Usage example
var generator = new InvoiceGenerator();
var invoice = new Invoice
{
    Number = "INV-2024-001",
    Date = DateTime.Now,
    DueDate = DateTime.Now.AddDays(30),
    CompanyName = "Your Company Name",
    CompanyAddress = "123 Business Street",
    CompanyCity = "New York",
    CompanyState = "NY",
    CompanyZip = "10001",
    CompanyPhone = "(555) 123-4567",
    CompanyEmail = "billing@yourcompany.com",
    CompanyWebsite = "www.yourcompany.com",
    CustomerName = "Acme Corporation",
    CustomerAddress = "456 Client Avenue",
    CustomerCity = "Los Angeles",
    CustomerState = "CA",
    CustomerZip = "90001",
    CustomerEmail = "accounts@acmecorp.com",
    PaymentTerms = "Net 30",
    TaxRate = 8.5m,
    Items = new List<InvoiceItem>
    {
        new() { Description = "Professional Services - March 2024", Quantity = 40, UnitPrice = 125.00m },
        new() { Description = "Software License (Annual)", Quantity = 1, UnitPrice = 2400.00m },
        new() { Description = "Technical Support", Quantity = 10, UnitPrice = 150.00m }
    }
};

generator.CreateInvoice(invoice);
using IronPDF;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

public class InvoiceGenerator
{
    private readonly ChromePdfRenderer _renderer;

    public InvoiceGenerator()
    {
        _renderer = new ChromePdfRenderer();
        ConfigureRenderer();
    }

    private void ConfigureRenderer()
    {
        // Professional page setup
        _renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
        _renderer.RenderingOptions.MarginTop = 25;
        _renderer.RenderingOptions.MarginBottom = 25;
        _renderer.RenderingOptions.MarginLeft = 25;
        _renderer.RenderingOptions.MarginRight = 25;
        _renderer.RenderingOptions.PrintHtmlBackgrounds = true;

        // Add footer with page numbers
        _renderer.RenderingOptions.TextFooter = new TextHeaderFooter
        {
            Text = "Page {page} of {total-pages} | Invoice generated on {date}",
            FontSize = 9,
            Font = "Arial",
            CenterText = true
        };
    }

    public void CreateInvoice(Invoice invoice)
    {
        var html = GenerateInvoiceHtml(invoice);
        var pdf = _renderer.RenderHtmlAsPdf(html);

        // Add metadata to the final document
        pdf.MetaData.Title = $"Invoice {invoice.Number}";
        pdf.MetaData.Author = "Your Company Name";
        pdf.MetaData.Subject = $"Invoice for {invoice.CustomerName}";
        pdf.MetaData.Keywords = "invoice, billing, payment";
        pdf.MetaData.CreationDate = DateTime.Now;

        // Save the PDF document
        var fileName = $"Invoice-{invoice.Number}.pdf";
        pdf.SaveAs(fileName);

        Console.WriteLine($"PDF generated successfully: {fileName}");
    }

    private string GenerateInvoiceHtml(Invoice invoice)
    {
        var itemsHtml = string.Join("", invoice.Items.Select(item => @"
            <tr>
                <td style='padding: 12px; border-bottom: 1px solid #eee;'>{item.Description}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: center;'>{item.Quantity}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: right;'>${item.UnitPrice:F2}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: right;'>${item.Total:F2}</td>
            </tr>"));

        return @"
            <html>
            <head>
                <style>
                    * {{box-sizing: border-box;}}
                    body {{font-family: 'Segoe UI', Arial, sans-serif; 
                        line-height: 1.6;
                        color: #333;
                        margin: 0;
                        padding: 0;}}
                    .invoice-container {{max-width: 800px;
                        margin: 0 auto;
                        padding: 40px;}}
                    .invoice-header {{display: flex;
                        justify-content: space-between;
                        margin-bottom: 40px;
                        padding-bottom: 20px;
                        border-bottom: 3px solid #0066cc;}}
                    .company-details {{flex: 1;}}
                    .company-details h1 {{color: #0066cc;
                        margin: 0 0 10px 0;
                        font-size: 28px;}}
                    .invoice-details {{flex: 1;
                        text-align: right;}}
                    .invoice-details h2 {{margin: 0 0 10px 0;
                        color: #666;
                        font-size: 24px;}}
                    .invoice-number {{font-size: 18px;
                        color: #0066cc;
                        font-weight: bold;}}
                    .billing-section {{display: flex;
                        justify-content: space-between;
                        margin-bottom: 40px;}}
                    .billing-box {{flex: 1;
                        padding: 20px;
                        background: #f8f9fa;
                        border-radius: 8px;
                        margin-right: 20px;}}
                    .billing-box:last-child {{margin-right: 0;}}
                    .billing-box h3 {{margin: 0 0 15px 0;
                        color: #0066cc;
                        font-size: 16px;
                        text-transform: uppercase;
                        letter-spacing: 1px;}}
                    .items-table {{width: 100%;
                        border-collapse: collapse;
                        margin-bottom: 40px;}}
                    .items-table th {{background: #0066cc;
                        color: white;
                        padding: 12px;
                        text-align: left;
                        font-weight: 600;}}
                    .items-table th:last-child {{text-align: right;}}
                    .totals-section {{display: flex;
                        justify-content: flex-end;
                        margin-bottom: 40px;}}
                    .totals-box {{width: 300px;}}
                    .total-row {{display: flex;
                        justify-content: space-between;
                        padding: 8px 0;
                        border-bottom: 1px solid #eee;}}
                    .total-row.final {{border-bottom: none;
                        border-top: 2px solid #0066cc;
                        margin-top: 10px;
                        padding-top: 15px;
                        font-size: 20px;
                        font-weight: bold;
                        color: #0066cc;}}
                    .payment-terms {{background: #f8f9fa;
                        padding: 20px;
                        border-radius: 8px;
                        margin-bottom: 30px;}}
                    .payment-terms h3 {{margin: 0 0 10px 0;
                        color: #0066cc;}}
                    .footer-note {{text-align: center;
                        color: #666;
                        font-size: 14px;
                        margin-top: 40px;
                        padding-top: 20px;
                        border-top: 1px solid #eee;}}
                </style>
            </head>
            <body>
                <div class='invoice-container'>
                    <div class='invoice-header'>
                        <div class='company-details'>
                            <h1>{invoice.CompanyName}</h1>
                            <p>{invoice.CompanyAddress}<br>
                            {invoice.CompanyCity}, {invoice.CompanyState} {invoice.CompanyZip}<br>
                            Phone: {invoice.CompanyPhone}<br>
                            Email: {invoice.CompanyEmail}</p>
                        </div>
                        <div class='invoice-details'>
                            <h2>INVOICE</h2>
                            <p class='invoice-number'>#{invoice.Number}</p>
                            <p><strong>Date:</strong> {invoice.Date:MMMM dd, yyyy}<br>
                            <strong>Due Date:</strong> {invoice.DueDate:MMMM dd, yyyy}</p>
                        </div>
                    </div>

                    <div class='billing-section'>
                        <div class='billing-box'>
                            <h3>Bill To</h3>
                            <p><strong>{invoice.CustomerName}</strong><br>
                            {invoice.CustomerAddress}<br>
                            {invoice.CustomerCity}, {invoice.CustomerState} {invoice.CustomerZip}<br>
                            {invoice.CustomerEmail}</p>
                        </div>
                        <div class='billing-box'>
                            <h3>Payment Information</h3>
                            <p><strong>Payment Terms:</strong> {invoice.PaymentTerms}<br>
                            <strong>Invoice Status:</strong> <span style='color: #ff6b6b;'>Unpaid</span><br>
                            <strong>Amount Due:</strong> ${invoice.Total:F2}</p>
                        </div>
                    </div>

                    <table class='items-table'>
                        <thead>
                            <tr>
                                <th>Description</th>
                                <th style='text-align: center;'>Quantity</th>
                                <th style='text-align: right;'>Unit Price</th>
                                <th style='text-align: right;'>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            {itemsHtml}
                        </tbody>
                    </table>

                    <div class='totals-section'>
                        <div class='totals-box'>
                            <div class='total-row'>
                                <span>Subtotal:</span>
                                <span>${invoice.Subtotal:F2}</span>
                            </div>
                            <div class='total-row'>
                                <span>Tax ({invoice.TaxRate:F0}%):</span>
                                <span>${invoice.Tax:F2}</span>
                            </div>
                            <div class='total-row final'>
                                <span>Total Due:</span>
                                <span>${invoice.Total:F2}</span>
                            </div>
                        </div>
                    </div>

                    <div class='payment-terms'>
                        <h3>Payment Terms & Conditions</h3>
                        <p>Payment is due within {invoice.PaymentTerms}. Late payments are subject to a 1.5% monthly service charge.
                        Please make checks payable to {invoice.CompanyName} or pay online at {invoice.CompanyWebsite}.</p>
                    </div>

                    <div class='footer-note'>
                        <p>Thank you for your business! This invoice was generated automatically using our C# PDF generation system.</p>
                        <p>Questions? Contact us at {invoice.CompanyEmail} or {invoice.CompanyPhone}</p>
                    </div>
                </div>
            </body>
            </html>";
    }
}

// Invoice model classes
public class Invoice
{
    public string Number { get; set; }
    public DateTime Date { get; set; }
    public DateTime DueDate { get; set; }
    public string CompanyName { get; set; }
    public string CompanyAddress { get; set; }
    public string CompanyCity { get; set; }
    public string CompanyState { get; set; }
    public string CompanyZip { get; set; }
    public string CompanyPhone { get; set; }
    public string CompanyEmail { get; set; }
    public string CompanyWebsite { get; set; }
    public string CustomerName { get; set; }
    public string CustomerAddress { get; set; }
    public string CustomerCity { get; set; }
    public string CustomerState { get; set; }
    public string CustomerZip { get; set; }
    public string CustomerEmail { get; set; }
    public string PaymentTerms { get; set; }
    public List<InvoiceItem> Items { get; set; }
    public decimal Subtotal => Items.Sum(i => i.Total);
    public decimal TaxRate { get; set; }
    public decimal Tax => Subtotal * (TaxRate / 100);
    public decimal Total => Subtotal + Tax;
}

public class InvoiceItem
{
    public string Description { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Total => Quantity * UnitPrice;
}

// Usage example
var generator = new InvoiceGenerator();
var invoice = new Invoice
{
    Number = "INV-2024-001",
    Date = DateTime.Now,
    DueDate = DateTime.Now.AddDays(30),
    CompanyName = "Your Company Name",
    CompanyAddress = "123 Business Street",
    CompanyCity = "New York",
    CompanyState = "NY",
    CompanyZip = "10001",
    CompanyPhone = "(555) 123-4567",
    CompanyEmail = "billing@yourcompany.com",
    CompanyWebsite = "www.yourcompany.com",
    CustomerName = "Acme Corporation",
    CustomerAddress = "456 Client Avenue",
    CustomerCity = "Los Angeles",
    CustomerState = "CA",
    CustomerZip = "90001",
    CustomerEmail = "accounts@acmecorp.com",
    PaymentTerms = "Net 30",
    TaxRate = 8.5m,
    Items = new List<InvoiceItem>
    {
        new() { Description = "Professional Services - March 2024", Quantity = 40, UnitPrice = 125.00m },
        new() { Description = "Software License (Annual)", Quantity = 1, UnitPrice = 2400.00m },
        new() { Description = "Technical Support", Quantity = 10, UnitPrice = 150.00m }
    }
};

generator.CreateInvoice(invoice);
Imports IronPDF
Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Linq

Public Class InvoiceGenerator
    Private ReadOnly _renderer As ChromePdfRenderer

    Public Sub New()
        _renderer = New ChromePdfRenderer()
        ConfigureRenderer()
    End Sub

    Private Sub ConfigureRenderer()
        ' Professional page setup
        _renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
        _renderer.RenderingOptions.MarginTop = 25
        _renderer.RenderingOptions.MarginBottom = 25
        _renderer.RenderingOptions.MarginLeft = 25
        _renderer.RenderingOptions.MarginRight = 25
        _renderer.RenderingOptions.PrintHtmlBackgrounds = True

        ' Add footer with page numbers
        _renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {
            .Text = "Page {page} of {total-pages} | Invoice generated on {date}",
            .FontSize = 9,
            .Font = "Arial",
            .CenterText = True
        }
    End Sub

    Public Sub CreateInvoice(invoice As Invoice)
        Dim html = GenerateInvoiceHtml(invoice)
        Dim pdf = _renderer.RenderHtmlAsPdf(html)

        ' Add metadata to the final document
        pdf.MetaData.Title = $"Invoice {invoice.Number}"
        pdf.MetaData.Author = "Your Company Name"
        pdf.MetaData.Subject = $"Invoice for {invoice.CustomerName}"
        pdf.MetaData.Keywords = "invoice, billing, payment"
        pdf.MetaData.CreationDate = DateTime.Now

        ' Save the PDF document
        Dim fileName = $"Invoice-{invoice.Number}.pdf"
        pdf.SaveAs(fileName)

        Console.WriteLine($"PDF generated successfully: {fileName}")
    End Sub

    Private Function GenerateInvoiceHtml(invoice As Invoice) As String
        Dim itemsHtml = String.Join("", invoice.Items.Select(Function(item) $"
            <tr>
                <td style='padding: 12px; border-bottom: 1px solid #eee;'>{item.Description}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: center;'>{item.Quantity}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: right;'>${item.UnitPrice:F2}</td>
                <td style='padding: 12px; border-bottom: 1px solid #eee; text-align: right;'>${item.Total:F2}</td>
            </tr>"))

        Return $"
            <html>
            <head>
                <style>
                    * {{box-sizing: border-box;}}
                    body {{font-family: 'Segoe UI', Arial, sans-serif; 
                        line-height: 1.6;
                        color: #333;
                        margin: 0;
                        padding: 0;}}
                    .invoice-container {{max-width: 800px;
                        margin: 0 auto;
                        padding: 40px;}}
                    .invoice-header {{display: flex;
                        justify-content: space-between;
                        margin-bottom: 40px;
                        padding-bottom: 20px;
                        border-bottom: 3px solid #0066cc;}}
                    .company-details {{flex: 1;}}
                    .company-details h1 {{color: #0066cc;
                        margin: 0 0 10px 0;
                        font-size: 28px;}}
                    .invoice-details {{flex: 1;
                        text-align: right;}}
                    .invoice-details h2 {{margin: 0 0 10px 0;
                        color: #666;
                        font-size: 24px;}}
                    .invoice-number {{font-size: 18px;
                        color: #0066cc;
                        font-weight: bold;}}
                    .billing-section {{display: flex;
                        justify-content: space-between;
                        margin-bottom: 40px;}}
                    .billing-box {{flex: 1;
                        padding: 20px;
                        background: #f8f9fa;
                        border-radius: 8px;
                        margin-right: 20px;}}
                    .billing-box:last-child {{margin-right: 0;}}
                    .billing-box h3 {{margin: 0 0 15px 0;
                        color: #0066cc;
                        font-size: 16px;
                        text-transform: uppercase;
                        letter-spacing: 1px;}}
                    .items-table {{width: 100%;
                        border-collapse: collapse;
                        margin-bottom: 40px;}}
                    .items-table th {{background: #0066cc;
                        color: white;
                        padding: 12px;
                        text-align: left;
                        font-weight: 600;}}
                    .items-table th:last-child {{text-align: right;}}
                    .totals-section {{display: flex;
                        justify-content: flex-end;
                        margin-bottom: 40px;}}
                    .totals-box {{width: 300px;}}
                    .total-row {{display: flex;
                        justify-content: space-between;
                        padding: 8px 0;
                        border-bottom: 1px solid #eee;}}
                    .total-row.final {{border-bottom: none;
                        border-top: 2px solid #0066cc;
                        margin-top: 10px;
                        padding-top: 15px;
                        font-size: 20px;
                        font-weight: bold;
                        color: #0066cc;}}
                    .payment-terms {{background: #f8f9fa;
                        padding: 20px;
                        border-radius: 8px;
                        margin-bottom: 30px;}}
                    .payment-terms h3 {{margin: 0 0 10px 0;
                        color: #0066cc;}}
                    .footer-note {{text-align: center;
                        color: #666;
                        font-size: 14px;
                        margin-top: 40px;
                        padding-top: 20px;
                        border-top: 1px solid #eee;}}
                </style>
            </head>
            <body>
                <div class='invoice-container'>
                    <div class='invoice-header'>
                        <div class='company-details'>
                            <h1>{invoice.CompanyName}</h1>
                            <p>{invoice.CompanyAddress}<br>
                            {invoice.CompanyCity}, {invoice.CompanyState} {invoice.CompanyZip}<br>
                            Phone: {invoice.CompanyPhone}<br>
                            Email: {invoice.CompanyEmail}</p>
                        </div>
                        <div class='invoice-details'>
                            <h2>INVOICE</h2>
                            <p class='invoice-number'>#{invoice.Number}</p>
                            <p><strong>Date:</strong> {invoice.Date:MMMM dd, yyyy}<br>
                            <strong>Due Date:</strong> {invoice.DueDate:MMMM dd, yyyy}</p>
                        </div>
                    </div>

                    <div class='billing-section'>
                        <div class='billing-box'>
                            <h3>Bill To</h3>
                            <p><strong>{invoice.CustomerName}</strong><br>
                            {invoice.CustomerAddress}<br>
                            {invoice.CustomerCity}, {invoice.CustomerState} {invoice.CustomerZip}<br>
                            {invoice.CustomerEmail}</p>
                        </div>
                        <div class='billing-box'>
                            <h3>Payment Information</h3>
                            <p><strong>Payment Terms:</strong> {invoice.PaymentTerms}<br>
                            <strong>Invoice Status:</strong> <span style='color: #ff6b6b;'>Unpaid</span><br>
                            <strong>Amount Due:</strong> ${invoice.Total:F2}</p>
                        </div>
                    </div>

                    <table class='items-table'>
                        <thead>
                            <tr>
                                <th>Description</th>
                                <th style='text-align: center;'>Quantity</th>
                                <th style='text-align: right;'>Unit Price</th>
                                <th style='text-align: right;'>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            {itemsHtml}
                        </tbody>
                    </table>

                    <div class='totals-section'>
                        <div class='totals-box'>
                            <div class='total-row'>
                                <span>Subtotal:</span>
                                <span>${invoice.Subtotal:F2}</span>
                            </div>
                            <div class='total-row'>
                                <span>Tax ({invoice.TaxRate:F0}%):</span>
                                <span>${invoice.Tax:F2}</span>
                            </div>
                            <div class='total-row final'>
                                <span>Total Due:</span>
                                <span>${invoice.Total:F2}</span>
                            </div>
                        </div>
                    </div>

                    <div class='payment-terms'>
                        <h3>Payment Terms & Conditions</h3>
                        <p>Payment is due within {invoice.PaymentTerms}. Late payments are subject to a 1.5% monthly service charge.
                        Please make checks payable to {invoice.CompanyName} or pay online at {invoice.CompanyWebsite}.</p>
                    </div>

                    <div class='footer-note'>
                        <p>Thank you for your business! This invoice was generated automatically using our C# PDF generation system.</p>
                        <p>Questions? Contact us at {invoice.CompanyEmail} or {invoice.CompanyPhone}</p>
                    </div>
                </div>
            </body>
            </html>"
    End Function
End Class

' Invoice model classes
Public Class Invoice
    Public Property Number As String
    Public Property [Date] As DateTime
    Public Property DueDate As DateTime
    Public Property CompanyName As String
    Public Property CompanyAddress As String
    Public Property CompanyCity As String
    Public Property CompanyState As String
    Public Property CompanyZip As String
    Public Property CompanyPhone As String
    Public Property CompanyEmail As String
    Public Property CompanyWebsite As String
    Public Property CustomerName As String
    Public Property CustomerAddress As String
    Public Property CustomerCity As String
    Public Property CustomerState As String
    Public Property CustomerZip As String
    Public Property CustomerEmail As String
    Public Property PaymentTerms As String
    Public Property Items As List(Of InvoiceItem)
    Public ReadOnly Property Subtotal As Decimal
        Get
            Return Items.Sum(Function(i) i.Total)
        End Get
    End Property
    Public Property TaxRate As Decimal
    Public ReadOnly Property Tax As Decimal
        Get
            Return Subtotal * (TaxRate / 100)
        End Get
    End Property
    Public ReadOnly Property Total As Decimal
        Get
            Return Subtotal + Tax
        End Get
    End Property
End Class

Public Class InvoiceItem
    Public Property Description As String
    Public Property Quantity As Integer
    Public Property UnitPrice As Decimal
    Public ReadOnly Property Total As Decimal
        Get
            Return Quantity * UnitPrice
        End Get
    End Property
End Class

' Usage example
Dim generator As New InvoiceGenerator()
Dim invoice As New Invoice With {
    .Number = "INV-2024-001",
    .[Date] = DateTime.Now,
    .DueDate = DateTime.Now.AddDays(30),
    .CompanyName = "Your Company Name",
    .CompanyAddress = "123 Business Street",
    .CompanyCity = "New York",
    .CompanyState = "NY",
    .CompanyZip = "10001",
    .CompanyPhone = "(555) 123-4567",
    .CompanyEmail = "billing@yourcompany.com",
    .CompanyWebsite = "www.yourcompany.com",
    .CustomerName = "Acme Corporation",
    .CustomerAddress = "456 Client Avenue",
    .CustomerCity = "Los Angeles",
    .CustomerState = "CA",
    .CustomerZip = "90001",
    .CustomerEmail = "accounts@acmecorp.com",
    .PaymentTerms = "Net 30",
    .TaxRate = 8.5D,
    .Items = New List(Of InvoiceItem) From {
        New InvoiceItem With {.Description = "Professional Services - March 2024", .Quantity = 40, .UnitPrice = 125.0D},
        New InvoiceItem With {.Description = "Software License (Annual)", .Quantity = 1, .UnitPrice = 2400.0D},
        New InvoiceItem With {.Description = "Technical Support", .Quantity = 10, .UnitPrice = 150.0D}
    }
}

generator.CreateInvoice(invoice)
$vbLabelText   $csharpLabel

IronPDF提供什么高级PDF功能?

这些高级功能允许您创建交互式表单、保护敏感文件,以及在您在 .NET 构建 PDF时精确操控现有 PDF。 这些功能是全球数千开发人员信赖 IronPDF 满足其任务关键型 PDF 生成 需求的原因。 这些功能就是为什么全球超过1400万开发者信任IronPDF来满足他们关键任务的PDF生成需求。 了解这些功能帮助您构建全面的PDF解决方案,以满足甚至是最苛刻的要求 - 从创建可填写的表单到在您的C# PDF创建项目中实施企业级安全。

生成互动PDF表单

IronPDF 可以将 HTML 表单转化为交互式 PDF 表单,用户可以在任何 PDF 阅读器中填写: ### 保护您的生成的 PDF

// Namespace: IronPDF
using IronPDF;
// Namespace: System
using System;

var renderer = new ChromePdfRenderer();

// Enable form creation from HTML
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

// Create an interactive form with various input types
var formHtml = @"
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; padding: 40px; }
            .form-group { margin-bottom: 20px; }
            label { display: block; margin-bottom: 5px; font-weight: bold; }
            input[type='text'], input[type='email'], select, textarea {
                width: 100%;
                padding: 8px;
                border: 1px solid #ccc;
                border-radius: 4px;
                font-size: 14px;
            }
            .checkbox-group { margin: 10px 0; }
            .submit-section { 
                margin-top: 30px; 
                padding-top: 20px; 
                border-top: 2px solid #0066cc; 
            }
        </style>
    </head>
    <body>
        <h1>Application Form</h1>
        <form>
            <div class='form-group'>
                <label for='fullName'>Full Name:</label>
                <input type='text' id='fullName' name='fullName' required />
            </div>

            <div class='form-group'>
                <label for='email'>Email Address:</label>
                <input type='email' id='email' name='email' required />
            </div>

            <div class='form-group'>
                <label for='department'>Department:</label>
                <select id='department' name='department'>
                    <option value=''>Select Department</option>
                    <option value='sales'>Sales</option>
                    <option value='marketing'>Marketing</option>
                    <option value='engineering'>Engineering</option>
                    <option value='hr'>Human Resources</option>
                </select>
            </div>

            <div class='form-group'>
                <label>Interests:</label>
                <div class='checkbox-group'>
                    <label><input type='checkbox' name='interests' value='training' /> Professional Training</label>
                    <label><input type='checkbox' name='interests' value='conferences' /> Industry Conferences</label>
                    <label><input type='checkbox' name='interests' value='certification' /> Certification Programs</label>
                </div>
            </div>

            <div class='form-group'>
                <label for='comments'>Additional Comments:</label>
                <textarea id='comments' name='comments' rows='4'></textarea>
            </div>

            <div class='submit-section'>
                <p><em>Please save this form and email to hr@company.com</em></p>
            </div>
        </form>
    </body>
    </html>";

// Create the PDF with form fields
var formPdf = renderer.RenderHtmlAsPdf(formHtml);

// Optionally pre-fill form fields programmatically
formPdf.Form.FindFormField("fullName").Value = "John Smith";
formPdf.Form.FindFormField("email").Value = "john.smith@example.com";
formPdf.Form.FindFormField("department").Value = "engineering";

// Save the interactive form
formPdf.SaveAs("application-form.pdf");

// You can also read and process submitted forms
var submittedPdf = PdfDocument.FromFile("submitted-form.pdf");
var name = submittedPdf.Form.FindFormField("fullName").Value;
var email = submittedPdf.Form.FindFormField("email").Value;
Console.WriteLine($"Form submitted by: {name} ({email})");
// Namespace: IronPDF
using IronPDF;
// Namespace: System
using System;

var renderer = new ChromePdfRenderer();

// Enable form creation from HTML
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

// Create an interactive form with various input types
var formHtml = @"
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; padding: 40px; }
            .form-group { margin-bottom: 20px; }
            label { display: block; margin-bottom: 5px; font-weight: bold; }
            input[type='text'], input[type='email'], select, textarea {
                width: 100%;
                padding: 8px;
                border: 1px solid #ccc;
                border-radius: 4px;
                font-size: 14px;
            }
            .checkbox-group { margin: 10px 0; }
            .submit-section { 
                margin-top: 30px; 
                padding-top: 20px; 
                border-top: 2px solid #0066cc; 
            }
        </style>
    </head>
    <body>
        <h1>Application Form</h1>
        <form>
            <div class='form-group'>
                <label for='fullName'>Full Name:</label>
                <input type='text' id='fullName' name='fullName' required />
            </div>

            <div class='form-group'>
                <label for='email'>Email Address:</label>
                <input type='email' id='email' name='email' required />
            </div>

            <div class='form-group'>
                <label for='department'>Department:</label>
                <select id='department' name='department'>
                    <option value=''>Select Department</option>
                    <option value='sales'>Sales</option>
                    <option value='marketing'>Marketing</option>
                    <option value='engineering'>Engineering</option>
                    <option value='hr'>Human Resources</option>
                </select>
            </div>

            <div class='form-group'>
                <label>Interests:</label>
                <div class='checkbox-group'>
                    <label><input type='checkbox' name='interests' value='training' /> Professional Training</label>
                    <label><input type='checkbox' name='interests' value='conferences' /> Industry Conferences</label>
                    <label><input type='checkbox' name='interests' value='certification' /> Certification Programs</label>
                </div>
            </div>

            <div class='form-group'>
                <label for='comments'>Additional Comments:</label>
                <textarea id='comments' name='comments' rows='4'></textarea>
            </div>

            <div class='submit-section'>
                <p><em>Please save this form and email to hr@company.com</em></p>
            </div>
        </form>
    </body>
    </html>";

// Create the PDF with form fields
var formPdf = renderer.RenderHtmlAsPdf(formHtml);

// Optionally pre-fill form fields programmatically
formPdf.Form.FindFormField("fullName").Value = "John Smith";
formPdf.Form.FindFormField("email").Value = "john.smith@example.com";
formPdf.Form.FindFormField("department").Value = "engineering";

// Save the interactive form
formPdf.SaveAs("application-form.pdf");

// You can also read and process submitted forms
var submittedPdf = PdfDocument.FromFile("submitted-form.pdf");
var name = submittedPdf.Form.FindFormField("fullName").Value;
var email = submittedPdf.Form.FindFormField("email").Value;
Console.WriteLine($"Form submitted by: {name} ({email})");
Imports IronPDF
Imports System

Dim renderer As New ChromePdfRenderer()

' Enable form creation from HTML
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

' Create an interactive form with various input types
Dim formHtml As String = "
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; padding: 40px; }
            .form-group { margin-bottom: 20px; }
            label { display: block; margin-bottom: 5px; font-weight: bold; }
            input[type='text'], input[type='email'], select, textarea {
                width: 100%;
                padding: 8px;
                border: 1px solid #ccc;
                border-radius: 4px;
                font-size: 14px;
            }
            .checkbox-group { margin: 10px 0; }
            .submit-section { 
                margin-top: 30px; 
                padding-top: 20px; 
                border-top: 2px solid #0066cc; 
            }
        </style>
    </head>
    <body>
        <h1>Application Form</h1>
        <form>
            <div class='form-group'>
                <label for='fullName'>Full Name:</label>
                <input type='text' id='fullName' name='fullName' required />
            </div>

            <div class='form-group'>
                <label for='email'>Email Address:</label>
                <input type='email' id='email' name='email' required />
            </div>

            <div class='form-group'>
                <label for='department'>Department:</label>
                <select id='department' name='department'>
                    <option value=''>Select Department</option>
                    <option value='sales'>Sales</option>
                    <option value='marketing'>Marketing</option>
                    <option value='engineering'>Engineering</option>
                    <option value='hr'>Human Resources</option>
                </select>
            </div>

            <div class='form-group'>
                <label>Interests:</label>
                <div class='checkbox-group'>
                    <label><input type='checkbox' name='interests' value='training' /> Professional Training</label>
                    <label><input type='checkbox' name='interests' value='conferences' /> Industry Conferences</label>
                    <label><input type='checkbox' name='interests' value='certification' /> Certification Programs</label>
                </div>
            </div>

            <div class='form-group'>
                <label for='comments'>Additional Comments:</label>
                <textarea id='comments' name='comments' rows='4'></textarea>
            </div>

            <div class='submit-section'>
                <p><em>Please save this form and email to hr@company.com</em></p>
            </div>
        </form>
    </body>
    </html>"

' Create the PDF with form fields
Dim formPdf = renderer.RenderHtmlAsPdf(formHtml)

' Optionally pre-fill form fields programmatically
formPdf.Form.FindFormField("fullName").Value = "John Smith"
formPdf.Form.FindFormField("email").Value = "john.smith@example.com"
formPdf.Form.FindFormField("department").Value = "engineering"

' Save the interactive form
formPdf.SaveAs("application-form.pdf")

' You can also read and process submitted forms
Dim submittedPdf = PdfDocument.FromFile("submitted-form.pdf")
Dim name = submittedPdf.Form.FindFormField("fullName").Value
Dim email = submittedPdf.Form.FindFormField("email").Value
Console.WriteLine($"Form submitted by: {name} ({email})")
$vbLabelText   $csharpLabel

保护您的生成PDF

处理敏感文件时,安全性首要考虑的因素。 IronPDF 提供了 全面的安全功能,以保护您的 PDF 文件免受未经授权的访问或修改:

// Namespace: IronPDF
using IronPDF;
// Namespace: IronPdf.Editing
using IronPdf.Editing;

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Document</h1><p>Sensitive information...</p>");

// Apply password protection
pdf.SecuritySettings.UserPassword = "user123";      // Required to open
pdf.SecuritySettings.OwnerPassword = "owner456";    // Required to modify

// Set detailed permissions
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserFormData = true;
pdf.SecuritySettings.AllowUserPrinting = PrintPermissions.LowQualityPrint;

// Add digital signature for authenticity
pdf.SignWithFile(
    certificatePath: "certificate.pfx",
    certificatePassword: "certpass123",
    signingReason: "Document Approval",
    signingLocation: "New York, NY",
    signatureImage: new Signature("signature.png")
    {
        Width = 150,
        Height = 50
    }
);

// Apply redaction to hide sensitive information
pdf.RedactTextOnPage(
    pageIndex: 0,
    searchText: "SSN: ***-**-****",
    replacementText: "[REDACTED]",
    caseSensitive: false
);

// Save the secured PDF
pdf.SaveAs("secure-confidential.pdf");
// Namespace: IronPDF
using IronPDF;
// Namespace: IronPdf.Editing
using IronPdf.Editing;

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Document</h1><p>Sensitive information...</p>");

// Apply password protection
pdf.SecuritySettings.UserPassword = "user123";      // Required to open
pdf.SecuritySettings.OwnerPassword = "owner456";    // Required to modify

// Set detailed permissions
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserFormData = true;
pdf.SecuritySettings.AllowUserPrinting = PrintPermissions.LowQualityPrint;

// Add digital signature for authenticity
pdf.SignWithFile(
    certificatePath: "certificate.pfx",
    certificatePassword: "certpass123",
    signingReason: "Document Approval",
    signingLocation: "New York, NY",
    signatureImage: new Signature("signature.png")
    {
        Width = 150,
        Height = 50
    }
);

// Apply redaction to hide sensitive information
pdf.RedactTextOnPage(
    pageIndex: 0,
    searchText: "SSN: ***-**-****",
    replacementText: "[REDACTED]",
    caseSensitive: false
);

// Save the secured PDF
pdf.SaveAs("secure-confidential.pdf");
Imports IronPDF
Imports IronPdf.Editing

Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Document</h1><p>Sensitive information...</p>")

' Apply password protection
pdf.SecuritySettings.UserPassword = "user123"      ' Required to open
pdf.SecuritySettings.OwnerPassword = "owner456"    ' Required to modify

' Set detailed permissions
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SecuritySettings.AllowUserFormData = True
pdf.SecuritySettings.AllowUserPrinting = PrintPermissions.LowQualityPrint

' Add digital signature for authenticity
pdf.SignWithFile(
    certificatePath:="certificate.pfx",
    certificatePassword:="certpass123",
    signingReason:="Document Approval",
    signingLocation:="New York, NY",
    signatureImage:=New Signature("signature.png") With {
        .Width = 150,
        .Height = 50
    }
)

' Apply redaction to hide sensitive information
pdf.RedactTextOnPage(
    pageIndex:=0,
    searchText:="SSN: ***-**-****",
    replacementText:="[REDACTED]",
    caseSensitive:=False
)

' Save the secured PDF
pdf.SaveAs("secure-confidential.pdf")
$vbLabelText   $csharpLabel

合并和拆分 PDF

合并多个 PDF 或提取特定页面对于文档管理工作流程是 必不可少 的:

// Namespace: IronPDF
using IronPDF;
// Namespace: System
using System;

// Merge multiple PDFs into one document
var coverPage = new ChromePdfRenderer().RenderHtmlAsPdf("<h1>Annual Report 2024</h1>");
var introduction = PdfDocument.FromFile("introduction.pdf");
var financials = PdfDocument.FromFile("financials.pdf");
var appendix = PdfDocument.FromFile("appendix.pdf");

// Merge all documents
var completeReport = PdfDocument.Merge(coverPage, introduction, financials, appendix);

// Add page numbers to the merged document
for (int i = 0; i < completeReport.PageCount; i++)
{
    completeReport.AddTextFooterToPage(i,
        $"Page {i + 1} of {completeReport.PageCount}",
        IronPdf.Font.FontTypes.Arial,
        10);
}

completeReport.SaveAs("annual-report-complete.pdf");

// Extract specific pages
var executiveSummary = completeReport.CopyPages(0, 4); // First 5 pages
executiveSummary.SaveAs("executive-summary.pdf");

// Split a large PDF into chapters
var sourcePdf = PdfDocument.FromFile("large-document.pdf");
var chaptersPerFile = 50;

for (int i = 0; i < sourcePdf.PageCount; i += chaptersPerFile)
{
    var endPage = Math.Min(i + chaptersPerFile - 1, sourcePdf.PageCount - 1);
    var chapter = sourcePdf.CopyPages(i, endPage);
    chapter.SaveAs($"chapter-{(i / chaptersPerFile) + 1}.pdf");
}
// Namespace: IronPDF
using IronPDF;
// Namespace: System
using System;

// Merge multiple PDFs into one document
var coverPage = new ChromePdfRenderer().RenderHtmlAsPdf("<h1>Annual Report 2024</h1>");
var introduction = PdfDocument.FromFile("introduction.pdf");
var financials = PdfDocument.FromFile("financials.pdf");
var appendix = PdfDocument.FromFile("appendix.pdf");

// Merge all documents
var completeReport = PdfDocument.Merge(coverPage, introduction, financials, appendix);

// Add page numbers to the merged document
for (int i = 0; i < completeReport.PageCount; i++)
{
    completeReport.AddTextFooterToPage(i,
        $"Page {i + 1} of {completeReport.PageCount}",
        IronPdf.Font.FontTypes.Arial,
        10);
}

completeReport.SaveAs("annual-report-complete.pdf");

// Extract specific pages
var executiveSummary = completeReport.CopyPages(0, 4); // First 5 pages
executiveSummary.SaveAs("executive-summary.pdf");

// Split a large PDF into chapters
var sourcePdf = PdfDocument.FromFile("large-document.pdf");
var chaptersPerFile = 50;

for (int i = 0; i < sourcePdf.PageCount; i += chaptersPerFile)
{
    var endPage = Math.Min(i + chaptersPerFile - 1, sourcePdf.PageCount - 1);
    var chapter = sourcePdf.CopyPages(i, endPage);
    chapter.SaveAs($"chapter-{(i / chaptersPerFile) + 1}.pdf");
}
Imports IronPDF
Imports System

' Merge multiple PDFs into one document
Dim coverPage = New ChromePdfRenderer().RenderHtmlAsPdf("<h1>Annual Report 2024</h1>")
Dim introduction = PdfDocument.FromFile("introduction.pdf")
Dim financials = PdfDocument.FromFile("financials.pdf")
Dim appendix = PdfDocument.FromFile("appendix.pdf")

' Merge all documents
Dim completeReport = PdfDocument.Merge(coverPage, introduction, financials, appendix)

' Add page numbers to the merged document
For i As Integer = 0 To completeReport.PageCount - 1
    completeReport.AddTextFooterToPage(i, $"Page {i + 1} of {completeReport.PageCount}", IronPdf.Font.FontTypes.Arial, 10)
Next

completeReport.SaveAs("annual-report-complete.pdf")

' Extract specific pages
Dim executiveSummary = completeReport.CopyPages(0, 4) ' First 5 pages
executiveSummary.SaveAs("executive-summary.pdf")

' Split a large PDF into chapters
Dim sourcePdf = PdfDocument.FromFile("large-document.pdf")
Dim chaptersPerFile As Integer = 50

For i As Integer = 0 To sourcePdf.PageCount - 1 Step chaptersPerFile
    Dim endPage = Math.Min(i + chaptersPerFile - 1, sourcePdf.PageCount - 1)
    Dim chapter = sourcePdf.CopyPages(i, endPage)
    chapter.SaveAs($"chapter-{(i \ chaptersPerFile) + 1}.pdf")
Next
$vbLabelText   $csharpLabel

为 PDF 添加水印对于文档控制和品牌推广至关重要。

为PDF加水印对于文档控制和品牌非常重要。 IronPDF 支持文本和图像水印:

// Namespace: IronPDF
using IronPDF;
// Namespace: System
using System;

var pdf = PdfDocument.FromFile("document.pdf");

// Add text watermark
pdf.ApplyWatermark(
    html: "<h1 style='color: #ff0000; opacity: 0.5; font-size: 100px;'>DRAFT</h1>",
    rotation: 45,
    opacity: 50,
    verticalAlignment: VerticalAlignment.Middle,
    horizontalAlignment: HorizontalAlignment.Center
);

// Add image watermark (company logo)
pdf.ApplyWatermark(
    html: "<img src='logo-watermark.png' style='width: 300px;' />",
    rotation: 0,
    opacity: 30,
    verticalAlignment: VerticalAlignment.Bottom,
    horizontalAlignment: HorizontalAlignment.Right
);

// Add stamps for document status
pdf.StampHtml(
    Html: @"<div style='border: 3px solid green; padding: 10px;
            background: white; font-weight: bold; color: green;'>
            APPROVED<br/>
            " + DateTime.Now.ToString("MM/dd/yyyy") + @"
            </div>",
    X: 400,
    Y: 100,
    Width: 150,
    Height: 60,
    PageIndex: 0
);

pdf.SaveAs("watermarked-document.pdf");
// Namespace: IronPDF
using IronPDF;
// Namespace: System
using System;

var pdf = PdfDocument.FromFile("document.pdf");

// Add text watermark
pdf.ApplyWatermark(
    html: "<h1 style='color: #ff0000; opacity: 0.5; font-size: 100px;'>DRAFT</h1>",
    rotation: 45,
    opacity: 50,
    verticalAlignment: VerticalAlignment.Middle,
    horizontalAlignment: HorizontalAlignment.Center
);

// Add image watermark (company logo)
pdf.ApplyWatermark(
    html: "<img src='logo-watermark.png' style='width: 300px;' />",
    rotation: 0,
    opacity: 30,
    verticalAlignment: VerticalAlignment.Bottom,
    horizontalAlignment: HorizontalAlignment.Right
);

// Add stamps for document status
pdf.StampHtml(
    Html: @"<div style='border: 3px solid green; padding: 10px;
            background: white; font-weight: bold; color: green;'>
            APPROVED<br/>
            " + DateTime.Now.ToString("MM/dd/yyyy") + @"
            </div>",
    X: 400,
    Y: 100,
    Width: 150,
    Height: 60,
    PageIndex: 0
);

pdf.SaveAs("watermarked-document.pdf");
Imports IronPDF
Imports System

Dim pdf = PdfDocument.FromFile("document.pdf")

' Add text watermark
pdf.ApplyWatermark(
    html:="<h1 style='color: #ff0000; opacity: 0.5; font-size: 100px;'>DRAFT</h1>",
    rotation:=45,
    opacity:=50,
    verticalAlignment:=VerticalAlignment.Middle,
    horizontalAlignment:=HorizontalAlignment.Center
)

' Add image watermark (company logo)
pdf.ApplyWatermark(
    html:="<img src='logo-watermark.png' style='width: 300px;' />",
    rotation:=0,
    opacity:=30,
    verticalAlignment:=VerticalAlignment.Bottom,
    horizontalAlignment:=HorizontalAlignment.Right
)

' Add stamps for document status
pdf.StampHtml(
    Html:="<div style='border: 3px solid green; padding: 10px;
            background: white; font-weight: bold; color: green;'>
            APPROVED<br/>" & DateTime.Now.ToString("MM/dd/yyyy") & "
            </div>",
    X:=400,
    Y:=100,
    Width:=150,
    Height:=60,
    PageIndex:=0
)

pdf.SaveAs("watermarked-document.pdf")
$vbLabelText   $csharpLabel

如何在大规模生成 PDF 时优化性能?

无论您是在创建成千上万的发票还是处理大型批量作业,优化您的 PDF 生成 代码都可以显著提高吞吐量并减少资源消耗。 现代应用程序需要高效的 C# 中创建 PDF 的能力,不会阻塞线程或消耗过多内存。 这里是一些可靠的提升性能的策略,以便在不同的 PDF 生成任务中最大化性能,当您需要高效地在 .NET 中生成 PDF时。 ### 异步 PDF 生成

异步 PDF 生成

现代应用程序需要 非阻塞操作 以保持响应性。 ### 批量处理最佳实践

// Namespace: IronPDF
using IronPDF;
// Namespace: System.Threading.Tasks
using System.Threading.Tasks;
// Namespace: System.Collections.Generic
using System.Collections.Generic;
// Namespace: System.Linq
using System.Linq;
// Namespace: System
using System;
// Namespace: System.Threading
using System.Threading;

public class AsyncPdfService
{
    private readonly ChromePdfRenderer _renderer;

    public AsyncPdfService()
    {
        _renderer = new ChromePdfRenderer();
        // Configure renderer once for reuse
        _renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
    }

    public async Task<byte[]> GeneratePdfAsync(string html)
    {
        // Non-blocking PDF generation
        var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
        return pdf.BinaryData;
    }

    public async Task GenerateBatchAsync(List<string> htmlDocuments)
    {
        // Process multiple PDFs concurrently
        var tasks = htmlDocuments.Select(async (html, index) =>
        {
            var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
            await pdf.SaveAsAsync($"document-{index}.pdf");
        });

        await Task.WhenAll(tasks);
    }

    // Async with cancellation support
    public async Task<PdfDocument> GenerateWithTimeoutAsync(string html, int timeoutSeconds)
    {
        using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds));

        try
        {
            return await _renderer.RenderHtmlAsPdfAsync(html, cts.Token);
        }
        catch (OperationCanceledException)
        {
            throw new TimeoutException("PDF generation exceeded timeout");
        }
    }
}
// Namespace: IronPDF
using IronPDF;
// Namespace: System.Threading.Tasks
using System.Threading.Tasks;
// Namespace: System.Collections.Generic
using System.Collections.Generic;
// Namespace: System.Linq
using System.Linq;
// Namespace: System
using System;
// Namespace: System.Threading
using System.Threading;

public class AsyncPdfService
{
    private readonly ChromePdfRenderer _renderer;

    public AsyncPdfService()
    {
        _renderer = new ChromePdfRenderer();
        // Configure renderer once for reuse
        _renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
    }

    public async Task<byte[]> GeneratePdfAsync(string html)
    {
        // Non-blocking PDF generation
        var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
        return pdf.BinaryData;
    }

    public async Task GenerateBatchAsync(List<string> htmlDocuments)
    {
        // Process multiple PDFs concurrently
        var tasks = htmlDocuments.Select(async (html, index) =>
        {
            var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
            await pdf.SaveAsAsync($"document-{index}.pdf");
        });

        await Task.WhenAll(tasks);
    }

    // Async with cancellation support
    public async Task<PdfDocument> GenerateWithTimeoutAsync(string html, int timeoutSeconds)
    {
        using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds));

        try
        {
            return await _renderer.RenderHtmlAsPdfAsync(html, cts.Token);
        }
        catch (OperationCanceledException)
        {
            throw new TimeoutException("PDF generation exceeded timeout");
        }
    }
}
Imports IronPDF
Imports System.Threading.Tasks
Imports System.Collections.Generic
Imports System.Linq
Imports System
Imports System.Threading

Public Class AsyncPdfService
    Private ReadOnly _renderer As ChromePdfRenderer

    Public Sub New()
        _renderer = New ChromePdfRenderer()
        ' Configure renderer once for reuse
        _renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
    End Sub

    Public Async Function GeneratePdfAsync(html As String) As Task(Of Byte())
        ' Non-blocking PDF generation
        Dim pdf = Await _renderer.RenderHtmlAsPdfAsync(html)
        Return pdf.BinaryData
    End Function

    Public Async Function GenerateBatchAsync(htmlDocuments As List(Of String)) As Task
        ' Process multiple PDFs concurrently
        Dim tasks = htmlDocuments.Select(Async Function(html, index)
                                             Dim pdf = Await _renderer.RenderHtmlAsPdfAsync(html)
                                             Await pdf.SaveAsAsync($"document-{index}.pdf")
                                         End Function)

        Await Task.WhenAll(tasks)
    End Function

    ' Async with cancellation support
    Public Async Function GenerateWithTimeoutAsync(html As String, timeoutSeconds As Integer) As Task(Of PdfDocument)
        Using cts As New CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds))
            Try
                Return Await _renderer.RenderHtmlAsPdfAsync(html, cts.Token)
            Catch ex As OperationCanceledException
                Throw New TimeoutException("PDF generation exceeded timeout")
            End Try
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

在处理多个 PDF 时,适当的资源管理和并行处理可以显著提高性能:

内存优化技术

using IronPDF;
using System.Threading.Tasks.Dataflow;

public class BatchPdfProcessor
{
    private readonly ChromePdfRenderer _renderer;
    private readonly ActionBlock<PdfJob> _processingBlock;

    public BatchPdfProcessor(int maxConcurrency = 4)
    {
        _renderer = new ChromePdfRenderer();

        // Create a processing pipeline with controlled concurrency
        _processingBlock = new ActionBlock<PdfJob>(
            async job => await ProcessPdfAsync(job),
            new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism = maxConcurrency,
                BoundedCapacity = maxConcurrency * 2
            });
    }

    private async Task ProcessPdfAsync(PdfJob job)
    {
        try
        {
            var pdf = await _renderer.RenderHtmlAsPdfAsync(job.Html);
            await pdf.SaveAsAsync(job.OutputPath);
            job.OnSuccess?.Invoke();
        }
        catch (Exception ex)
        {
            job.OnError?.Invoke(ex);
        }
    }

    public async Task<bool> QueuePdfAsync(PdfJob job)
    {
        return await _processingBlock.SendAsync(job);
    }

    public async Task CompleteAsync()
    {
        _processingBlock.Complete();
        await _processingBlock.Completion;
    }
}

public class PdfJob
{
    public string Html { get; set; }
    public string OutputPath { get; set; }
    public Action OnSuccess { get; set; }
    public Action<Exception> OnError { get; set; }
}
using IronPDF;
using System.Threading.Tasks.Dataflow;

public class BatchPdfProcessor
{
    private readonly ChromePdfRenderer _renderer;
    private readonly ActionBlock<PdfJob> _processingBlock;

    public BatchPdfProcessor(int maxConcurrency = 4)
    {
        _renderer = new ChromePdfRenderer();

        // Create a processing pipeline with controlled concurrency
        _processingBlock = new ActionBlock<PdfJob>(
            async job => await ProcessPdfAsync(job),
            new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism = maxConcurrency,
                BoundedCapacity = maxConcurrency * 2
            });
    }

    private async Task ProcessPdfAsync(PdfJob job)
    {
        try
        {
            var pdf = await _renderer.RenderHtmlAsPdfAsync(job.Html);
            await pdf.SaveAsAsync(job.OutputPath);
            job.OnSuccess?.Invoke();
        }
        catch (Exception ex)
        {
            job.OnError?.Invoke(ex);
        }
    }

    public async Task<bool> QueuePdfAsync(PdfJob job)
    {
        return await _processingBlock.SendAsync(job);
    }

    public async Task CompleteAsync()
    {
        _processingBlock.Complete();
        await _processingBlock.Completion;
    }
}

public class PdfJob
{
    public string Html { get; set; }
    public string OutputPath { get; set; }
    public Action OnSuccess { get; set; }
    public Action<Exception> OnError { get; set; }
}
Imports IronPDF
Imports System.Threading.Tasks.Dataflow

Public Class BatchPdfProcessor
    Private ReadOnly _renderer As ChromePdfRenderer
    Private ReadOnly _processingBlock As ActionBlock(Of PdfJob)

    Public Sub New(Optional maxConcurrency As Integer = 4)
        _renderer = New ChromePdfRenderer()

        ' Create a processing pipeline with controlled concurrency
        _processingBlock = New ActionBlock(Of PdfJob)(
            Async Function(job) Await ProcessPdfAsync(job),
            New ExecutionDataflowBlockOptions With {
                .MaxDegreeOfParallelism = maxConcurrency,
                .BoundedCapacity = maxConcurrency * 2
            })
    End Sub

    Private Async Function ProcessPdfAsync(job As PdfJob) As Task
        Try
            Dim pdf = Await _renderer.RenderHtmlAsPdfAsync(job.Html)
            Await pdf.SaveAsAsync(job.OutputPath)
            job.OnSuccess?.Invoke()
        Catch ex As Exception
            job.OnError?.Invoke(ex)
        End Try
    End Function

    Public Async Function QueuePdfAsync(job As PdfJob) As Task(Of Boolean)
        Return Await _processingBlock.SendAsync(job)
    End Function

    Public Async Function CompleteAsync() As Task
        _processingBlock.Complete()
        Await _processingBlock.Completion
    End Function
End Class

Public Class PdfJob
    Public Property Html As String
    Public Property OutputPath As String
    Public Property OnSuccess As Action
    Public Property OnError As Action(Of Exception)
End Class
$vbLabelText   $csharpLabel

内存优化技术

缓存和模板优化

using IronPDF;

public class MemoryEfficientPdfGenerator
{
    private readonly ChromePdfRenderer _renderer;

    public MemoryEfficientPdfGenerator()
    {
        _renderer = new ChromePdfRenderer();

        // Optimize for memory usage
        _renderer.RenderingOptions.RenderQuality = 90; // Slightly lower quality for smaller size
        _renderer.RenderingOptions.ImageQuality = 85; // Compress images
    }

    // Stream large PDFs instead of loading into memory
    public async Task GenerateLargePdfToStreamAsync(string html, Stream outputStream)
    {
        var pdf = await _renderer.RenderHtmlAsPdfAsync(html);

        // Write directly to stream without keeping in memory
        using (pdf)
        {
            var bytes = pdf.BinaryData;
            await outputStream.WriteAsync(bytes, 0, bytes.Length);
        }
    }

    // Process large HTML in chunks
    public async Task<PdfDocument> GenerateFromChunksAsync(List<string> htmlChunks)
    {
        var pdfs = new List<PdfDocument>();

        try
        {
            // Generate each chunk separately
            foreach (var chunk in htmlChunks)
            {
                var chunkPdf = await _renderer.RenderHtmlAsPdfAsync(chunk);
                pdfs.Add(chunkPdf);
            }

            // Merge all chunks
            return PdfDocument.Merge(pdfs.ToArray());
        }
        finally
        {
            // Ensure all temporary PDFs are disposed
            foreach (var pdf in pdfs)
            {
                pdf?.Dispose();
            }
        }
    }
}
using IronPDF;

public class MemoryEfficientPdfGenerator
{
    private readonly ChromePdfRenderer _renderer;

    public MemoryEfficientPdfGenerator()
    {
        _renderer = new ChromePdfRenderer();

        // Optimize for memory usage
        _renderer.RenderingOptions.RenderQuality = 90; // Slightly lower quality for smaller size
        _renderer.RenderingOptions.ImageQuality = 85; // Compress images
    }

    // Stream large PDFs instead of loading into memory
    public async Task GenerateLargePdfToStreamAsync(string html, Stream outputStream)
    {
        var pdf = await _renderer.RenderHtmlAsPdfAsync(html);

        // Write directly to stream without keeping in memory
        using (pdf)
        {
            var bytes = pdf.BinaryData;
            await outputStream.WriteAsync(bytes, 0, bytes.Length);
        }
    }

    // Process large HTML in chunks
    public async Task<PdfDocument> GenerateFromChunksAsync(List<string> htmlChunks)
    {
        var pdfs = new List<PdfDocument>();

        try
        {
            // Generate each chunk separately
            foreach (var chunk in htmlChunks)
            {
                var chunkPdf = await _renderer.RenderHtmlAsPdfAsync(chunk);
                pdfs.Add(chunkPdf);
            }

            // Merge all chunks
            return PdfDocument.Merge(pdfs.ToArray());
        }
        finally
        {
            // Ensure all temporary PDFs are disposed
            foreach (var pdf in pdfs)
            {
                pdf?.Dispose();
            }
        }
    }
}
Imports IronPDF

Public Class MemoryEfficientPdfGenerator
    Private ReadOnly _renderer As ChromePdfRenderer

    Public Sub New()
        _renderer = New ChromePdfRenderer()

        ' Optimize for memory usage
        _renderer.RenderingOptions.RenderQuality = 90 ' Slightly lower quality for smaller size
        _renderer.RenderingOptions.ImageQuality = 85 ' Compress images
    End Sub

    ' Stream large PDFs instead of loading into memory
    Public Async Function GenerateLargePdfToStreamAsync(html As String, outputStream As Stream) As Task
        Dim pdf = Await _renderer.RenderHtmlAsPdfAsync(html)

        ' Write directly to stream without keeping in memory
        Using pdf
            Dim bytes = pdf.BinaryData
            Await outputStream.WriteAsync(bytes, 0, bytes.Length)
        End Using
    End Function

    ' Process large HTML in chunks
    Public Async Function GenerateFromChunksAsync(htmlChunks As List(Of String)) As Task(Of PdfDocument)
        Dim pdfs = New List(Of PdfDocument)()

        Try
            ' Generate each chunk separately
            For Each chunk In htmlChunks
                Dim chunkPdf = Await _renderer.RenderHtmlAsPdfAsync(chunk)
                pdfs.Add(chunkPdf)
            Next

            ' Merge all chunks
            Return PdfDocument.Merge(pdfs.ToArray())
        Finally
            ' Ensure all temporary PDFs are disposed
            For Each pdf In pdfs
                pdf?.Dispose()
            Next
        End Try
    End Function
End Class
$vbLabelText   $csharpLabel

通过缓存常用元素和优化模板来减少渲染时间:

创建 PDF 时常见问题有哪些,我如何解决这些问题?

using IronPDF;
using Microsoft.Extensions.Caching.Memory;

public class CachedPdfService
{
    private readonly ChromePdfRenderer _renderer;
    private readonly IMemoryCache _cache;
    private readonly Dictionary<string, string> _compiledTemplates;

    public CachedPdfService(IMemoryCache cache)
    {
        _renderer = new ChromePdfRenderer();
        _cache = cache;
        _compiledTemplates = new Dictionary<string, string>();

        // Pre-compile common templates
        PrecompileTemplates();
    }

    private void PrecompileTemplates()
    {
        // Load and cache common CSS
        var commonCss = File.ReadAllText("Templates/common.css");
        _compiledTemplates["commonCss"] = commonCss;

        // Cache logo as Base64
        var logoBytes = File.ReadAllBytes("Assets/logo.png");
        var logoBase64 = Convert.ToBase64String(logoBytes);
        _compiledTemplates["logoData"] = $"data:image/png;base64,{logoBase64}";
    }

    public async Task<byte[]> GenerateInvoicePdfAsync(string invoiceId, InvoiceData data)
    {
        // Check cache first
        var cacheKey = $"invoice_{invoiceId}";
        if (_cache.TryGetValue<byte[]>(cacheKey, out var cachedPdf))
        {
            return cachedPdf;
        }

        // Generate PDF with cached templates
        var html = BuildHtmlWithCache(data);
        var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
        var pdfBytes = pdf.BinaryData;

        // Cache for 1 hour
        _cache.Set(cacheKey, pdfBytes, TimeSpan.FromHours(1));

        return pdfBytes;
    }

    private string BuildHtmlWithCache(InvoiceData data)
    {
        return @"
            <html>
            <head>
                <style>{_compiledTemplates["commonCss"]}</style>
            </head>
            <body>
                <img src='{_compiledTemplates["logoData"]}' />

            </body>
            </html>";
    }
}
using IronPDF;
using Microsoft.Extensions.Caching.Memory;

public class CachedPdfService
{
    private readonly ChromePdfRenderer _renderer;
    private readonly IMemoryCache _cache;
    private readonly Dictionary<string, string> _compiledTemplates;

    public CachedPdfService(IMemoryCache cache)
    {
        _renderer = new ChromePdfRenderer();
        _cache = cache;
        _compiledTemplates = new Dictionary<string, string>();

        // Pre-compile common templates
        PrecompileTemplates();
    }

    private void PrecompileTemplates()
    {
        // Load and cache common CSS
        var commonCss = File.ReadAllText("Templates/common.css");
        _compiledTemplates["commonCss"] = commonCss;

        // Cache logo as Base64
        var logoBytes = File.ReadAllBytes("Assets/logo.png");
        var logoBase64 = Convert.ToBase64String(logoBytes);
        _compiledTemplates["logoData"] = $"data:image/png;base64,{logoBase64}";
    }

    public async Task<byte[]> GenerateInvoicePdfAsync(string invoiceId, InvoiceData data)
    {
        // Check cache first
        var cacheKey = $"invoice_{invoiceId}";
        if (_cache.TryGetValue<byte[]>(cacheKey, out var cachedPdf))
        {
            return cachedPdf;
        }

        // Generate PDF with cached templates
        var html = BuildHtmlWithCache(data);
        var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
        var pdfBytes = pdf.BinaryData;

        // Cache for 1 hour
        _cache.Set(cacheKey, pdfBytes, TimeSpan.FromHours(1));

        return pdfBytes;
    }

    private string BuildHtmlWithCache(InvoiceData data)
    {
        return @"
            <html>
            <head>
                <style>{_compiledTemplates["commonCss"]}</style>
            </head>
            <body>
                <img src='{_compiledTemplates["logoData"]}' />

            </body>
            </html>";
    }
}
Imports IronPDF
Imports Microsoft.Extensions.Caching.Memory
Imports System.IO
Imports System.Threading.Tasks

Public Class CachedPdfService
    Private ReadOnly _renderer As ChromePdfRenderer
    Private ReadOnly _cache As IMemoryCache
    Private ReadOnly _compiledTemplates As Dictionary(Of String, String)

    Public Sub New(cache As IMemoryCache)
        _renderer = New ChromePdfRenderer()
        _cache = cache
        _compiledTemplates = New Dictionary(Of String, String)()

        ' Pre-compile common templates
        PrecompileTemplates()
    End Sub

    Private Sub PrecompileTemplates()
        ' Load and cache common CSS
        Dim commonCss As String = File.ReadAllText("Templates/common.css")
        _compiledTemplates("commonCss") = commonCss

        ' Cache logo as Base64
        Dim logoBytes As Byte() = File.ReadAllBytes("Assets/logo.png")
        Dim logoBase64 As String = Convert.ToBase64String(logoBytes)
        _compiledTemplates("logoData") = $"data:image/png;base64,{logoBase64}"
    End Sub

    Public Async Function GenerateInvoicePdfAsync(invoiceId As String, data As InvoiceData) As Task(Of Byte())
        ' Check cache first
        Dim cacheKey As String = $"invoice_{invoiceId}"
        Dim cachedPdf As Byte() = Nothing
        If _cache.TryGetValue(cacheKey, cachedPdf) Then
            Return cachedPdf
        End If

        ' Generate PDF with cached templates
        Dim html As String = BuildHtmlWithCache(data)
        Dim pdf = Await _renderer.RenderHtmlAsPdfAsync(html)
        Dim pdfBytes As Byte() = pdf.BinaryData

        ' Cache for 1 hour
        _cache.Set(cacheKey, pdfBytes, TimeSpan.FromHours(1))

        Return pdfBytes
    End Function

    Private Function BuildHtmlWithCache(data As InvoiceData) As String
        Return $"
            <html>
            <head>
                <style>{_compiledTemplates("commonCss")}</style>
            </head>
            <body>
                <img src='{_compiledTemplates("logoData")}' />
            </body>
            </html>"
    End Function
End Class
$vbLabelText   $csharpLabel

即使使用像 IronPDF 这样的强大 .NET PDF 库,在开发或部署时您仍可能会遇到挑战, 当您在 C# 中创建 PDF时。

了解常见问题及其解决方案有助于您快速解决问题并维护流畅的PDF 生成操作。 理解 常见问题 及其 解决方案 有助于您快速解决问题并保持流畅的 PDF 生成 操作。 此故障排除指南涵盖了开发人员在他们在 .NET 中创建 PDF时面临的最常见问题,并提供了基于现实世界经验的实用解决方案。 别忘了,如果您在处理PDF 创建挑战时需立即协助,24/7 支持始终可用。 请记住,24/7 支持 始终可用,如果您需要立即解决 PDF 创建 的挑战。

最常见的问题之一是 PDF 看起来空白或无法正确渲染。

这通常发生在资源未及时加载或存在JavaScript 时序问题时: 这通常发生在资源加载不及时或JavaScript 存在时序问题时:

// Namespace: IronPDF
using IronPDF;
// Namespace: System.IO
using System.IO;
// Namespace: System
using System;

// Problem: PDF is blank or missing content
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(complexHtml); // Results in blank PDF

// Solution 1: Add render delay for JavaScript-heavy content
renderer.RenderingOptions.RenderDelay = 3000; // Wait 3 seconds
renderer.RenderingOptions.EnableJavaScript = true;

// Solution 2: Wait for specific elements
renderer.RenderingOptions.WaitFor = new WaitFor()
{
    JavaScriptQuery = "document.querySelector('#chart-loaded') !== null",
    WaitForType = WaitForType.JavaScript,
    Timeout = 30000 // 30 second timeout
};

// Solution 3: Use base path for local assets
var basePath = Path.GetFullPath("Assets");
var pdf = renderer.RenderHtmlAsPdf(htmlWithImages, basePath);

// Solution 4: Embed assets as Base64
var imageBase64 = Convert.ToBase64String(File.ReadAllBytes("logo.png"));
var htmlWithEmbedded = $@"<img src='data:image/png;base64,{imageBase64}' />";
// Namespace: IronPDF
using IronPDF;
// Namespace: System.IO
using System.IO;
// Namespace: System
using System;

// Problem: PDF is blank or missing content
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(complexHtml); // Results in blank PDF

// Solution 1: Add render delay for JavaScript-heavy content
renderer.RenderingOptions.RenderDelay = 3000; // Wait 3 seconds
renderer.RenderingOptions.EnableJavaScript = true;

// Solution 2: Wait for specific elements
renderer.RenderingOptions.WaitFor = new WaitFor()
{
    JavaScriptQuery = "document.querySelector('#chart-loaded') !== null",
    WaitForType = WaitForType.JavaScript,
    Timeout = 30000 // 30 second timeout
};

// Solution 3: Use base path for local assets
var basePath = Path.GetFullPath("Assets");
var pdf = renderer.RenderHtmlAsPdf(htmlWithImages, basePath);

// Solution 4: Embed assets as Base64
var imageBase64 = Convert.ToBase64String(File.ReadAllBytes("logo.png"));
var htmlWithEmbedded = $@"<img src='data:image/png;base64,{imageBase64}' />";
Imports IronPDF
Imports System.IO
Imports System

' Problem: PDF is blank or missing content
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(complexHtml) ' Results in blank PDF

' Solution 1: Add render delay for JavaScript-heavy content
renderer.RenderingOptions.RenderDelay = 3000 ' Wait 3 seconds
renderer.RenderingOptions.EnableJavaScript = True

' Solution 2: Wait for specific elements
renderer.RenderingOptions.WaitFor = New WaitFor() With {
    .JavaScriptQuery = "document.querySelector('#chart-loaded') !== null",
    .WaitForType = WaitForType.JavaScript,
    .Timeout = 30000 ' 30 second timeout
}

' Solution 3: Use base path for local assets
Dim basePath As String = Path.GetFullPath("Assets")
pdf = renderer.RenderHtmlAsPdf(htmlWithImages, basePath)

' Solution 4: Embed assets as Base64
Dim imageBase64 As String = Convert.ToBase64String(File.ReadAllBytes("logo.png"))
Dim htmlWithEmbedded As String = $"<img src='data:image/png;base64,{imageBase64}' />"
$vbLabelText   $csharpLabel

了解更多关于渲染问题的故障排除

// Enable detailed logging
IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "IronPdf.log";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
// Enable detailed logging
IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "IronPdf.log";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
' Enable detailed logging
IronPdf.Logging.Logger.EnableDebugging = True
IronPdf.Logging.Logger.LogFilePath = "IronPdf.log"
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All
$vbLabelText   $csharpLabel

问题 2:初始渲染缓慢

问题 2:初始渲染缓慢

IronPDF 的初始渲染可能需要 2-3 秒执行,这在桌面环境下类似于 Chrome 打开时的正常启动时间: 阅读更多有关优化启动性能的信息

// Problem: First PDF takes too long to generate
public class PdfService
{
    private ChromePdfRenderer _renderer;

    // Solution 1: Initialize renderer at startup
    public void Initialize()
    {
        _renderer = new ChromePdfRenderer();

        // Warm up the renderer
        _ = _renderer.RenderHtmlAsPdf("<p>Warm up</p>");
    }

    // Solution 2: Use IronPdf.Native packages for faster initialization
    // `Install-Package IronPdf.Native.Windows.X64`
    // This includes pre-loaded binaries for your platform

    // Solution 3: For cloud deployments, use appropriate packages
    // For Linux: `Install-Package IronPdf.Linux`
    // For Docker: Use IronPdf.Linux with proper dependencies
}

// Solution 4: Skip initialization checks in production
IronPdf.Installation.SkipInitialization = true; // Use only with persistent storage
// Problem: First PDF takes too long to generate
public class PdfService
{
    private ChromePdfRenderer _renderer;

    // Solution 1: Initialize renderer at startup
    public void Initialize()
    {
        _renderer = new ChromePdfRenderer();

        // Warm up the renderer
        _ = _renderer.RenderHtmlAsPdf("<p>Warm up</p>");
    }

    // Solution 2: Use IronPdf.Native packages for faster initialization
    // `Install-Package IronPdf.Native.Windows.X64`
    // This includes pre-loaded binaries for your platform

    // Solution 3: For cloud deployments, use appropriate packages
    // For Linux: `Install-Package IronPdf.Linux`
    // For Docker: Use IronPdf.Linux with proper dependencies
}

// Solution 4: Skip initialization checks in production
IronPdf.Installation.SkipInitialization = true; // Use only with persistent storage
Imports IronPdf

' Problem: First PDF takes too long to generate
Public Class PdfService
    Private _renderer As ChromePdfRenderer

    ' Solution 1: Initialize renderer at startup
    Public Sub Initialize()
        _renderer = New ChromePdfRenderer()

        ' Warm up the renderer
        Dim unused = _renderer.RenderHtmlAsPdf("<p>Warm up</p>")
    End Sub

    ' Solution 2: Use IronPdf.Native packages for faster initialization
    ' `Install-Package IronPdf.Native.Windows.X64`
    ' This includes pre-loaded binaries for your platform

    ' Solution 3: For cloud deployments, use appropriate packages
    ' For Linux: `Install-Package IronPdf.Linux`
    ' For Docker: Use IronPdf.Linux with proper dependencies
End Class

' Solution 4: Skip initialization checks in production
IronPdf.Installation.SkipInitialization = True ' Use only with persistent storage
$vbLabelText   $csharpLabel

问题 3:Linux/Docker 上的部署问题

问题3:Linux/Docker上的部署问题

以下是解决常见部署问题的方法: 特别是在 Google Cloud Run 环境中:

# Dockerfile for IronPDF on Linux
FROM mcr.microsoft.com/dotnet/aspnet:8.0

# Install required dependencies
RUN apt-get update && apt-get install -y \
    libglib2.0-0 \
    libnss3 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxrandr2 \
    libgbm1 \
    libpango-1.0-0 \
    libcairo2 \
    libasound2 \
    libxshmfence1 \
    libx11-xcb1

# Copy and run your application
WORKDIR /app
COPY . .
ENTRYPOINT [".NET", "YourApp.dll"]

了解更多关于 Docker 部署的信息

// Use 2nd generation execution environment
// Deploy with: gcloud run deploy --execution-environment gen2

// In your code, ensure compatibility
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
// Use 2nd generation execution environment
// Deploy with: gcloud run deploy --execution-environment gen2

// In your code, ensure compatibility
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
' Use 2nd generation execution environment
' Deploy with: gcloud run deploy --execution-environment gen2

' In your code, ensure compatibility
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = True
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
$vbLabelText   $csharpLabel

问题 4:内存和性能问题

对于高容量 PDF 生成,优化内存使用和性能:

对于大批量 PDF 生成,优化内存使用和性能:

// Problem: High memory usage or slow batch processing
public class OptimizedPdfService
{
    private readonly ChromePdfRenderer _renderer;

    public OptimizedPdfService()
    {
        _renderer = new ChromePdfRenderer();

        // Optimize for performance
        _renderer.RenderingOptions.RenderQuality = 90;
        _renderer.RenderingOptions.ImageQuality = 85;

        // Disable features you don't need
        _renderer.RenderingOptions.EnableJavaScript = false; // If not needed
        _renderer.RenderingOptions.RenderDelay = 0; // If content is static
    }

    // Solution 1: Process large documents in chunks
    public async Task<PdfDocument> GenerateLargeReportAsync(List<ReportSection> sections)
    {
        var pdfs = new List<PdfDocument>();

        foreach (var section in sections)
        {
            var sectionHtml = GenerateSectionHtml(section);
            var sectionPdf = await _renderer.RenderHtmlAsPdfAsync(sectionHtml);
            pdfs.Add(sectionPdf);

            // Force garbage collection after each section
            if (pdfs.Count % 10 == 0)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        return PdfDocument.Merge(pdfs.ToArray());
    }

    // Solution 2: Use streaming for large files
    public async Task StreamLargePdfAsync(string html, HttpResponse response)
    {
        response.ContentType = "application/pdf";
        response.Headers.Add("Content-Disposition", "attachment; filename=report.pdf");

        var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
        var bytes = pdf.BinaryData;

        await response.Body.WriteAsync(bytes, 0, bytes.Length);
        await response.Body.FlushAsync();
    }
}
// Problem: High memory usage or slow batch processing
public class OptimizedPdfService
{
    private readonly ChromePdfRenderer _renderer;

    public OptimizedPdfService()
    {
        _renderer = new ChromePdfRenderer();

        // Optimize for performance
        _renderer.RenderingOptions.RenderQuality = 90;
        _renderer.RenderingOptions.ImageQuality = 85;

        // Disable features you don't need
        _renderer.RenderingOptions.EnableJavaScript = false; // If not needed
        _renderer.RenderingOptions.RenderDelay = 0; // If content is static
    }

    // Solution 1: Process large documents in chunks
    public async Task<PdfDocument> GenerateLargeReportAsync(List<ReportSection> sections)
    {
        var pdfs = new List<PdfDocument>();

        foreach (var section in sections)
        {
            var sectionHtml = GenerateSectionHtml(section);
            var sectionPdf = await _renderer.RenderHtmlAsPdfAsync(sectionHtml);
            pdfs.Add(sectionPdf);

            // Force garbage collection after each section
            if (pdfs.Count % 10 == 0)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        return PdfDocument.Merge(pdfs.ToArray());
    }

    // Solution 2: Use streaming for large files
    public async Task StreamLargePdfAsync(string html, HttpResponse response)
    {
        response.ContentType = "application/pdf";
        response.Headers.Add("Content-Disposition", "attachment; filename=report.pdf");

        var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
        var bytes = pdf.BinaryData;

        await response.Body.WriteAsync(bytes, 0, bytes.Length);
        await response.Body.FlushAsync();
    }
}
' Problem: High memory usage or slow batch processing
Public Class OptimizedPdfService
	Private ReadOnly _renderer As ChromePdfRenderer

	Public Sub New()
		_renderer = New ChromePdfRenderer()

		' Optimize for performance
		_renderer.RenderingOptions.RenderQuality = 90
		_renderer.RenderingOptions.ImageQuality = 85

		' Disable features you don't need
		_renderer.RenderingOptions.EnableJavaScript = False ' If not needed
		_renderer.RenderingOptions.RenderDelay = 0 ' If content is static
	End Sub

	' Solution 1: Process large documents in chunks
	Public Async Function GenerateLargeReportAsync(ByVal sections As List(Of ReportSection)) As Task(Of PdfDocument)
		Dim pdfs = New List(Of PdfDocument)()

		For Each section In sections
			Dim sectionHtml = GenerateSectionHtml(section)
			Dim sectionPdf = Await _renderer.RenderHtmlAsPdfAsync(sectionHtml)
			pdfs.Add(sectionPdf)

			' Force garbage collection after each section
			If pdfs.Count Mod 10 = 0 Then
				GC.Collect()
				GC.WaitForPendingFinalizers()
			End If
		Next section

		Return PdfDocument.Merge(pdfs.ToArray())
	End Function

	' Solution 2: Use streaming for large files
	Public Async Function StreamLargePdfAsync(ByVal html As String, ByVal response As HttpResponse) As Task
		response.ContentType = "application/pdf"
		response.Headers.Add("Content-Disposition", "attachment; filename=report.pdf")

		Dim pdf = Await _renderer.RenderHtmlAsPdfAsync(html)
		Dim bytes = pdf.BinaryData

		Await response.Body.WriteAsync(bytes, 0, bytes.Length)
		Await response.Body.FlushAsync()
	End Function
End Class
$vbLabelText   $csharpLabel

阅读完整的性能优化指南

问题 5:字体和编码问题

处理国际内容或自定义字体时:

// Problem: Fonts not rendering correctly
var renderer = new ChromePdfRenderer();

// Solution 1: Install fonts on the server
// For Linux/Docker, add to Dockerfile:
// RUN apt-get install -y fonts-liberation fonts-noto

// Solution 2: Embed fonts in HTML
var html = @"
<html>
<head>
    <style>
        @font-face {
            font-family: 'CustomFont';
            src: url('data:font/woff2;base64,[base64-encoded-font]') format('woff2');
        }
        body { font-family: 'CustomFont', Arial, sans-serif; }
    </style>
</head>
<body>
    <p>Content with custom font</p>
</body>
</html>";

// Solution 3: Use web fonts
var htmlWithWebFont = @"
<html>
<head>
    <link href='https://fonts.googleapis.com/css2?family=Noto+Sans+JP' rel='stylesheet'>
    <style>
        body { font-family: 'Noto Sans JP', sans-serif; }
    </style>
</head>
<body>
    <p>日本語のテキスト</p>
</body>
</html>";

// Ensure proper encoding
renderer.RenderingOptions.InputEncoding = Encoding.UTF8;
// Problem: Fonts not rendering correctly
var renderer = new ChromePdfRenderer();

// Solution 1: Install fonts on the server
// For Linux/Docker, add to Dockerfile:
// RUN apt-get install -y fonts-liberation fonts-noto

// Solution 2: Embed fonts in HTML
var html = @"
<html>
<head>
    <style>
        @font-face {
            font-family: 'CustomFont';
            src: url('data:font/woff2;base64,[base64-encoded-font]') format('woff2');
        }
        body { font-family: 'CustomFont', Arial, sans-serif; }
    </style>
</head>
<body>
    <p>Content with custom font</p>
</body>
</html>";

// Solution 3: Use web fonts
var htmlWithWebFont = @"
<html>
<head>
    <link href='https://fonts.googleapis.com/css2?family=Noto+Sans+JP' rel='stylesheet'>
    <style>
        body { font-family: 'Noto Sans JP', sans-serif; }
    </style>
</head>
<body>
    <p>日本語のテキスト</p>
</body>
</html>";

// Ensure proper encoding
renderer.RenderingOptions.InputEncoding = Encoding.UTF8;
' Problem: Fonts not rendering correctly
Dim renderer = New ChromePdfRenderer()

' Solution 1: Install fonts on the server
' For Linux/Docker, add to Dockerfile:
' RUN apt-get install -y fonts-liberation fonts-noto

' Solution 2: Embed fonts in HTML
Dim html = "
<html>
<head>
    <style>
        @font-face {
            font-family: 'CustomFont';
            src: url('data:font/woff2;base64,[base64-encoded-font]') format('woff2');
        }
        body { font-family: 'CustomFont', Arial, sans-serif; }
    </style>
</head>
<body>
    <p>Content with custom font</p>
</body>
</html>"

' Solution 3: Use web fonts
Dim htmlWithWebFont = "
<html>
<head>
    <link href='https://fonts.googleapis.com/css2?family=Noto+Sans+JP' rel='stylesheet'>
    <style>
        body { font-family: 'Noto Sans JP', sans-serif; }
    </style>
</head>
<body>
    <p>日本語のテキスト</p>
</body>
</html>"

' Ensure proper encoding
renderer.RenderingOptions.InputEncoding = Encoding.UTF8
$vbLabelText   $csharpLabel

如果您遇到此处未覆盖的问题,IronPDF 提供了优秀的支持资源:

  1. 24/7 在线聊天支持 - 实时与工程师交流,响应时间 30 秒

  2. 全面文档 - 详细的 API 参考和指南
  3. 知识库 - 常见问题的解决方案
  4. 代码示例 - 可直接使用的代码片段
  5. 代码示例 - 准备好使用的代码片段
  • IronPDF 版本

  • .NET 版本和平台
  • 导致问题的最小代码示例
  • 最小化代码示例重现该问题
  • 堆栈跟踪或错误消息

哪些平台支持 IronPDF 进行 PDF 生成?

IronPDF 的跨平台支持确保您的 PDF 生成 代码在不同环境中始终如一地工作。

无论您是部署到 Windows 服务器、Linux 容器或云平台,IronPDF 都提供生产部署必须的灵活性和可靠性,满足您的 C# PDF 生成器 需求。 这种通用兼容性是为什么 50多个国家的组织 每日依赖 IronPDF 来 生成数百万个 PDF。 从《财富》500 强公司 创建财务报告 到创业公司 生成客户发票,IronPDF 能满足任何对 PDF 在 .NET 中创建 的需求。 了解平台特定的考虑因素有助于确保您基础设施的平稳部署 - 无论是在本地服务器,还是在云环境中,您都需要在 以 C# 建立 PDF时。 ### .NET 版本兼容性

IronPDF 支持所有现代 .NET 版本,并持续更新以支持最新版本:

  • .NET 8 - 支持所有功能

  • .NET 9 - 完全支持(目前最新版本)
  • .NET 10 - 提前支持(IronPDF 已符合 2025 年 11 月发布)
  • .NET 7, 6, 5 - 完全支持
  • .NET Core 3.1+ - 支持所有功能
  • .NET Framework 4.6.2+ - 维护旧版本支持

    操作系统支持

操作系统支持

在任何主要操作系统上部署您的 PDF 生成解决方案:

Windows

  • Windows Server 2022, 2019, 2016, 2012
  • Windows Server 2022、2019、2016、2012

  • Red Hat Enterprise Linux

  • Red Hat Enterprise Linux
  • Red Hat Enterprise Linux
  • Red Hat Enterprise Linux
  • Red Hat Enterprise Linux
  • macOS 13(Ventura)及更新版本

  • macOS 13(Ventura)及更新版本

  • Apple Silicon (M1/M2/M3)原生支持
  • Intel-based Macs 完全支持
  • 完全支持基于 Intel 的 Mac

IronPDF 在所有主要云平台上无缝工作:

微软 Azure

亚马逊网络服务(AWS)

// Azure App Service configuration
// Use at least B1 tier for optimal performance
// Enable 64-bit platform in Configuration settings

// For Azure Functions
public static class PdfFunction
{
    [FunctionName("GeneratePdf")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req)
    {
        var renderer = new ChromePdfRenderer();
        var html = await new StreamReader(req.Body).ReadToEndAsync();
        var pdf = await renderer.RenderHtmlAsPdfAsync(html);

        return new FileContentResult(pdf.BinaryData, "application/pdf");
    }
}
// Azure App Service configuration
// Use at least B1 tier for optimal performance
// Enable 64-bit platform in Configuration settings

// For Azure Functions
public static class PdfFunction
{
    [FunctionName("GeneratePdf")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req)
    {
        var renderer = new ChromePdfRenderer();
        var html = await new StreamReader(req.Body).ReadToEndAsync();
        var pdf = await renderer.RenderHtmlAsPdfAsync(html);

        return new FileContentResult(pdf.BinaryData, "application/pdf");
    }
}
' Azure App Service configuration
' Use at least B1 tier for optimal performance
' Enable 64-bit platform in Configuration settings

' For Azure Functions
Public Module PdfFunction
	<FunctionName("GeneratePdf")>
	Public Async Function Run(<HttpTrigger(AuthorizationLevel.Function, "post")> ByVal req As HttpRequest) As Task(Of IActionResult)
		Dim renderer = New ChromePdfRenderer()
		Dim html = Await (New StreamReader(req.Body)).ReadToEndAsync()
		Dim pdf = Await renderer.RenderHtmlAsPdfAsync(html)

		Return New FileContentResult(pdf.BinaryData, "application/pdf")
	End Function
End Module
$vbLabelText   $csharpLabel

谷歌云平台

// AWS Lambda configuration
// Use custom runtime or container deployment
// Ensure Lambda has at least 512MB memory

public class PdfLambdaFunction
{
    private readonly ChromePdfRenderer _renderer;

    public PdfLambdaFunction()
    {
        _renderer = new ChromePdfRenderer();
        // Configure for Lambda environment
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
    }

    public async Task<APIGatewayProxyResponse> FunctionHandler(
        APIGatewayProxyRequest request,
        ILambdaContext context)
    {
        var pdf = await _renderer.RenderHtmlAsPdfAsync(request.Body);

        return new APIGatewayProxyResponse
        {
            StatusCode = 200,
            Headers = new Dictionary<string, string>
            {
                { "Content-Type", "application/pdf" }
            },
            Body = Convert.ToBase64String(pdf.BinaryData),
            IsBase64Encoded = true
        };
    }
}
// AWS Lambda configuration
// Use custom runtime or container deployment
// Ensure Lambda has at least 512MB memory

public class PdfLambdaFunction
{
    private readonly ChromePdfRenderer _renderer;

    public PdfLambdaFunction()
    {
        _renderer = new ChromePdfRenderer();
        // Configure for Lambda environment
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
    }

    public async Task<APIGatewayProxyResponse> FunctionHandler(
        APIGatewayProxyRequest request,
        ILambdaContext context)
    {
        var pdf = await _renderer.RenderHtmlAsPdfAsync(request.Body);

        return new APIGatewayProxyResponse
        {
            StatusCode = 200,
            Headers = new Dictionary<string, string>
            {
                { "Content-Type", "application/pdf" }
            },
            Body = Convert.ToBase64String(pdf.BinaryData),
            IsBase64Encoded = true
        };
    }
}
Imports System.Collections.Generic
Imports System.Threading.Tasks
Imports Amazon.Lambda.APIGatewayEvents
Imports Amazon.Lambda.Core
Imports IronPdf

' AWS Lambda configuration
' Use custom runtime or container deployment
' Ensure Lambda has at least 512MB memory

Public Class PdfLambdaFunction
    Private ReadOnly _renderer As ChromePdfRenderer

    Public Sub New()
        _renderer = New ChromePdfRenderer()
        ' Configure for Lambda environment
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
    End Sub

    Public Async Function FunctionHandler(
        request As APIGatewayProxyRequest,
        context As ILambdaContext) As Task(Of APIGatewayProxyResponse)

        Dim pdf = Await _renderer.RenderHtmlAsPdfAsync(request.Body)

        Return New APIGatewayProxyResponse With {
            .StatusCode = 200,
            .Headers = New Dictionary(Of String, String) From {
                {"Content-Type", "application/pdf"}
            },
            .Body = Convert.ToBase64String(pdf.BinaryData),
            .IsBase64Encoded = True
        }
    End Function
End Class
$vbLabelText   $csharpLabel

容器部署(Docker/Kubernetes)

# app.yaml for App Engine
runtime: aspnetcore
env: flex

# Use 2nd generation for Cloud Run
# Deploy with: gcloud run deploy --execution-environment gen2
# app.yaml for App Engine
runtime: aspnetcore
env: flex

# Use 2nd generation for Cloud Run
# Deploy with: gcloud run deploy --execution-environment gen2
YAML

容器部署 (Docker/Kubernetes)

桌面应用支持

# Multi-stage Dockerfile for optimal size
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["YourApp.csproj", "./"]
RUN .NET restore
COPY . .
RUN .NET publish -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app

# Install IronPDF dependencies
RUN apt-get update && apt-get install -y \
    libglib2.0-0 libnss3 libatk1.0-0 libatk-bridge2.0-0 \
    libcups2 libxkbcommon0 libxcomposite1 libxdamage1 \
    libxrandr2 libgbm1 libpango-1.0-0 libcairo2 libasound2

COPY --from=build /app/publish .
ENTRYPOINT [".NET", "YourApp.dll"]

IronPDF 兼容所有主要的 .NET 桌面框架:

IronPDF 适用于所有主要 .NET 桌面框架:

Windows 窗体

public partial class MainWindow : Window
{
    private async void GeneratePdfButton_Click(object sender, RoutedEventArgs e)
    {
        var renderer = new ChromePdfRenderer();
        var html = HtmlEditor.Text;

        var pdf = await renderer.RenderHtmlAsPdfAsync(html);

        var saveDialog = new SaveFileDialog
        {
            Filter = "PDF files (*.pdf)|*.pdf",
            DefaultExt = "pdf"
        };

        if (saveDialog.ShowDialog() == true)
        {
            pdf.SaveAs(saveDialog.FileName);
            MessageBox.Show("PDF saved successfully!");
        }
    }
}
public partial class MainWindow : Window
{
    private async void GeneratePdfButton_Click(object sender, RoutedEventArgs e)
    {
        var renderer = new ChromePdfRenderer();
        var html = HtmlEditor.Text;

        var pdf = await renderer.RenderHtmlAsPdfAsync(html);

        var saveDialog = new SaveFileDialog
        {
            Filter = "PDF files (*.pdf)|*.pdf",
            DefaultExt = "pdf"
        };

        if (saveDialog.ShowDialog() == true)
        {
            pdf.SaveAs(saveDialog.FileName);
            MessageBox.Show("PDF saved successfully!");
        }
    }
}
Partial Public Class MainWindow
	Inherits Window

	Private Async Sub GeneratePdfButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
		Dim renderer = New ChromePdfRenderer()
		Dim html = HtmlEditor.Text

		Dim pdf = Await renderer.RenderHtmlAsPdfAsync(html)

		Dim saveDialog = New SaveFileDialog With {
			.Filter = "PDF files (*.pdf)|*.pdf",
			.DefaultExt = "pdf"
		}

		If saveDialog.ShowDialog() = True Then
			pdf.SaveAs(saveDialog.FileName)
			MessageBox.Show("PDF saved successfully!")
		End If
	End Sub
End Class
$vbLabelText   $csharpLabel

MAUI(多平台应用 UI)

public partial class PdfGeneratorForm : Form
{
    private void btnGeneratePdf_Click(object sender, EventArgs e)
    {
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(txtHtml.Text);

        using (var saveDialog = new SaveFileDialog())
        {
            saveDialog.Filter = "PDF files|*.pdf";
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                pdf.SaveAs(saveDialog.FileName);
                MessageBox.Show($"PDF saved to {saveDialog.FileName}");
            }
        }
    }
}
public partial class PdfGeneratorForm : Form
{
    private void btnGeneratePdf_Click(object sender, EventArgs e)
    {
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(txtHtml.Text);

        using (var saveDialog = new SaveFileDialog())
        {
            saveDialog.Filter = "PDF files|*.pdf";
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                pdf.SaveAs(saveDialog.FileName);
                MessageBox.Show($"PDF saved to {saveDialog.FileName}");
            }
        }
    }
}
Partial Public Class PdfGeneratorForm
	Inherits Form

	Private Sub btnGeneratePdf_Click(ByVal sender As Object, ByVal e As EventArgs)
		Dim renderer = New ChromePdfRenderer()
		Dim pdf = renderer.RenderHtmlAsPdf(txtHtml.Text)

		Using saveDialog = New SaveFileDialog()
			saveDialog.Filter = "PDF files|*.pdf"
			If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
				pdf.SaveAs(saveDialog.FileName)
				MessageBox.Show($"PDF saved to {saveDialog.FileName}")
			End If
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

开始使用 PDF 创建

public partial class MainPage : ContentPage
{
    public async Task GeneratePdfAsync()
    {
        var renderer = new ChromePdfRenderer();
        var pdf = await renderer.RenderHtmlAsPdfAsync(HtmlContent);

        // Save to app's document directory
        var documentsPath = FileSystem.Current.AppDataDirectory;
        var filePath = Path.Combine(documentsPath, "output.pdf");

        await File.WriteAllBytesAsync(filePath, pdf.BinaryData);

        await DisplayAlert("Success", $"PDF saved to {filePath}", "OK");
    }
}
public partial class MainPage : ContentPage
{
    public async Task GeneratePdfAsync()
    {
        var renderer = new ChromePdfRenderer();
        var pdf = await renderer.RenderHtmlAsPdfAsync(HtmlContent);

        // Save to app's document directory
        var documentsPath = FileSystem.Current.AppDataDirectory;
        var filePath = Path.Combine(documentsPath, "output.pdf");

        await File.WriteAllBytesAsync(filePath, pdf.BinaryData);

        await DisplayAlert("Success", $"PDF saved to {filePath}", "OK");
    }
}
Partial Public Class MainPage
	Inherits ContentPage

	Public Async Function GeneratePdfAsync() As Task
		Dim renderer = New ChromePdfRenderer()
		Dim pdf = Await renderer.RenderHtmlAsPdfAsync(HtmlContent)

		' Save to app's document directory
		Dim documentsPath = FileSystem.Current.AppDataDirectory
		Dim filePath = Path.Combine(documentsPath, "output.pdf")

		Await File.WriteAllBytesAsync(filePath, pdf.BinaryData)

		Await DisplayAlert("Success", $"PDF saved to {filePath}", "OK")
	End Function
End Class
$vbLabelText   $csharpLabel

开始 PDF 创建

按照这个分步指南,从安装到您的第一个生成的 PDF。 按照此逐步指南,从安装到生成您的第一个 PDF。 IronPDF 使得入门变得简单,提供全面的资源和支持,帮助您每一步。

步骤1:安装IronPDF

选择最适合您开发环境的安装方法:

Visual Studio 包管理器**(推荐)

  1. 在 Visual Studio 中打开您的项目
  2. 右键点击解决方案资源管理器中的项目
  3. 选择"管理 NuGet 包" 4.搜索 "IronPDF" 5.点击安装 Iron Software 的 IronPDF 软件包

软件包管理器控制台**。

Install-Package IronPdf

.NET CLI

.NET add package IronPDF

NuGet 软件包包括在 Windows、Linux 和 macOS 上生成 PDF 所需的一切。 对于专业部署,可以考虑使用这些特定于平台的软件包,以优化大小和性能:

  • IronPdf.Linux - 为Linux环境优化
  • IronPdf.MacOs - 原生苹果硅芯片支持
  • IronPdf.Slim - 在运行时下载依赖项的最小包

步骤 2:创建您的第一个 PDF 文件

从一个简单的示例开始,以验证一切正常:

using IronPDF;

class Program
{
    static void Main()
    {
        // Create a new PDF generator instance
        var renderer = new ChromePdfRenderer();

        // Generate PDF from HTML
        var pdf = renderer.RenderHtmlAsPdf(@"
            <h1>Welcome to IronPDF!</h1>
            <p>This is your first generated PDF document.</p>
            <p>Created on: " + DateTime.Now + "</p>"
        );

        // Save the PDF
        pdf.SaveAs("my-first-pdf.pdf");

        Console.WriteLine("PDF created successfully!");
    }
}
using IronPDF;

class Program
{
    static void Main()
    {
        // Create a new PDF generator instance
        var renderer = new ChromePdfRenderer();

        // Generate PDF from HTML
        var pdf = renderer.RenderHtmlAsPdf(@"
            <h1>Welcome to IronPDF!</h1>
            <p>This is your first generated PDF document.</p>
            <p>Created on: " + DateTime.Now + "</p>"
        );

        // Save the PDF
        pdf.SaveAs("my-first-pdf.pdf");

        Console.WriteLine("PDF created successfully!");
    }
}
Imports IronPDF

Class Program
    Shared Sub Main()
        ' Create a new PDF generator instance
        Dim renderer As New ChromePdfRenderer()

        ' Generate PDF from HTML
        Dim pdf = renderer.RenderHtmlAsPdf("
            <h1>Welcome to IronPDF!</h1>
            <p>This is your first generated PDF document.</p>
            <p>Created on: " & DateTime.Now & "</p>"
        )

        ' Save the PDF
        pdf.SaveAs("my-first-pdf.pdf")

        Console.WriteLine("PDF created successfully!")
    End Sub
End Class
$vbLabelText   $csharpLabel

步骤 3:探索示例和教程

IronPDF 提供广泛的资源,帮助您掌握 PDF 生成:

1.代码示例--常见场景下的即用代码片段 2.教程 - 特定功能的分步指南 3.使用指南 - 实际问题的实用解决方案 4.API Reference - 所有类和方法的综合文档

步骤 4:在需要时寻求帮助

IronPDF 提供多种支持渠道,以确保您的成功:

步骤 5:开发和部署

免费开发许可

IronPDF 可免费用于开发和测试。 您可以在开发过程中不受任何限制地探索所有功能。 水印会出现在开发模式下生成的 PDF 上,但不会影响功能。

生产部署选项

当您准备部署到生产环境时,IronPDF 可提供灵活的许可:

1.免费试用 - 获得为期 30 天的试用许可证,在生产中进行测试,无水印

  1. 商业许可证 - 单项目部署起价为$999 3.企业解决方案 - 面向大型组织的定制软件包

在您的代码中应用许可证:

IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

步骤 6:保持更新

让您的 PDF 生成能力与时俱进:

定期更新可确保与最新的 .NET 版本兼容,包括性能改进、新功能和安全更新。

为什么选择 IronPDF 进行 C# PDF 生成?

在探索了 C# 中创建 PDF 的各种方法之后,您可能会想知道是什么让 IronPDF 成为许多开发人员的首选。 这不仅仅是功能的问题,而是 整个开发人员体验的问题,从最初的实施到需要 在 .NET 中生成 PDF 时的长期维护。 全球有超过 1400 万开发人员**使用 IronPDF 作为他们的 C# PDF 生成器,它已成为 .NET 应用程序中 **PDF 生成的事实标准。 让我们来看看为什么开发人员会选择 IronPDF 来满足他们的 PDF creation in C# 需求。

像素完美渲染

与其他生成近似 HTML 设计的 PDF 库不同,IronPDF 使用真正的 Chromium 引擎,确保您的 PDF 看起来完全像在现代网络浏览器中一样。 这种像素级的完美渲染能力正是金融机构信任 IronPDF 的原因,IronPDF 可在精度至关重要的情况下生成监管报告。 您的CSS 网格布局flexbox 设计JavaScript 渲染内容都能完美运行。 当您创建 PDF 文档时,无需再与专有的排版引擎作斗争或接受 "足够接近 "的结果。

开发人员友好型 API.

IronPDF 的 API 是 由开发人员设计,为开发人员而设计。 API 的简易性是初创公司能在数小时而不是数天内完成 PDF 生成工作的原因。 您无需学习复杂的PDF规范,而是使用熟悉的概念:


using IronPDF;

// Other libraries might require this:
// document.Add(new Paragraph("Hello World"));
// document.Add(new Table(3, 2));
// cell.SetBackgroundColor(ColorConstants.LIGHT_GRAY);

// With IronPDF, just use HTML:
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
    <h1>Hello World</h1>
    <table>
        <tr style='background: lightgray;'>
            <td>Simple</td>
            <td>Intuitive</td>
        </tr>
    </table>
");

### Enterprise-Ready Features

IronPDF includes features that **enterprise applications demand**, which is why Fortune 500 companies rely on it for mission-critical *document generation*:

- **Security:** Encryption, digital signatures, and permission controls protect sensitive documents
- **Compliance:** [PDF/A](/how-to/pdfa/) and [PDF/UA](/how-to/pdfua/) support ensures your *generated PDFs* meet regulatory requirements
- **Performance:** Async operations and batch processing handle millions of documents efficiently
- **Reliability:** Extensive error handling and logging help maintain uptime in production

### Outstanding Support

When you need help, IronPDF's support team consists of **actual engineers** who understand your challenges. With **24/7 live chat support** and typical response times **under 30 seconds**, you're never stuck waiting for answers. This level of support is why developers consistently rate IronPDF as having the best support in the industry. The support team can help with everything from basic questions to complex implementation challenges, ensuring your *PDF generation* projects succeed.

### Transparent Pricing

**No hidden fees**, **no surprise costs**, **no per-server licensing complications**. IronPDF's straightforward licensing model means you know exactly what you're paying for. Development is **always free**, and production licenses are **perpetual** - you own them forever. This transparency is refreshing in an industry known for complex licensing schemes.

### Active Development

IronPDF is **continuously improved** with monthly updates that add features, enhance performance, and ensure compatibility with the latest .NET releases. The team actively monitors customer feedback and implements requested features regularly. Recent additions include [enhanced form handling](/examples/form-data/), improved [PDF editing capabilities](/examples/editing-pdfs/), and optimizations for cloud deployments.

### Real-World Success Stories

**Thousands of companies** across industries trust IronPDF for mission-critical ***PDF generation*:

- **Finance:** Banks *generate millions of statements* and reports monthly using IronPDF's secure document features
- **Healthcare:** Hospitals ***create patient records*** and lab results with HIPAA-compliant security settings
- **E-commerce:** Online retailers produce invoices and shipping labels at scale, handling peak loads effortlessly
- **Government:** Agencies ***generate official documents*** and forms with digital signatures and encryption

These organizations choose IronPDF because it delivers consistent results at scale - whether *generating* a single invoice or processing millions of documents daily.

## How Does IronPDF Compare to Other C&#35; PDF Libraries?

Choosing the right **PDF library** is crucial for your project's success when you need to ***generate PDFs in C#***. Let's look at how IronPDF compares to other popular options in the **.NET ecosystem** for **PDF creation**. This comparison is based on real-world usage, developer feedback, and technical capabilities for those looking to **build PDFs in .NET**. Understanding these differences helps you choose the best **C# PDF generator** for your specific needs.

### Comparison Table

<div class='comparison-platform-pricing-table'>
<div class="card">
<div class="scroll">
<table id="t">
<colgroup>
  <col class="c0"/>
  <col class="c1"/>
  <col class="cx"/><col class="cx"/><col class="cx"/>
  <col class="cx"/><col class="cx"/><col class="cx"/>
  <col class="cx"/><col class="cx"/><col class="cx"/>
  <col class="cx"/>
</colgroup>
<thead id="thead"></thead>
<tbody id="tbody"></tbody>
</table>
</div>
</div>

</div>

### 详细比较

#### IronPDF 对比 wkhtmltopdf

- wkhtmltopdf 是免费的,但已于 2020 年停止维护,生成的 PDF 效果也较为过时
- 需要使用特定平台的可执行文件,增加了部署的复杂性
- 不支持 JavaScript,意味着现代 Web 应用无法正确渲染
- IronPDF 提供现代化渲染,且无需任何外部依赖

#### IronPDF 对比 QuestPDF

- QuestPDF 需要完全通过 C# 代码构建 PDF,不支持 HTML
- 适合以编程方式创建 PDF,但处理复杂布局时较为耗时
- 近期已从 MIT 许可证改为商业许可
- IronPDF 让您可以直接使用已经掌握的 HTML/CSS 技能

#### IronPDF 对比 iText

- iText 采用 AGPL 许可证,可能会"传染"到您的代码库
- 商业许可证起价为 $1,999,且定价体系复杂
- HTML 转 PDF 功能有限,对 CSS 的支持也较差
- IronPDF 以更低的价格提供更出色的 HTML 渲染效果

#### IronPDF 对比 PDFSharp

- PDFSharp 非常适合底层 PDF 操作,但完全不支持 HTML
- 需要手动定位页面上的每一个元素
- 免费且开源,但功能非常有限
- IronPDF 同时支持高层级的 HTML 处理和底层的 PDF 操作

#### IronPDF 对比 Syncfusion/Aspose.PDF

- 两者都是功能出色的企业级方案,但价格较高
- Syncfusion 起价为 $2,995,Aspose 起价为 $2,499
- 两者都无法实现与 IronPDF 相同的像素级精确 HTML 渲染效果
- IronPDF 在提供同等企业级功能的同时,性价比更高

### 迁移方案

许多开发者从其他库迁移到 IronPDF,原因如下:

#### 从 wkhtmltopdf 迁移

- "我们已经厌倦了处理特定平台的二进制文件和过时的渲染效果"
- "IronPDF 为我们提供了现代化的 CSS 支持,还解决了我们在 Docker 方面的困扰"

#### 从 iText/iText 7 迁移

- "陡峭的学习曲线严重影响了我们的生产效率——IronPDF 让我们可以直接使用 HTML"
- "AGPL 许可证对我们的商业产品来说是不可接受的"

#### 从 PDFSharp 迁移

- "我们需要将 HTML 转换为 PDF 的功能,而 PDFSharp 根本无法做到这一点"
- "手动定位每一个元素实在太费时间了"

#### 从 QuestPDF 迁移

- "与使用 HTML/CSS 相比,用 C# 代码构建布局非常繁琐"
- "近期的许可证变更让我们重新考虑了自己的选择"

## 结论

***在 C# 中创建 PDF*** 无需变得复杂。借助 IronPDF,您可以运用自己已经掌握的 HTML 和 CSS 技能来**生成专业的 PDF 文档**。无论您要构建的是简单的报告,还是包含图表和表单的复杂文档,IronPDF 都能替您完成繁重的工作,让您**专注于应用程序逻辑**。加入全球 **1400 万开发者**的行列,将 IronPDF 作为值得信赖的 **C# PDF 生成器**,可靠且高效地***生成 PDF***。

在本指南中,我们探讨了多种***创建 PDF 文档***的方法——从 HTML 字符串和 URL,到转换 Word 文档、Markdown 等现有文件。我们看到了 IronPDF 的**现代化 Chromium 渲染引擎**如何生成**像素级精确的结果**,让效果真正呈现出您的网页设计,而非过时的打印输出样式。以编程方式**处理 PDF 文档**、添加安全功能并优化性能的能力,让 IronPDF 成为您在 .NET 中完成各种***PDF 生成任务***的**完整解决方案**。

IronPDF 的与众不同之处在于其**以开发者为先的理念**。仅需三行代码,您就可以***生成第一份 PDF***。直观的 API 意味着您可以**减少学习**专有 PDF 语法的时间,将**更多时间用于构建功能**。再加上来自[真实工程师](#live-chat-support)的**优质支持**、**透明的定价**,以及**持续的更新**(包括对 .NET 10 的预发行版本支持),IronPDF 让您对自己的***C# PDF 创建***能力充满信心,无论现在还是未来都能稳定运行。

即刻开始***创建 PDF***——**[获取您的免费试用许可证](trial-license)**,体验在您的应用程序中进行***.NET PDF 生成***是多么轻松。使用 IronPDF,您将在几分钟内(而非几小时)***生成专业的 PDF***。

**准备好构建您的第一份 PDF 了吗?** **[立即开始使用 IronPDF](trial-license)**——开发环境免费使用,几分钟内您就能***用 C# 创建 PDF***。

请注意Aspose、iText、wkhtmltopdf、QuestPDF、PDFSharp 和 Syncfusion 均为其各自所有者的注册商标。本网站与 Aspose、iText、wkhtmltopdf、QuestPDF、PDFSharp 或 Syncfusion 均无关联,也未获得其认可或赞助。所有产品名称、标志和品牌均为其各自所有者的财产。相关比较仅供参考,且基于撰写时可公开获取的信息。

using IronPDF;

// Other libraries might require this:
// document.Add(new Paragraph("Hello World"));
// document.Add(new Table(3, 2));
// cell.SetBackgroundColor(ColorConstants.LIGHT_GRAY);

// With IronPDF, just use HTML:
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
    <h1>Hello World</h1>
    <table>
        <tr style='background: lightgray;'>
            <td>Simple</td>
            <td>Intuitive</td>
        </tr>
    </table>
");

### Enterprise-Ready Features

IronPDF includes features that **enterprise applications demand**, which is why Fortune 500 companies rely on it for mission-critical *document generation*:

- **Security:** Encryption, digital signatures, and permission controls protect sensitive documents
- **Compliance:** [PDF/A](/how-to/pdfa/) and [PDF/UA](/how-to/pdfua/) support ensures your *generated PDFs* meet regulatory requirements
- **Performance:** Async operations and batch processing handle millions of documents efficiently
- **Reliability:** Extensive error handling and logging help maintain uptime in production

### Outstanding Support

When you need help, IronPDF's support team consists of **actual engineers** who understand your challenges. With **24/7 live chat support** and typical response times **under 30 seconds**, you're never stuck waiting for answers. This level of support is why developers consistently rate IronPDF as having the best support in the industry. The support team can help with everything from basic questions to complex implementation challenges, ensuring your *PDF generation* projects succeed.

### Transparent Pricing

**No hidden fees**, **no surprise costs**, **no per-server licensing complications**. IronPDF's straightforward licensing model means you know exactly what you're paying for. Development is **always free**, and production licenses are **perpetual** - you own them forever. This transparency is refreshing in an industry known for complex licensing schemes.

### Active Development

IronPDF is **continuously improved** with monthly updates that add features, enhance performance, and ensure compatibility with the latest .NET releases. The team actively monitors customer feedback and implements requested features regularly. Recent additions include [enhanced form handling](/examples/form-data/), improved [PDF editing capabilities](/examples/editing-pdfs/), and optimizations for cloud deployments.

### Real-World Success Stories

**Thousands of companies** across industries trust IronPDF for mission-critical ***PDF generation*:

- **Finance:** Banks *generate millions of statements* and reports monthly using IronPDF's secure document features
- **Healthcare:** Hospitals ***create patient records*** and lab results with HIPAA-compliant security settings
- **E-commerce:** Online retailers produce invoices and shipping labels at scale, handling peak loads effortlessly
- **Government:** Agencies ***generate official documents*** and forms with digital signatures and encryption

These organizations choose IronPDF because it delivers consistent results at scale - whether *generating* a single invoice or processing millions of documents daily.

## How Does IronPDF Compare to Other C&#35; PDF Libraries?

Choosing the right **PDF library** is crucial for your project's success when you need to ***generate PDFs in C#***. Let's look at how IronPDF compares to other popular options in the **.NET ecosystem** for **PDF creation**. This comparison is based on real-world usage, developer feedback, and technical capabilities for those looking to **build PDFs in .NET**. Understanding these differences helps you choose the best **C# PDF generator** for your specific needs.

### Comparison Table

<div class='comparison-platform-pricing-table'>
<div class="card">
<div class="scroll">
<table id="t">
<colgroup>
  <col class="c0"/>
  <col class="c1"/>
  <col class="cx"/><col class="cx"/><col class="cx"/>
  <col class="cx"/><col class="cx"/><col class="cx"/>
  <col class="cx"/><col class="cx"/><col class="cx"/>
  <col class="cx"/>
</colgroup>
<thead id="thead"></thead>
<tbody id="tbody"></tbody>
</table>
</div>
</div>

</div>

### 详细比较

#### IronPDF 对比 wkhtmltopdf

- wkhtmltopdf 是免费的,但已于 2020 年停止维护,生成的 PDF 效果也较为过时
- 需要使用特定平台的可执行文件,增加了部署的复杂性
- 不支持 JavaScript,意味着现代 Web 应用无法正确渲染
- IronPDF 提供现代化渲染,且无需任何外部依赖

#### IronPDF 对比 QuestPDF

- QuestPDF 需要完全通过 C# 代码构建 PDF,不支持 HTML
- 适合以编程方式创建 PDF,但处理复杂布局时较为耗时
- 近期已从 MIT 许可证改为商业许可
- IronPDF 让您可以直接使用已经掌握的 HTML/CSS 技能

#### IronPDF 对比 iText

- iText 采用 AGPL 许可证,可能会"传染"到您的代码库
- 商业许可证起价为 $1,999,且定价体系复杂
- HTML 转 PDF 功能有限,对 CSS 的支持也较差
- IronPDF 以更低的价格提供更出色的 HTML 渲染效果

#### IronPDF 对比 PDFSharp

- PDFSharp 非常适合底层 PDF 操作,但完全不支持 HTML
- 需要手动定位页面上的每一个元素
- 免费且开源,但功能非常有限
- IronPDF 同时支持高层级的 HTML 处理和底层的 PDF 操作

#### IronPDF 对比 Syncfusion/Aspose.PDF

- 两者都是功能出色的企业级方案,但价格较高
- Syncfusion 起价为 $2,995,Aspose 起价为 $2,499
- 两者都无法实现与 IronPDF 相同的像素级精确 HTML 渲染效果
- IronPDF 在提供同等企业级功能的同时,性价比更高

### 迁移方案

许多开发者从其他库迁移到 IronPDF,原因如下:

#### 从 wkhtmltopdf 迁移

- "我们已经厌倦了处理特定平台的二进制文件和过时的渲染效果"
- "IronPDF 为我们提供了现代化的 CSS 支持,还解决了我们在 Docker 方面的困扰"

#### 从 iText/iText 7 迁移

- "陡峭的学习曲线严重影响了我们的生产效率——IronPDF 让我们可以直接使用 HTML"
- "AGPL 许可证对我们的商业产品来说是不可接受的"

#### 从 PDFSharp 迁移

- "我们需要将 HTML 转换为 PDF 的功能,而 PDFSharp 根本无法做到这一点"
- "手动定位每一个元素实在太费时间了"

#### 从 QuestPDF 迁移

- "与使用 HTML/CSS 相比,用 C# 代码构建布局非常繁琐"
- "近期的许可证变更让我们重新考虑了自己的选择"

## 结论

***在 C# 中创建 PDF*** 无需变得复杂。借助 IronPDF,您可以运用自己已经掌握的 HTML 和 CSS 技能来**生成专业的 PDF 文档**。无论您要构建的是简单的报告,还是包含图表和表单的复杂文档,IronPDF 都能替您完成繁重的工作,让您**专注于应用程序逻辑**。加入全球 **1400 万开发者**的行列,将 IronPDF 作为值得信赖的 **C# PDF 生成器**,可靠且高效地***生成 PDF***。

在本指南中,我们探讨了多种***创建 PDF 文档***的方法——从 HTML 字符串和 URL,到转换 Word 文档、Markdown 等现有文件。我们看到了 IronPDF 的**现代化 Chromium 渲染引擎**如何生成**像素级精确的结果**,让效果真正呈现出您的网页设计,而非过时的打印输出样式。以编程方式**处理 PDF 文档**、添加安全功能并优化性能的能力,让 IronPDF 成为您在 .NET 中完成各种***PDF 生成任务***的**完整解决方案**。

IronPDF 的与众不同之处在于其**以开发者为先的理念**。仅需三行代码,您就可以***生成第一份 PDF***。直观的 API 意味着您可以**减少学习**专有 PDF 语法的时间,将**更多时间用于构建功能**。再加上来自[真实工程师](#live-chat-support)的**优质支持**、**透明的定价**,以及**持续的更新**(包括对 .NET 10 的预发行版本支持),IronPDF 让您对自己的***C# PDF 创建***能力充满信心,无论现在还是未来都能稳定运行。

即刻开始***创建 PDF***——**[获取您的免费试用许可证](trial-license)**,体验在您的应用程序中进行***.NET PDF 生成***是多么轻松。使用 IronPDF,您将在几分钟内(而非几小时)***生成专业的 PDF***。

**准备好构建您的第一份 PDF 了吗?** **[立即开始使用 IronPDF](trial-license)**——开发环境免费使用,几分钟内您就能***用 C# 创建 PDF***。

请注意Aspose、iText、wkhtmltopdf、QuestPDF、PDFSharp 和 Syncfusion 均为其各自所有者的注册商标。本网站与 Aspose、iText、wkhtmltopdf、QuestPDF、PDFSharp 或 Syncfusion 均无关联,也未获得其认可或赞助。所有产品名称、标志和品牌均为其各自所有者的财产。相关比较仅供参考,且基于撰写时可公开获取的信息。
Imports IronPDF

' Other libraries might require this:
' document.Add(New Paragraph("Hello World"))
' document.Add(New Table(3, 2))
' cell.SetBackgroundColor(ColorConstants.LIGHT_GRAY)

' With IronPDF, just use HTML:
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("
    <h1>Hello World</h1>
    <table>
        <tr style='background: lightgray;'>
            <td>Simple</td>
            <td>Intuitive</td>
        </tr>
    </table>
")
$vbLabelText   $csharpLabel

常见问题解答

我如何在 C# 中从 HTML 内容创建 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法在 C# 中从 HTML 内容创建 PDF。这允许您轻松将 HTML 字符串或 URL 直接转换为 PDF 文档。

使用商业 PDF 库比免费库有什么优势?

像 IronPDF 这样的商业 PDF 库提供强大的功能,例如全面支持 JavaScript、CSS3 和响应式布局。它们提供可靠的性能、定期更新、全面的支持,并针对创建高质量 PDF 进行了优化。

IronPDF 可以用于在 ASP.NET MVC 应用程序中生成 PDF 吗?

是的,IronPDF 可以用于 ASP.NET MVC 应用程序中,将 Razor 视图或 HTML 模板转换为 PDF,让您可以无缝集成 PDF 生成功能到 Web 应用程序中。

如何使用 C# 将图像和 CSS 集成到 PDF 文档中?

using IronPDF,您可以轻松将图像和 CSS 集成到 PDF 文档中。这可以通过在转换为 PDF 之前在 HTML 内容中包含图像标签和 CSS 样式来实现。

是否可以在 C# 中向 PDF 添加页眉、页脚和页码?

是的,IronPDF 提供高级功能,允许您向 PDF 文档添加页眉、页脚和页码。这可以通过在渲染 HTML 内容之前配置 PDF 设置来完成。

在生成 PDF 时如何处理 C# 中的 XML 数据?

using IronPDF,您可以通过将 XML 转换为 HTML 或使用 XSLT 样式化 XML,然后使用 IronPDF 的 RenderHtmlAsPdf 方法将其转换为 PDF 文档来处理 XML 数据。

在 C# 中生成 PDF 的常见挑战是什么?

常见的挑战包括保持布局一致性、处理复杂的 CSS 和 JavaScript,以及确保准确渲染 Web 技术。IronPDF 通过其现代的 Chromium 引擎和对 HTML5 和 CSS3 标准的广泛支持解决了这些挑战。

如何使用 C# 高效生成大型 PDF?

IronPDF 设计用于高效处理大型 PDF 生成。它使用高性能渲染引擎并支持异步操作,以轻松管理大型文档。

我可以在没有商业许可证的情况下测试 IronPDF 吗?

是的,IronPDF 提供一个免费的开发和测试许可证,允许您在购买生产用途的商业许可证之前评估其功能。

.NET 10 兼容性:我可以在 .NET 10 中使用 IronPDF 吗?有什么特殊注意事项吗?

是的,IronPDF 完全兼容 .NET 10。它开箱即用,支持包括面向 Windows、Linux、容器化环境和 Web 框架的项目在内的所有 .NET 10 环境。无需任何特殊配置——只需安装最新的 IronPDF NuGet 包,即可与 .NET 10 无缝协作。

Jacob Mellor,Team Iron 的首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。

Jacob 拥有曼彻斯特大学土木工程一级荣誉工程学士学位(BEng)(1998-2001 年)。他的旗舰产品 IronPDF 和 Iron Suite for .NET 库在全球的 NuGet 安装量已超过 3000 万次,其基础代码继续为全球使用的开发人员工具提供动力。Jacob 拥有 25 年的商业经验和 41 年的编码专业知识,他一直专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。

钢铁支援团队

我们每周 5 天,每天 24 小时在线。
聊天
电子邮件
打电话给我