如何使用 IronPDF 在 .NET Core 中生成 PDF 文件
从 PDFPrinting.NET 迁移到IronPDF可将您的 PDF 功能从仅用于打印的库扩展到可处理整个 PDF 生命周期(包括创建、操作、提取、安全和打印)的综合解决方案。 本指南提供了完整的分步迁移路径,在保留现有打印工作流程的同时,增加了 PDF 生成、HTML 转换和跨平台支持功能。
为什么要从 PDFPrinting.NET 迁移到 IronPDF?
了解 PDFPrinting.NET.
PDFPrinting.NET (Terminalworks; NuGet PdfPrintingNet) 是一个专注于在Windows环境中静默打印PDF的专用库。 作为一个致力于静默打印现有PDF的工具,它简化了将文件以编程方式调度到打印机而不需要用户干预的任务。 新的入口点是 PdfPrint 类; 旧代码可能仍然引用遗留的 TerminalWorks.PDFPrinting.PDFPrinter。
其核心优势是静默打印——绕过通常的打印对话窗口,以便全自动工作流可以无人值守地运行。
关注打印的限制
PDFPrinting.NET专注于一小撮操作,并不撰写新的PDF内容。 其范围包括打印、查看、基本编辑和栅格化现有文档:
-
关注打印:不撰写新的PDF内容——仅打印、查看、编辑和栅格化已有的PDFs。
-
仅限于Windows打印:受限于Windows打印基础设施,限制了跨平台的可用性。
-
无HTML/URL到PDF API:没有
HtmlToPdfConverter和没有WebPageToPdfConverter类 — 这些功能在API表面不存在。 -
有限操作:基本的合并/分割/提取是通过
PdfPrintDocument实现的; 没有水印或现代内容创作。 -
没有全面的文本提取表面。
- 没有表单填写或平铺。
PDFPrinting.NET 与IronPDF的比较
| 特征 | PDFPrinting.NET | IronPDF |
|---|---|---|
| 主要功能 | 静音 PDF 打印 | 全周期处理(创建、编辑、打印) |
| 平台支持 | 仅限 Windows | 跨平台 |
| PDF 创建/操纵功能 | 否 | 是 |
| HTML-PDF 转换 | 否 | 是 |
| 自动化工作流程的适用性 | 高的 | 高的 |
| 附加依赖项 | 依赖于 Windows 打印机 | 用于渲染的内部浏览器引擎 |
| 静音打印 | 是 | 是 |
| 文本提取 | 不支持 | 支持 |
| 许可 | 商业翻译 | 商业翻译 |
IronPDF 通过处理 PDF 的整个生命周期,提供了一个更全面的解决方案。 该工具便于创建、编辑、转换和打印 PDF 文档,通过统一的 API 为开发人员提供全套功能。 与 PDFPrinting.NET 不同,IronPDF 可在不同平台上部署,因此是在不同环境中运行的应用程序的多功能选择。
对于使用现代.NET的团队,IronPDF提供了一个适用于Windows, Linux和macOS环境的完整的PDF解决方案。
开始之前
前提条件
- .NET 环境: .NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet 访问权限:能够安装 NuGet 包
- IronPDF 许可证:请从ironpdf.com获取您的许可证密钥。
NuGet 软件包变更
# Remove PDFPrinting.NET (the actual NuGet ID is PdfPrintingNet)
dotnet remove package PdfPrintingNet
# Install IronPDF
dotnet add package IronPdf
# Remove PDFPrinting.NET (the actual NuGet ID is PdfPrintingNet)
dotnet remove package PdfPrintingNet
# Install IronPDF
dotnet add package IronPdf
许可配置
// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
' Add at application startup (Program.vb or Startup.vb)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
识别 PDFPrinting.NET 的用法
# Find PDFPrinting.NET usage (newer + legacy namespaces)
grep -rE "PdfPrintingNet|TerminalWorks\.PDFPrinting|PdfPrint\b|PdfPrintDocument|PDFPrinter" --include="*.cs" .
# Find print-related code
grep -r "\.Print(\|PrinterName\|GetPrintDocument" --include="*.cs" .
# Find PDFPrinting.NET usage (newer + legacy namespaces)
grep -rE "PdfPrintingNet|TerminalWorks\.PDFPrinting|PdfPrint\b|PdfPrintDocument|PDFPrinter" --include="*.cs" .
# Find print-related code
grep -r "\.Print(\|PrinterName\|GetPrintDocument" --include="*.cs" .
完整的 API 参考
命名空间变更
// Before: PDFPrinting.NET
// Newer API:
using PdfPrintingNet;
// Older code may use:
using TerminalWorks.PDFPrinting;
// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Printing;
// Before: PDFPrinting.NET
// Newer API:
using PdfPrintingNet;
// Older code may use:
using TerminalWorks.PDFPrinting;
// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Printing;
' Before: PDFPrinting.NET
' Newer API:
Imports PdfPrintingNet
' Older code may use:
Imports TerminalWorks.PDFPrinting
' After: IronPDF
Imports IronPdf
Imports IronPdf.Rendering
Imports IronPdf.Printing
核心类映射
| PDFPrinting.NET | IronPDF |
|---|---|
PdfPrint (新的) / PDFPrinter (遗留的) |
PdfDocument + Print() |
PdfPrintDocument |
PdfDocument |
| (没有HTML到PDF类存在) | ChromePdfRenderer.RenderHtmlAsPdf |
| (没有URL到PDF类存在) | ChromePdfRenderer.RenderUrlAsPdf |
| 打印设置属性 | PrintSettings |
打印方法映射
| PDFPrinting.NET | IronPDF |
|---|---|
pdfPrint.Print(filePath) |
pdf.Print() |
pdfPrint.PrinterName = "..."; pdfPrint.Print(path) |
pdf.Print(printerName) |
new PdfPrintDocument(...) |
pdf.GetPrintDocument() |
pdfPrint.Copies = n |
printSettings.NumberOfCopies = n |
pdfPrint.Duplex = true |
printSettings.DuplexMode = Duplex.Vertical |
pdfPrint.Collate = true |
printSettings.Collate = true |
PDFPrinting.NET 中不提供的新功能
| IronPDF 特点 | 说明 |
|---|---|
renderer.RenderHtmlAsPdf(html) |
HTML 到 PDF 的转换 |
renderer.RenderUrlAsPdf(url) |
URL 到 PDF 的转换 |
PdfDocument.Merge(pdfs) |
合并多个 PDF |
pdf.ApplyWatermark(html) |
添加水印 |
pdf.SecuritySettings.UserPassword |
密码保护 |
pdf.ExtractAllText() |
文本提取 |
代码迁移示例
示例 1:HTML 到 PDF 的转换
之前 (PDFPrinting.NET): PDFPrinting.NET 没有 HTML 到 PDF API — 没有 HtmlToPdfConverter 类。 最接近的工作流程是用另一个库生成PDF,然后打印或交付文件:
// NuGet: Install-Package PdfPrintingNet
using PdfPrintingNet;
using System;
class Program
{
static void Main()
{
// Step 1: Produce the PDF with another library (PDFPrinting.NET cannot).
// Step 2: Print the existing PDF file.
var pdfPrint = new PdfPrint("license-owner", "license-key");
var status = pdfPrint.Print("output.pdf");
Console.WriteLine($"Printed: {status}");
}
}
// NuGet: Install-Package PdfPrintingNet
using PdfPrintingNet;
using System;
class Program
{
static void Main()
{
// Step 1: Produce the PDF with another library (PDFPrinting.NET cannot).
// Step 2: Print the existing PDF file.
var pdfPrint = new PdfPrint("license-owner", "license-key");
var status = pdfPrint.Print("output.pdf");
Console.WriteLine($"Printed: {status}");
}
}
Imports PdfPrintingNet
Imports System
Class Program
Shared Sub Main()
' Step 1: Produce the PDF with another library (PDFPrinting.NET cannot).
' Step 2: Print the existing PDF file.
Dim pdfPrint = New PdfPrint("license-owner", "license-key")
Dim status = pdfPrint.Print("output.pdf")
Console.WriteLine($"Printed: {status}")
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
Console.WriteLine("PDF created successfully")
End Sub
End Class
因为PDFPrinting.NET根本不能撰写PDF,迁移不是一对一的类替换——而是在采用一种新能力。IronPDF的 ChromePdfRenderer.RenderHtmlAsPdf() 返回一个可以操作的 PdfDocument (水印、合并、安全性) 在使用 SaveAs() 保存之前。 通过利用基于Chromium的引擎,IronPDF能够高保真地复制现代CSS和JavaScript的渲染。 请参阅 HTML 转 PDF 文档,了解全面的示例。
示例 2:URL 到 PDF 的转换
之前 (PDFPrinting.NET): 没有 WebPageToPdfConverter 类 — PDFPrinting.NET 不下载或渲染网页。 必须使用一个单独的库首先将URL捕获为PDF;然后,PDFPrinting.NET可以打印生成的文件:
// NuGet: Install-Package PdfPrintingNet
using PdfPrintingNet;
using System;
class Program
{
static void Main()
{
// Step 1: Capture the URL with another library (PDFPrinting.NET cannot).
// Step 2: Print the resulting PDF.
var pdfPrint = new PdfPrint("license-owner", "license-key");
var status = pdfPrint.Print("webpage.pdf");
Console.WriteLine($"Printed: {status}");
}
}
// NuGet: Install-Package PdfPrintingNet
using PdfPrintingNet;
using System;
class Program
{
static void Main()
{
// Step 1: Capture the URL with another library (PDFPrinting.NET cannot).
// Step 2: Print the resulting PDF.
var pdfPrint = new PdfPrint("license-owner", "license-key");
var status = pdfPrint.Print("webpage.pdf");
Console.WriteLine($"Printed: {status}");
}
}
Imports PdfPrintingNet
Imports System
Class Program
Shared Sub Main()
' Step 1: Capture the URL with another library (PDFPrinting.NET cannot).
' Step 2: Print the resulting PDF.
Dim pdfPrint As New PdfPrint("license-owner", "license-key")
Dim status = pdfPrint.Print("webpage.pdf")
Console.WriteLine($"Printed: {status}")
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string url = "https://www.example.com";
var pdf = renderer.RenderUrlAsPdf(url);
pdf.SaveAs("webpage.pdf");
Console.WriteLine("PDF from URL created successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string url = "https://www.example.com";
var pdf = renderer.RenderUrlAsPdf(url);
pdf.SaveAs("webpage.pdf");
Console.WriteLine("PDF from URL created successfully");
}
}
Imports IronPdf
Imports System
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
Dim url As String = "https://www.example.com"
Dim pdf = renderer.RenderUrlAsPdf(url)
pdf.SaveAs("webpage.pdf")
Console.WriteLine("PDF from URL created successfully")
End Sub
End Module
IronPDF 使用单个 ChromePdfRenderer 类处理 HTML 字符串和 URL,RenderUrlAsPdf() 在一次调用中处理网页捕获。 在我们的教程中了解更多信息。
示例 3:页眉和页脚
之前(PDFPrinting.NET):PDFPrinting.NET无法创建页眉或页脚——它不从HTML或其他源生成PDF,也不提供页眉/页脚组成API。 如果您的PDF已经包含了它们,该库可以打印它:
// NuGet: Install-Package PdfPrintingNet
using PdfPrintingNet;
using System;
class Program
{
static void Main()
{
// Headers/footers must already be baked into the PDF.
var pdfPrint = new PdfPrint("license-owner", "license-key");
pdfPrint.Print("report.pdf");
Console.WriteLine("PDF with pre-existing headers/footers printed");
}
}
// NuGet: Install-Package PdfPrintingNet
using PdfPrintingNet;
using System;
class Program
{
static void Main()
{
// Headers/footers must already be baked into the PDF.
var pdfPrint = new PdfPrint("license-owner", "license-key");
pdfPrint.Print("report.pdf");
Console.WriteLine("PDF with pre-existing headers/footers printed");
}
}
Imports PdfPrintingNet
Imports System
Module Program
Sub Main()
' Headers/footers must already be baked into the PDF.
Dim pdfPrint As New PdfPrint("license-owner", "license-key")
pdfPrint.Print("report.pdf")
Console.WriteLine("PDF with pre-existing headers/footers printed")
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:center'>Company Report</div>"
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:center'>Page {page} of {total-pages}</div>"
};
string html = "<html><body><h1>Document Content</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("report.pdf");
Console.WriteLine("PDF with headers/footers created");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:center'>Company Report</div>"
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:center'>Page {page} of {total-pages}</div>"
};
string html = "<html><body><h1>Document Content</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("report.pdf");
Console.WriteLine("PDF with headers/footers created");
}
}
Imports IronPdf
Imports IronPdf.Rendering
Imports System
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {
.HtmlFragment = "<div style='text-align:center'>Company Report</div>"
}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {
.HtmlFragment = "<div style='text-align:center'>Page {page} of {total-pages}</div>"
}
Dim html As String = "<html><body><h1>Document Content</h1></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("report.pdf")
Console.WriteLine("PDF with headers/footers created")
End Sub
End Module
IronPDF 使用 HtmlHeaderFooter 对象,具有一个接受完整 HTML 的 HtmlFragment 属性,允许用 CSS 进行丰富的样式化。 占位符如 {page} 和 {total-pages} 在渲染时被替换。
关键迁移说明
页眉/页脚占位符是IronPDF的独有功能
PDFPrinting.NET根本没有页眉或页脚创建API,因此没有占位符语法需要迁移。IronPDF支持 {page} 和 {total-pages} 占位符在 HtmlHeaderFooter.HtmlFragment 内:
//IronPDFplaceholders
"Page {page} of {total-pages}"
//IronPDFplaceholders
"Page {page} of {total-pages}"
加载然后打印模式
PDFPrinting.NET 直接将文件路径传递给 Print(); IronPDF首先加载文档:
// PDFPrinting.NET: Direct path to Print()
pdfPrint.Print("document.pdf");
// IronPDF: Load first, then operate
var pdf = PdfDocument.FromFile("document.pdf");
pdf.Print();
// PDFPrinting.NET: Direct path to Print()
pdfPrint.Print("document.pdf");
// IronPDF: Load first, then operate
var pdf = PdfDocument.FromFile("document.pdf");
pdf.Print();
' PDFPrinting.NET: Direct path to Print()
pdfPrint.Print("document.pdf")
' IronPDF: Load first, then operate
Dim pdf = PdfDocument.FromFile("document.pdf")
pdf.Print()
打印设置迁移
PDFPrinting.NET 使用 PdfPrint 上的属性;IronPDF使用一个设置对象:
// PDFPrinting.NET: Properties on PdfPrint
pdfPrint.Copies = 2;
pdfPrint.Duplex = true;
// IronPDF: Settings object
var settings = new PrintSettings
{
NumberOfCopies = 2,
DuplexMode = System.Drawing.Printing.Duplex.Vertical
};
pdf.Print(settings);
// PDFPrinting.NET: Properties on PdfPrint
pdfPrint.Copies = 2;
pdfPrint.Duplex = true;
// IronPDF: Settings object
var settings = new PrintSettings
{
NumberOfCopies = 2,
DuplexMode = System.Drawing.Printing.Duplex.Vertical
};
pdf.Print(settings);
' PDFPrinting.NET: Properties on PdfPrint
pdfPrint.Copies = 2
pdfPrint.Duplex = True
' IronPDF: Settings object
Dim settings As New PrintSettings With {
.NumberOfCopies = 2,
.DuplexMode = System.Drawing.Printing.Duplex.Vertical
}
pdf.Print(settings)
迁移后的新功能
迁移到IronPDF后,您将获得 PDFPrinting.NET 无法提供的功能:
PDF 合并
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
Dim pdf1 = PdfDocument.FromFile("document1.pdf")
Dim pdf2 = PdfDocument.FromFile("document2.pdf")
Dim merged = PdfDocument.Merge(pdf1, pdf2)
merged.SaveAs("merged.pdf")
水印
pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>");
pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>");
pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>")
密码保护
pdf.SecuritySettings.UserPassword = "userpassword";
pdf.SecuritySettings.OwnerPassword = "ownerpassword";
pdf.SecuritySettings.UserPassword = "userpassword";
pdf.SecuritySettings.OwnerPassword = "ownerpassword";
pdf.SecuritySettings.UserPassword = "userpassword"
pdf.SecuritySettings.OwnerPassword = "ownerpassword"
文本提取
string text = pdf.ExtractAllText();
string text = pdf.ExtractAllText();
Dim text As String = pdf.ExtractAllText()
生成然后打印工作流程
using IronPDF,您可以在一个工作流程中生成 PDF 并打印:
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Invoice</h1>");
pdf.Print("Invoice Printer");
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Invoice</h1>");
pdf.Print("Invoice Printer");
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Invoice</h1>")
pdf.Print("Invoice Printer")
跨平台打印
PDFPrinting.NET 仅适用于 Windows。IronPDF可跨平台运行:
Windows
pdf.Print("HP LaserJet");
pdf.Print("HP LaserJet");
pdf.Print("HP LaserJet")
Linux
// Requires CUPS (Common Unix Printing System)
// Install: apt-get install cups
pdf.Print("HP_LaserJet"); // CUPS uses underscores instead of spaces
// Requires CUPS (Common Unix Printing System)
// Install: apt-get install cups
pdf.Print("HP_LaserJet"); // CUPS uses underscores instead of spaces
macOS
pdf.Print("HP LaserJet");
pdf.Print("HP LaserJet");
pdf.Print("HP LaserJet")
功能对比摘要
| 特征 | PDFPrinting.NET | IronPDF |
|---|---|---|
| 无声印刷 | ✓ | ✓ |
| 打印设置 | ✓ | ✓ |
| HTML 至 PDF | ✗ | ✓ |
| URL 至 PDF | ✗ | ✓ |
| 页眉/页脚 | 基本的 | 完整的 HTML |
| 合并 PDF | ✗ | ✓ |
| 拆分 PDF | ✗ | ✓ |
| 水印 | ✗ | ✓ |
| 文本提取 | ✗ | ✓ |
| 密码保护 | ✗ | ✓ |
| 跨平台 | ✗ | ✓ |
迁移清单
迁移前
- 清点代码库中所有 PDFPrinting.NET 的使用情况
- 记录当前使用的所有打印机名称
- 请注意所有打印设置配置
- 确定是否需要跨平台支持
- 规划IronPDF许可证密钥存储(建议使用环境变量)
- 先使用IronPDF试用许可证进行测试
软件包变更
- 删除
PdfPrintingNetNuGet 包 - 安装
IronPdfNuGet 包:dotnet add package IronPdf
代码更改
- 更新命名空间导入 (
PdfPrintingNet/ 遗留TerminalWorks.PDFPrinting→IronPdf) - 将
pdfPrint.Print(path)调用转换为PdfDocument.FromFile(path).Print()模式 - 将每次打印的属性 (
Copies,Duplex,Collate,PrinterName) 移动到PrintSettings对象 - 采用新功能:使用
ChromePdfRenderer.RenderHtmlAsPdf/RenderUrlAsPdf处理 HTML 和 URL 输入 (PDFPrinting.NET 中不存在等效功能) - 通过
RenderingOptions.HtmlHeader和HtmlFooter配置页眉/页脚 (新功能) - 在应用程序启动时设置
IronPdf.License.LicenseKey
后迁移
- 在所有目标平台上进行测试打印
- 验证页眉/页脚渲染效果
- 考虑为动态文档添加 PDF 生成功能
- 根据需要添加新功能(合并、水印、安全)。

