如何用 C# 从 PDFFilePrint 迁移到 IronPDF
从PDFFilePrint迁移到IronPDF可将您的.NET PDF工作流从仅打印的Pdfium包装器移至一个全面的PDF库,该库通过单一统一的API处理创建、操作和打印。 本指南提供了一个逐步迁移路径,将PrinterSettings,同时增加了PDF生成和操作功能,这些功能是PDFFilePrint不提供的。
为什么要从PDF 文件打印迁移到 IronPDF?
了解 PDFFilePrint
PDFFilePrint是一个小型开源NuGet包(MIT,由Christian Andersen编写),它包装了PdfiumViewer和谷歌的Pdfium以静默地将现有的PDF或XPS文件发送到打印机驱动。 最新发布是1.0.3,发布于2020-02-10,目标是.NET Framework 4.6.1+,在Windows上。 虽然对特定的打印任务有用,但其功能仅限于文档处理的一个方面——公共界面基本上是由app.config / Properties.Settings.Default键驱动的。
PDF 文件打印的关键限制
-
仅限打印: 无法创建、编辑、合并或修改PDF——
FilePrint类使用现有的PDF/XPS文件并将它们传递给打印后台处理程序。 -
配置文件驱动的API: 打印机名称、纸张尺寸、副本数量和"打印到文件"模式从
Properties.Settings.Default)读取,而不是作为强类型选项对象传递。 -
仅支持Windows + .NET Framework: 目标是
net461并且引入PdfiumViewer的Win32本机二进制文件。 没有.NET Core / .NET 6+ TFM,也没有Linux/macOS支持。 -
有限的打印旋钮:双面打印、页面范围、方向和颜色依赖打印机驱动的默认值——没有一流的API供它们使用。
-
基本上不再维护:自1.0.3(2020年2月)以来没有发布,并且没有从NuGet列表链接的公共源代码库。
-
简单异常表面:失败从PdfiumViewer/后台处理程序以通用异常泡沫化而不是类型化错误层次。
- 没有PDF生成:无法创建PDFs——要从HTML、URL或图像到打印页,必须将PDFFilePrint与其他渲染器配对。
PDF 文件打印与IronPDF对比
| 方面 | PDF 文件打印 | IronPDF |
|---|---|---|
| 主要关注点 | PDF/XPS打印 | 全面的 PDF API |
| 类型 | Pdfium打印包装 | 本机.NET库+Chromium渲染器 |
| 集成 | new FilePrint(...).Print() + app.config |
直接PdfDocument API |
| PDF 打印 | 是 | 是 |
| PDF 创建 | 否 | 是 (HTML, URL, 图像) |
| PDF 操作 | 否 | 是 (合并, 分割, 编辑) |
| 跨平台 | 仅限Windows (net461) | Windows、Linux、macOS、Docker |
| 错误处理 | 来自PdfiumViewer的普通异常 | 类型化IronPdfException |
| 智能感知 | 最小的 (小表面) | 满的 |
| NuGet软件包 | PDFFilePrint 1.0.3 (MIT, 2020年2月) |
IronPdf |
| 最后发布 | 1.0.3 (2020年2月) | 活跃版本 |
对于目标为现代.NET (Framework 4.6.2+ 和 .NET 6/7/8/9/10)的团队,IronPDF 提供了跨平台支持和积极开发的综合基础,解决了PDFFilePrint的架构限制。
开始之前
前提条件
- .NET 环境: .NET Framework 4.6.2+ 或 .NET 6/7/8/9/10
- NuGet 访问权限:能够安装 NuGet 包
- IronPDF 许可证:请从ironpdf.com获取您的许可证密钥。
NuGet 软件包变更
# RemovePDF 文件打印package
dotnet remove package PDFFilePrint
# Install IronPDF
dotnet add package IronPdf
# RemovePDF 文件打印package
dotnet remove package PDFFilePrint
# 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"
确定PDF 文件打印的用法
# FindPDF 文件打印API usages
grep -r "using PDFFilePrint\|new FilePrint" --include="*.cs" .
# Find related app.config keys
grep -r "PrinterName\|PaperName\|PrintToFile\|DefaultPrintToDirectory" \
--include="*.config" --include="*.settings" .
# FindPDF 文件打印API usages
grep -r "using PDFFilePrint\|new FilePrint" --include="*.cs" .
# Find related app.config keys
grep -r "PrinterName\|PaperName\|PrintToFile\|DefaultPrintToDirectory" \
--include="*.config" --include="*.settings" .
完整的 API 参考
命名空间变更
// PDFFilePrint
using PDFFilePrint;
//IronPDF— printing flows through System.Drawing.Printing
using IronPdf;
using System.Drawing.Printing;
// PDFFilePrint
using PDFFilePrint;
//IronPDF— printing flows through System.Drawing.Printing
using IronPdf;
using System.Drawing.Printing;
Imports PDFFilePrint
Imports IronPdf
Imports System.Drawing.Printing
核心类映射
| PDF 文件打印 | IronPDF |
|---|---|
new FilePrint(path, null) (加载) |
PdfDocument.FromFile(path) |
new ChromePdfRenderer() (无等效项 — PDFFilePrint无法创建PDF) |
new ChromePdfRenderer() |
FilePrint |
PdfDocument |
PDF 生成方法映射
| PDF 文件打印 | IronPDF |
|---|---|
| (不可用)_ | renderer.RenderHtmlAsPdf(html) |
| (不可用)_ | renderer.RenderUrlAsPdf(url) |
| (不可用 — 只打印) | pdf.SaveAs(path) |
PDF 加载和打印映射
| PDF 文件打印 | IronPDF |
|---|---|
new FilePrint(path, null) |
PdfDocument.FromFile(path) |
fileprint.Print() (静音, 总是) |
pdf.Print() (静音) / pdf.Print(true) (对话框) |
Properties.Settings.Default.PrinterName = "..." 然后 Print() |
pdf.GetPrintDocument(new PrinterSettings { PrinterName = "..." }).Print() |
打印设置映射 (app.config到PrinterSettings)
PDFFilePrint设置 (Settings.Default) |
IronPDF(System.Drawing.Printing.PrinterSettings) |
|---|---|
PrinterName |
PrinterName |
Copies |
Copies |
| (无静音标志 — 始终静音) | pdf.Print() (静音) / pdf.Print(true) (显示对话框) |
| (驱动程序默认) | FromPage, ToPage, PrintRange |
| (驱动程序默认) | DefaultPageSettings.Landscape |
| (驱动程序默认) | Duplex |
| (驱动程序默认) | Collate |
PaperName |
DefaultPageSettings.PaperSize |
PrintToFile + DefaultPrintToDirectory |
PrintToFile + PrintFileName |
PDF 文件打印中没有的新功能
| IronPDF 特点 | 说明 |
|---|---|
PdfDocument.Merge() |
合并多个 PDF |
pdf.CopyPages() |
提取特定页面 |
pdf.ApplyWatermark() |
添加水印 |
pdf.SecuritySettings |
密码保护 |
pdf.ExtractAllText() |
提取文本内容 |
pdf.RasterizeToImageFiles() |
转换为图像 |
pdf.SignWithDigitalSignature() |
数字签名 |
代码迁移示例
示例 1:HTML 到 PDF 的转换
之前(PDFFilePrint):
// NuGet: Install-Package PDFFilePrint
//PDF 文件打印is a print-only wrapper around PdfiumViewer / Pdfium.
// Its FilePrint class only consumes existing PDF/XPS files — there is
// no CreateFromHtml or document-creation method.
using System;
class Program
{
static void Main()
{
throw new NotSupportedException(
"PDFFilePrint cannot convert HTML to PDF. Render the HTML to a PDF " +
"with another library, then pass the path to new FilePrint(path, null).Print().");
}
}
// NuGet: Install-Package PDFFilePrint
//PDF 文件打印is a print-only wrapper around PdfiumViewer / Pdfium.
// Its FilePrint class only consumes existing PDF/XPS files — there is
// no CreateFromHtml or document-creation method.
using System;
class Program
{
static void Main()
{
throw new NotSupportedException(
"PDFFilePrint cannot convert HTML to PDF. Render the HTML to a PDF " +
"with another library, then pass the path to new FilePrint(path, null).Print().");
}
}
Imports System
Class Program
Shared Sub Main()
Throw New NotSupportedException("PDFFilePrint cannot convert HTML to PDF. Render the HTML to a PDF " & _
"with another library, then pass the path to new FilePrint(path, null).Print().")
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string htmlContent = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string htmlContent = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim htmlContent As String = "<html><body><h1>Hello World</h1></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("output.pdf")
End Sub
End Class
这里的根本区别在于范围。 PDFFilePrint没有HTML-to-PDF路径——其公共界面是一个FilePrint类,将现有的PDF传递给打印机。 IronPDF将渲染与文档对象分开:SaveAs()保存。
这种分离还允许您在转换之前在渲染器上配置渲染选项,并在保存之前操作返回的PdfDocument(添加水印,与其他PDF合并,添加安全性)。 有关其他渲染选项,请参阅 HTML to PDF 文档。
示例 2:URL 到 PDF 的转换
之前(PDFFilePrint):
// NuGet: Install-Package PDFFilePrint
//PDF 文件打印cannot fetch a URL or render HTML — it only sends existing
// PDF/XPS files to a printer. URL-to-PDF requires a separate renderer.
using System;
class Program
{
static void Main()
{
throw new NotSupportedException(
"PDFFilePrint cannot convert URLs to PDF. Render the URL to a PDF with " +
"another library first, then call new FilePrint(pdfPath, null).Print().");
}
}
// NuGet: Install-Package PDFFilePrint
//PDF 文件打印cannot fetch a URL or render HTML — it only sends existing
// PDF/XPS files to a printer. URL-to-PDF requires a separate renderer.
using System;
class Program
{
static void Main()
{
throw new NotSupportedException(
"PDFFilePrint cannot convert URLs to PDF. Render the URL to a PDF with " +
"another library first, then call new FilePrint(pdfPath, null).Print().");
}
}
Imports System
Class Program
Shared Sub Main()
Throw New NotSupportedException("PDFFilePrint cannot convert URLs to PDF. Render the URL to a PDF with " & _
"another library first, then call new FilePrint(pdfPath, null).Print().")
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("webpage.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("webpage.pdf");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://www.example.com")
pdf.SaveAs("webpage.pdf")
End Sub
End Class
PDFFilePrint 没有 URL 获取功能。 IronPDF使用专用的ChromePdfRenderer上,该方法利用了Chromium引擎来渲染现代CSS、JavaScript和Web功能。 了解有关 URL 至 PDF 转换的更多信息。
示例 3:PDF 打印
之前(PDFFilePrint):
// NuGet: Install-Package PDFFilePrint
using System;
using PDFFilePrint;
class Program
{
static void Main()
{
// Optional: override the configured printer / copy count at runtime.
Properties.Settings.Default.PrinterName = "Default Printer";
var fileprint = new FilePrint("document.pdf", null);
fileprint.Print();
Console.WriteLine("PDF sent to printer");
}
}
// NuGet: Install-Package PDFFilePrint
using System;
using PDFFilePrint;
class Program
{
static void Main()
{
// Optional: override the configured printer / copy count at runtime.
Properties.Settings.Default.PrinterName = "Default Printer";
var fileprint = new FilePrint("document.pdf", null);
fileprint.Print();
Console.WriteLine("PDF sent to printer");
}
}
Imports System
Imports PDFFilePrint
Module Program
Sub Main()
' Optional: override the configured printer / copy count at runtime.
Properties.Settings.[Default].PrinterName = "Default Printer"
Dim fileprint = New FilePrint("document.pdf", Nothing)
fileprint.Print()
Console.WriteLine("PDF sent to printer")
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Drawing.Printing;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
// Silent print to the default printer
pdf.Print();
// Or print to a named printer with explicit PrinterSettings:
// var settings = new PrinterSettings { PrinterName = "HP LaserJet Pro" };
// pdf.GetPrintDocument(settings).Print();
Console.WriteLine("PDF sent to printer");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Drawing.Printing;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
// Silent print to the default printer
pdf.Print();
// Or print to a named printer with explicit PrinterSettings:
// var settings = new PrinterSettings { PrinterName = "HP LaserJet Pro" };
// pdf.GetPrintDocument(settings).Print();
Console.WriteLine("PDF sent to printer");
}
}
Imports IronPdf
Imports System
Imports System.Drawing.Printing
Module Program
Sub Main()
Dim pdf = PdfDocument.FromFile("document.pdf")
' Silent print to the default printer
pdf.Print()
' Or print to a named printer with explicit PrinterSettings:
' Dim settings As New PrinterSettings With {.PrinterName = "HP LaserJet Pro"}
' pdf.GetPrintDocument(settings).Print()
Console.WriteLine("PDF sent to printer")
End Sub
End Module
此示例显示了两个库之间的架构区别。 PDFFilePrint实例化Properties.Settings.Default)读取。 IronPDF使用静态工厂System.Drawing.Printing.PrinterSettings进行显式控制。
关键的迁移变化:
new FilePrint(path, null)→PdfDocument.FromFile(path)Properties.Settings.Default.PrinterName = "..."+Print()→pdf.GetPrintDocument(new PrinterSettings { PrinterName = "..." }).Print()- 默认打印机:在
PrinterName→ 调用pdf.Print()
对于高级打印设置,IronPDF使用System.Drawing.Printing.PrinterSettings。 请参阅打印文档以获取其他选项。
高级打印设置迁移
对于使用PDFFilePrint的PrinterSettings:
//PDF 文件打印approach — settings live in app.config and are read via
// Properties.Settings.Default. To override at runtime you mutate Settings.Default
// before instantiating FilePrint:
// Properties.Settings.Default.PrinterName = "HP LaserJet";
// Properties.Settings.Default.Copies = 3;
// new FilePrint("document.pdf", null).Print();
//
//IronPDFequivalent — pass a System.Drawing.Printing.PrinterSettings per call:
using IronPdf;
using System.Drawing.Printing;
var pdf = PdfDocument.FromFile("document.pdf");
var settings = new PrinterSettings
{
PrinterName = "HP LaserJet",
Copies = 3,
FromPage = 1,
ToPage = 5,
PrintRange = PrintRange.SomePages
};
pdf.GetPrintDocument(settings).Print();
//PDF 文件打印approach — settings live in app.config and are read via
// Properties.Settings.Default. To override at runtime you mutate Settings.Default
// before instantiating FilePrint:
// Properties.Settings.Default.PrinterName = "HP LaserJet";
// Properties.Settings.Default.Copies = 3;
// new FilePrint("document.pdf", null).Print();
//
//IronPDFequivalent — pass a System.Drawing.Printing.PrinterSettings per call:
using IronPdf;
using System.Drawing.Printing;
var pdf = PdfDocument.FromFile("document.pdf");
var settings = new PrinterSettings
{
PrinterName = "HP LaserJet",
Copies = 3,
FromPage = 1,
ToPage = 5,
PrintRange = PrintRange.SomePages
};
pdf.GetPrintDocument(settings).Print();
Imports IronPdf
Imports System.Drawing.Printing
Dim pdf = PdfDocument.FromFile("document.pdf")
Dim settings As New PrinterSettings With {
.PrinterName = "HP LaserJet",
.Copies = 3,
.FromPage = 1,
.ToPage = 5,
.PrintRange = PrintRange.SomePages
}
pdf.GetPrintDocument(settings).Print()
静音模式
//PDF 文件打印is always silent — there is no print-dialog code path.
//IronPDFis silent by default. Pass true explicitly to show the print dialog:
pdf.Print(); // silent
pdf.Print(true); // shows print dialog
//PDF 文件打印is always silent — there is no print-dialog code path.
//IronPDFis silent by default. Pass true explicitly to show the print dialog:
pdf.Print(); // silent
pdf.Print(true); // shows print dialog
' PDF 文件打印is always silent — there is no print-dialog code path.
' IronPDFis silent by default. Pass true explicitly to show the print dialog:
pdf.Print() ' silent
pdf.Print(True) ' shows print dialog
迁移后的新功能
迁移到IronPDF后,您将获得PDF 文件打印无法提供的功能:
创建和打印一步到位
using IronPdf;
using System.Drawing.Printing;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Invoice #12345</h1><p>Thank you for your order.</p>");
var settings = new PrinterSettings { PrinterName = "Office Printer" };
pdf.GetPrintDocument(settings).Print();
using IronPdf;
using System.Drawing.Printing;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Invoice #12345</h1><p>Thank you for your order.</p>");
var settings = new PrinterSettings { PrinterName = "Office Printer" };
pdf.GetPrintDocument(settings).Print();
Imports IronPdf
Imports System.Drawing.Printing
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Invoice #12345</h1><p>Thank you for your order.</p>")
Dim settings As New PrinterSettings With {.PrinterName = "Office Printer"}
pdf.GetPrintDocument(settings).Print()
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")
水印
var pdf = PdfDocument.FromFile("document.pdf");
pdf.ApplyWatermark("<h1 style='color:red; opacity:0.3;'>CONFIDENTIAL</h1>");
pdf.SaveAs("watermarked.pdf");
var pdf = PdfDocument.FromFile("document.pdf");
pdf.ApplyWatermark("<h1 style='color:red; opacity:0.3;'>CONFIDENTIAL</h1>");
pdf.SaveAs("watermarked.pdf");
Dim pdf = PdfDocument.FromFile("document.pdf")
pdf.ApplyWatermark("<h1 style='color:red; opacity:0.3;'>CONFIDENTIAL</h1>")
pdf.SaveAs("watermarked.pdf")
密码保护
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SecuritySettings.UserPassword = "userpassword";
pdf.SecuritySettings.OwnerPassword = "ownerpassword";
pdf.SaveAs("secured.pdf");
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SecuritySettings.UserPassword = "userpassword";
pdf.SecuritySettings.OwnerPassword = "ownerpassword";
pdf.SaveAs("secured.pdf");
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SecuritySettings.UserPassword = "userpassword"
pdf.SecuritySettings.OwnerPassword = "ownerpassword"
pdf.SaveAs("secured.pdf")
文本提取
var pdf = PdfDocument.FromFile("document.pdf");
string text = pdf.ExtractAllText();
var pdf = PdfDocument.FromFile("document.pdf");
string text = pdf.ExtractAllText();
Dim pdf = PdfDocument.FromFile("document.pdf")
Dim text As String = pdf.ExtractAllText()
关键迁移说明
类模式更改
PDFFilePrint使用一个小的Print()方法。IronPDF将渲染、文档操作和打印分成不同类型:
// PDFFilePrint: load + silent print (settings come from app.config)
Properties.Settings.Default.PrinterName = "HP LaserJet";
var fileprint = new FilePrint(path, null);
fileprint.Print();
// IronPDF: PdfDocument for load/manipulate, PrinterSettings for print options
var pdf = PdfDocument.FromFile(path);
var settings = new PrinterSettings { PrinterName = "HP LaserJet" };
pdf.GetPrintDocument(settings).Print();
//IronPDFcan also create PDFs from HTML —PDF 文件打印cannot
var renderer = new ChromePdfRenderer();
var newPdf = renderer.RenderHtmlAsPdf(html);
newPdf.SaveAs(path);
// PDFFilePrint: load + silent print (settings come from app.config)
Properties.Settings.Default.PrinterName = "HP LaserJet";
var fileprint = new FilePrint(path, null);
fileprint.Print();
// IronPDF: PdfDocument for load/manipulate, PrinterSettings for print options
var pdf = PdfDocument.FromFile(path);
var settings = new PrinterSettings { PrinterName = "HP LaserJet" };
pdf.GetPrintDocument(settings).Print();
//IronPDFcan also create PDFs from HTML —PDF 文件打印cannot
var renderer = new ChromePdfRenderer();
var newPdf = renderer.RenderHtmlAsPdf(html);
newPdf.SaveAs(path);
' PDFFilePrint: load + silent print (settings come from app.config)
Properties.Settings.Default.PrinterName = "HP LaserJet"
Dim fileprint = New FilePrint(path, Nothing)
fileprint.Print()
' IronPDF: PdfDocument for load/manipulate, PrinterSettings for print options
Dim pdf = PdfDocument.FromFile(path)
Dim settings = New PrinterSettings With {.PrinterName = "HP LaserJet"}
pdf.GetPrintDocument(settings).Print()
' IronPDF can also create PDFs from HTML — PDF 文件打印 cannot
Dim renderer = New ChromePdfRenderer()
Dim newPdf = renderer.RenderHtmlAsPdf(html)
newPdf.SaveAs(path)
方法 / 设置命名更改
//PDF 文件打印→ IronPDF
new FilePrint(path, null) → PdfDocument.FromFile(path)
fileprint.Print() → pdf.Print() // silent default
Properties.Settings.Default.PrinterName = "X" → new PrinterSettings { PrinterName = "X" }
Properties.Settings.Default.Copies = 3 → new PrinterSettings { Copies = 3 }
Properties.Settings.Default.PaperName = "A4" → settings.DefaultPageSettings.PaperSize = ...
//PDF 文件打印has no native HTML/URL/merge/watermark methods — these are IronPDF-only.
//PDF 文件打印→ IronPDF
new FilePrint(path, null) → PdfDocument.FromFile(path)
fileprint.Print() → pdf.Print() // silent default
Properties.Settings.Default.PrinterName = "X" → new PrinterSettings { PrinterName = "X" }
Properties.Settings.Default.Copies = 3 → new PrinterSettings { Copies = 3 }
Properties.Settings.Default.PaperName = "A4" → settings.DefaultPageSettings.PaperSize = ...
//PDF 文件打印has no native HTML/URL/merge/watermark methods — these are IronPDF-only.
异常处理
// PDFFilePrint: failures bubble up from PdfiumViewer as plain exceptions
try {
new FilePrint(path, null).Print();
}
catch (Exception ex) {
// 否 granular error type — log the message and inner exception.
}
// IronPDF: typed exception hierarchy
try {
pdf.Print();
}
catch (IronPdf.Exceptions.IronPdfException ex) {
// Granular error details
}
// PDFFilePrint: failures bubble up from PdfiumViewer as plain exceptions
try {
new FilePrint(path, null).Print();
}
catch (Exception ex) {
// 否 granular error type — log the message and inner exception.
}
// IronPDF: typed exception hierarchy
try {
pdf.Print();
}
catch (IronPdf.Exceptions.IronPdfException ex) {
// Granular error details
}
Imports IronPdf.Exceptions
' PDFFilePrint: failures bubble up from PdfiumViewer as plain exceptions
Try
Dim filePrint As New FilePrint(path, Nothing)
filePrint.Print()
Catch ex As Exception
' 否 granular error type — log the message and inner exception.
End Try
' IronPDF: typed exception hierarchy
Try
pdf.Print()
Catch ex As IronPdfException
' Granular error details
End Try
无外部可执行文件
两个库都作为NuGet包提供。 无IronPdf包自包含,本机依赖项在恢复时解决。
功能对比摘要
| 特征 | PDF 文件打印 | IronPDF |
|---|---|---|
| 基本印刷 | ✓ | ✓ |
| 静音打印 | ✓ (始终静音) | ✓ |
| 一式多份 | ✓ (通过app.config) | ✓ |
| 页面范围 | 仅限驱动程序默认 | ✓ |
| 双工 | 仅限驱动程序默认 | ✓ |
| 从 HTML 创建 | ✗ | ✓ |
| 从 URL 创建 | ✗ | ✓ |
| 合并 PDF | ✗ | ✓ |
| 拆分 PDF | ✗ | ✓ |
| 添加水印 | ✗ | ✓ |
| 提取文本 | ✗ | ✓ |
| 密码保护 | ✗ | ✓ |
| 数字签名 | ✗ | ✓ |
| 跨平台 | ✗ (Windows / net461) | ✓ |
| 本地 .NET 应用程序接口 | ✓ (小表面) | ✓ |
迁移清单
迁移前
- 定位所有
new FilePrint(...)调用点 - 记录当前
PrinterName,PaperName,Copies,PrintToFile,DefaultPrintToDirectory) - 识别不同环境中使用的打印机名称
- 注意任何在运行时改变
Properties.Settings.Default的代码 - 识别新功能的机会 (HTML/URL 渲染,合并,水印,安全性)
- 获取IronPDF许可证密钥
软件包变更
- 移除
PDFFilePrintNuGet包:dotnet remove package PDFFilePrint - 安装
IronPdfNuGet包:dotnet add package IronPdf - 将
using System.Drawing.Printing;)
代码更改
- 在启动时添加许可证密钥配置
- 将
PdfDocument.FromFile(path) - 将打印机/副本/纸张设置从
System.Drawing.Printing.PrinterSettings中 - 使用
pdf.GetPrintDocument(settings).Print()进行显式选项 - 在您之前捕获通用异常的地方,将
try/catch IronPdfException中 - 对于HTML/URL/合并/水印功能(PDFFilePrint从未提供),使用
PdfDocument操作API
后迁移
- 从
app.config中移除PDFFilePrint特定键 - 在所有目标打印机上进行测试打印
- 适用时进行跨平台测试(Linux 打印需要CUPS)
- 根据需要添加新功能(水印,安全性,合并)
- 更新文档

