如何在 .NET Core 中使用 IronPDF 生成 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內容——僅列印、查看、編輯和矢量化預先存在的PDF。
-
僅限Windows列印: 捆綁於Windows列印基礎設施,限制了跨平台可用性。
-
沒有HTML/URL到PDF的API: 沒有
WebPageToPdfConverter類——這些功能於API介面中不存在。 -
操作有限: 基本合併/拆分/提取可通過
PdfPrintDocument獲得; 沒有水印或現代內容創作。 -
沒有全面的文字提取介面。
- 沒有表單填寫或展平功能。
PDFPrinting.NET與IronPDF的比較
| 功能 | PDFPrinting.NET | IronPDF |
|---|---|---|
| 主要功能 | 靜默PDF列印 | 全週期處理(建立、編輯、列印) |
| 平台支持 | 僅限於Windows | 跨平台 |
| PDF建立/操縱能力 | 否 | 是 |
| HTML到PDF轉換 | 否 | 是 |
| 適合自動化工作流程 | 高 | 高 |
| 額外依賴 | 依賴Windows列印機 | 內部瀏覽器引擎進行渲染 |
| 靜默打?? | 是 | 是 |
| 文字提取 | 不支持 | 支援 |
| 授權 | 商業 | 商業 |
IronPDF通過處理PDF操作的完整生命週期來呈現一個更加全面的解決方案。 它促進PDF文件的建立、編輯、轉換和列印,通過統一的API為開發人員提供全套功能。 與PDFPrinting.NET不同,IronPDF可以部署在不同的平台上,這使得它成為一個能夠在多種環境操作的應用程式的多功能選擇。
對於現代.NET的團隊,IronPDF提供了一套完整的PDF解決方案,可在Windows、Linux和macOS環境下工作。
開始之前
前提條件
- .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
之後(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的PdfDocument允許您在儲存前進行操作(加水印、合併、安全)。 通過使用基於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
之後(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使用一個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
之後(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使用HtmlFragment屬性,允許使用CSS進行豐富的樣式設計。 佔位符如{total-pages}在渲染時被替換。
關鍵遷移注意事項
標題/頁腳佔位符是IronPDF獨有的功能
PDFPrinting.NET完全沒有標題或頁腳建立API,因此沒有佔位符語法可供遷移。 IronPDF在{total-pages}佔位符:
//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()
生成然後列印工作流程
使用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) - 將
PdfDocument.FromFile(path).Print()模式 - 將每個列印屬性(
Copies,Duplex,Collate,PrintSettings物件上 - 採用新功能:使用
ChromePdfRenderer.RenderHtmlAsPdf/RenderUrlAsPdf處理HTML和URL輸入(PDFPrinting.NET中沒有等價項) - 通過
HtmlFooter配置標題/頁腳(新功能) - 在應用程式啟動時設置
IronPdf.License.LicenseKey
遷移後
- 在所有目標平台上測試列印
- 驗證頁眉/頁腳的渲染
- 考慮為動態文件新增PDF生成
- 根據需要新增新功能(合併,水印,安全性)

