如何從 Apache PDFBox 轉移到 IronPDF
Apache PDFBox 是一個備受推崇的開源 Java 程式庫,用於處理 PDF 檔案。 然而,對於 .NET 開發人員來說,可用的選項是非官方的社群驅動移植版本,這些版本帶來了重大挑戰——Java 風格的 API、不完整的功能覆蓋範圍以及有限的 .NET 社群支援。 本指南提供了從 Apache PDFBox .NET 移植版到 IronPDF(專為 .NET 生態系統建置的原生 .NET PDF 函式庫)的詳細遷移路徑。
為什麼要考慮從 Apache PDFBox .NET 連接埠遷移?
雖然 Apache PDFBox 在 Java 生態系統中表現出色,但其非官方的 .NET 移植版本卻為 .NET 開發團隊帶來了一些挑戰。
非官方埠狀態
Apache PDFBox 主要是一個 Java 函式庫。 所有 .NET 版本均為社群驅動的移植版本,缺乏 Apache 專案的官方支援。 這些移植版本通常落後於 Java 版本,可能會錯過關鍵功能、錯誤修復或安全性更新。 對於建置應用程式的團隊而言,其長期使用需求可延長至 2025 年和 2026 年,這種不確定性會造成技術風險。
Java-First API 設計
移植的 API 保留了在 .NET 程式碼中感覺陌生的 Java 慣例。 開發人員會遇到 camelCase 方法而不是 PascalCase,Java File 物件而不是標準的 .NET 字串,以及明確的 close() 呼叫而不是 @@--CODE-16716--@@ 模式。 這種認知負擔會影響開發速度和程式碼可維護性。
無 HTML 渲染能力
Apache PDFBox 專為 PDF 操作而設計,而非 HTML 至 PDF 的轉換。 建立 PDF 需要以手動方式建立頁面,並進行精確的座標定位 - 這個過程既乏味又容易出錯,無法滿足現代文件產生的需求。
有限的 .NET 社群支援
圍繞 Apache PDFBox 連接埠的 .NET 生態系統非常稀少。 與擁有活躍的 .NET 社群的圖書館相比,尋找針對 .NET 特定問題的說明、範例或最佳實務相當困難。
潛在的 JVM 相依性
某些 Apache PDFBox 移植可能需要 Java 執行時元件,增加了以 .NET 為重點的基礎架構中部署與環境管理的複雜性。
Apache PDFBox vs. IronPDF:主要差異
瞭解這些程式庫的基本差異有助於規劃有效的遷移策略。
| 範疇 | Apache PDFBox .NET 連接埠 | IronPDF |
|---|---|---|
| 原生設計 | 以 Java 為中心,非官方的 .NET 移植 | 原生 .NET,專業支援 |
| API 風格 | Java 約定(camelCase, close()) |
慣用 C# 碼 (PascalCase, using) |
| HTML 渲染 | 不支援 (手動建頁) | 完全基於 Chromium 的 HTML/CSS/JS |
| PDF製作 | 手動坐標定位 | 基於 CSS 的版面設計 |
| 社群 | Java 為主,.NET 資源稀少 | 活躍的 .NET 社群,下載量超過 10M |
| <強>支援</強 | 僅限社群 | 提供專業支援 |
| 資源清理 | 明確 close() 調用 |
IDisposable 帶有 using 語句 |
遷移前的準備工作
先決條件
確保您的環境符合這些要求:
- .NET Framework 4.6.2+ 或 .NET Core 3.1 / .NET 5-9
- Visual Studio 2019+ 或 JetBrains Rider
- NuGet 套件管理員存取權限 -IronPDF授權金鑰 (可於 ironpdf.com 網站免費試用)
審核 Apache PDFBox 的使用情況
在您的解決方案目錄中執行這些指令,以識別所有 Apache PDFBox 參考資料:
grep -r "apache.pdfbox\|PdfBox\|PDDocument\|PDFTextStripper" --include="*.cs" .
grep -r "PdfBox\|Apache.PdfBox" --include="*.csproj" .
grep -r "apache.pdfbox\|PdfBox\|PDDocument\|PDFTextStripper" --include="*.cs" .
grep -r "PdfBox\|Apache.PdfBox" --include="*.csproj" .
可預期的重大變更
| 類別 | Apache PDFBox .NET 移植 | IronPDF | 遷移行動 |
|---|---|---|---|
| 物件模型 | PDDocument, PDPage |
PdfDocument, ChromePdfRenderer |
不同的類別層級 |
| PDF 製作 | 手動頁面/內容串流 | HTML 渲染 | 重寫建立邏輯 |
| 方法風格 | camelCase()(Java 風格) |
PascalCase() (.NET 風格) |
更新方法名稱 |
| 資源清理 | document.close() |
using 語句 |
變更處理模式 |
| 檔案存取 | Java 物件 | 標準 .NET 字串/流程 | 使用 .NET 類型 |
| 文字擷取 | PDFTextStripper 類 |
pdf.ExtractAllText() |
更簡單的 API |
逐步遷移的過程
步驟 1:更新 NuGet 套件
移除 Apache PDFBox .NET port 套件並安裝 IronPDF:
# Remove PDFBox .NET port packages
dotnet remove package PdfBox
dotnet remove package PDFBoxNet
dotnet remove package Apache.PdfBox
# Install IronPDF
dotnet add package IronPdf
# Remove PDFBox .NET port packages
dotnet remove package PdfBox
dotnet remove package PDFBoxNet
dotnet remove package Apache.PdfBox
# Install IronPDF
dotnet add package IronPdf
步驟 2:配置授權金鑰
在應用程式啟動時加入IronPDF授權金鑰:
// Add at application startup, before anyIronPDFoperations
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Add at application startup, before anyIronPDFoperations
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
' Add at application startup, before any IronPDF operations
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
步驟 3:更新命名空間參考資料
在您的解決方案中執行全局搜尋與取代:
| 尋找 | 取代 |
|---|---|
using org.apache.pdfbox.pdmodel; |
using IronPdf; |
using org.apache.pdfbox.text; |
using IronPdf; |
using org.apache.pdfbox.multipdf; |
using IronPdf; |
using PdfBoxDotNet.Pdmodel; |
using IronPdf; |
using Apache.Pdfbox.PdModel; |
using IronPdf; |
完整的 API 遷移參考。
文件操作
| Apache PDFBox 方法 | IronPDF 方法 |
|---|---|
PDDocument.load(path) |
PdfDocument.FromFile(path) |
PDDocument.load(stream) |
PdfDocument.FromStream(stream) |
new PDDocument() |
new ChromePdfRenderer() |
document.save(path) |
pdf.SaveAs(path) |
document.close() |
using 語句或 Dispose() |
document.getNumberOfPages() |
pdf.PageCount |
document.getPage(index) |
pdf.Pages[index] |
document.removePage(index) |
pdf.RemovePages(index) |
文字萃取
| Apache PDFBox 方法 | IronPDF 方法 |
|---|---|
new PDFTextStripper() |
不需要 |
stripper.getText(document) |
pdf.ExtractAllText() |
stripper.setStartPage(n) |
pdf.Pages[n].Text |
stripper.setSortByPosition(true) |
自動化 |
合併與分割作業
| Apache PDFBox 方法 | IronPDF 方法 |
|---|---|
new PDFMergerUtility() |
不需要 |
merger.addSource(file) |
載入 FromFile() |
merger.mergeDocuments() |
PdfDocument.Merge(pdfs) |
new Splitter() |
不需要 |
splitter.split(document) |
pdf.CopyPages(indices) |
安全與加密
| Apache PDFBox 方法 | IronPDF 方法 |
|---|---|
StandardProtectionPolicy |
pdf.SecuritySettings |
policy.setUserPassword() |
pdf.SecuritySettings.UserPassword |
policy.setOwnerPassword() |
pdf.SecuritySettings.OwnerPassword |
policy.setPermissions() |
pdf.SecuritySettings.AllowUserXxx |
程式碼遷移範例
文字萃取
最常見的 Apache PDFBox 操作展示了IronPDF提供的 API 簡化。
Apache PDFBox .NET Port 實作:
// Apache PDFBox .NET ports are experimental and incomplete
using PdfBoxDotNet.Pdmodel;
using PdfBoxDotNet.Text;
using System;
using System.IO;
class Program
{
static void Main()
{
// Note: PDFBox-dotnet has limited functionality
using (var document = PDDocument.Load("document.pdf"))
{
var stripper = new PDFTextStripper();
string text = stripper.GetText(document);
Console.WriteLine(text);
}
}
}
// Apache PDFBox .NET ports are experimental and incomplete
using PdfBoxDotNet.Pdmodel;
using PdfBoxDotNet.Text;
using System;
using System.IO;
class Program
{
static void Main()
{
// Note: PDFBox-dotnet has limited functionality
using (var document = PDDocument.Load("document.pdf"))
{
var stripper = new PDFTextStripper();
string text = stripper.GetText(document);
Console.WriteLine(text);
}
}
}
Imports PdfBoxDotNet.Pdmodel
Imports PdfBoxDotNet.Text
Imports System
Imports System.IO
Class Program
Shared Sub Main()
' Note: PDFBox-dotnet has limited functionality
Using document = PDDocument.Load("document.pdf")
Dim stripper = New PDFTextStripper()
Dim text As String = stripper.GetText(document)
Console.WriteLine(text)
End Using
End Sub
End Class
IronPDF 實作:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
string text = pdf.ExtractAllText();
Console.WriteLine(text);
// Or extract text from specific pages
string pageText = pdf.ExtractTextFromPage(0);
Console.WriteLine(pageText);
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
string text = pdf.ExtractAllText();
Console.WriteLine(text);
// Or extract text from specific pages
string pageText = pdf.ExtractTextFromPage(0);
Console.WriteLine(pageText);
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim pdf = PdfDocument.FromFile("document.pdf")
Dim text As String = pdf.ExtractAllText()
Console.WriteLine(text)
' Or extract text from specific pages
Dim pageText As String = pdf.ExtractTextFromPage(0)
Console.WriteLine(pageText)
End Sub
End Class
IronPDF 完全消除了 PDFTextStripper 類,以單一方法呼叫取代了多步驟擷取。
HTML 至 PDF 轉換
Apache PDFBox 並不支援 HTML 至 PDF 的原生轉換 - 這代表著基本能力上的差距。
IronPDF 實作:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is HTML to PDF</p>");
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is HTML to PDF</p>");
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is HTML to PDF</p>")
pdf.SaveAs("output.pdf")
Console.WriteLine("PDF created successfully")
End Sub
End Class
IronPDF 基於 Chromium 的渲染引擎提供完整的 HTML、CSS 和 JavaScript 支援。 如需進階方案,請參閱 HTML to PDF 文件。
合併多個 PDF 文件
Apache PDFBox .NET Port 實作:
// Apache PDFBox .NET port attempt (incomplete support)
using PdfBoxDotNet.Pdmodel;
using PdfBoxDotNet.Multipdf;
using System;
using System.IO;
class Program
{
static void Main()
{
// PDFBox-dotnet ports have incomplete API coverage
var merger = new PDFMergerUtility();
merger.AddSource("document1.pdf");
merger.AddSource("document2.pdf");
merger.SetDestinationFileName("merged.pdf");
merger.MergeDocuments();
Console.WriteLine("PDFs merged");
}
}
// Apache PDFBox .NET port attempt (incomplete support)
using PdfBoxDotNet.Pdmodel;
using PdfBoxDotNet.Multipdf;
using System;
using System.IO;
class Program
{
static void Main()
{
// PDFBox-dotnet ports have incomplete API coverage
var merger = new PDFMergerUtility();
merger.AddSource("document1.pdf");
merger.AddSource("document2.pdf");
merger.SetDestinationFileName("merged.pdf");
merger.MergeDocuments();
Console.WriteLine("PDFs merged");
}
}
Imports PdfBoxDotNet.Pdmodel
Imports PdfBoxDotNet.Multipdf
Imports System
Imports System.IO
Module Program
Sub Main()
' PDFBox-dotnet ports have incomplete API coverage
Dim merger As New PDFMergerUtility()
merger.AddSource("document1.pdf")
merger.AddSource("document2.pdf")
merger.SetDestinationFileName("merged.pdf")
merger.MergeDocuments()
Console.WriteLine("PDFs merged")
End Sub
End Module
IronPDF 實作:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var pdf3 = PdfDocument.FromFile("document3.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2, pdf3);
merged.SaveAs("merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var pdf3 = PdfDocument.FromFile("document3.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2, pdf3);
merged.SaveAs("merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Module Program
Sub Main()
Dim pdf1 = PdfDocument.FromFile("document1.pdf")
Dim pdf2 = PdfDocument.FromFile("document2.pdf")
Dim pdf3 = PdfDocument.FromFile("document3.pdf")
Dim merged = PdfDocument.Merge(pdf1, pdf2, pdf3)
merged.SaveAs("merged.pdf")
Console.WriteLine("PDFs merged successfully")
End Sub
End Module
IronPDF 的靜態 Merge 方法直接接受多個文檔,消除了實用程式類別模式。
從零開始創建 PDF
最顯著的差異出現在建立 PDF 時。 Apache PDFBox 需要手動坐標定位。
Apache PDFBox .NET Port 實作:
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.pdmodel.font;
using org.apache.pdfbox.pdmodel.edit;
public void CreatePdf(string outputPath)
{
PDDocument document = new PDDocument();
try
{
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.beginText();
contentStream.setFont(font, 24);
contentStream.moveTextPositionByAmount(72, 700);
contentStream.drawString("Hello World");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.moveTextPositionByAmount(72, 650);
contentStream.drawString("This is a paragraph of text.");
contentStream.endText();
contentStream.close();
document.save(outputPath);
}
finally
{
document.close();
}
}
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.pdmodel.font;
using org.apache.pdfbox.pdmodel.edit;
public void CreatePdf(string outputPath)
{
PDDocument document = new PDDocument();
try
{
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.beginText();
contentStream.setFont(font, 24);
contentStream.moveTextPositionByAmount(72, 700);
contentStream.drawString("Hello World");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.moveTextPositionByAmount(72, 650);
contentStream.drawString("This is a paragraph of text.");
contentStream.endText();
contentStream.close();
document.save(outputPath);
}
finally
{
document.close();
}
}
Imports org.apache.pdfbox.pdmodel
Imports org.apache.pdfbox.pdmodel.font
Imports org.apache.pdfbox.pdmodel.edit
Public Sub CreatePdf(outputPath As String)
Dim document As New PDDocument()
Try
Dim page As New PDPage()
document.addPage(page)
Dim contentStream As New PDPageContentStream(document, page)
Dim font As PDFont = PDType1Font.HELVETICA_BOLD
contentStream.beginText()
contentStream.setFont(font, 24)
contentStream.moveTextPositionByAmount(72, 700)
contentStream.drawString("Hello World")
contentStream.endText()
contentStream.beginText()
contentStream.setFont(PDType1Font.HELVETICA, 12)
contentStream.moveTextPositionByAmount(72, 650)
contentStream.drawString("This is a paragraph of text.")
contentStream.endText()
contentStream.close()
document.save(outputPath)
Finally
document.close()
End Try
End Sub
IronPDF 實作:
using IronPdf;
public void CreatePdf(string outputPath)
{
var renderer = new ChromePdfRenderer();
string html = @"
<html>
<head>
<style>
body { font-family: Helvetica, Arial, sans-serif; margin: 1in; }
h1 { font-size: 24pt; font-weight: bold; }
p { font-size: 12pt; }
</style>
</head>
<body>
<h1>Hello World</h1>
<p>This is a paragraph of text.</p>
</body>
</html>";
using var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs(outputPath);
}
using IronPdf;
public void CreatePdf(string outputPath)
{
var renderer = new ChromePdfRenderer();
string html = @"
<html>
<head>
<style>
body { font-family: Helvetica, Arial, sans-serif; margin: 1in; }
h1 { font-size: 24pt; font-weight: bold; }
p { font-size: 12pt; }
</style>
</head>
<body>
<h1>Hello World</h1>
<p>This is a paragraph of text.</p>
</body>
</html>";
using var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs(outputPath);
}
Imports IronPdf
Public Sub CreatePdf(outputPath As String)
Dim renderer As New ChromePdfRenderer()
Dim html As String = "
<html>
<head>
<style>
body { font-family: Helvetica, Arial, sans-serif; margin: 1in; }
h1 { font-size: 24pt; font-weight: bold; }
p { font-size: 12pt; }
</style>
</head>
<body>
<h1>Hello World</h1>
<p>This is a paragraph of text.</p>
</body>
</html>"
Using pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs(outputPath)
End Using
End Sub
基於 HTML/CSS 的創作省去了座標計算、字型管理和內容串流操作。
新增密碼保護
Apache PDFBox .NET Port 實作:
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.pdmodel.encryption;
public void ProtectPdf(string inputPath, string outputPath, string password)
{
PDDocument document = PDDocument.load(new File(inputPath));
try
{
AccessPermission ap = new AccessPermission();
ap.setCanPrint(true);
ap.setCanExtractContent(false);
StandardProtectionPolicy spp = new StandardProtectionPolicy(password, password, ap);
spp.setEncryptionKeyLength(128);
document.protect(spp);
document.save(outputPath);
}
finally
{
document.close();
}
}
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.pdmodel.encryption;
public void ProtectPdf(string inputPath, string outputPath, string password)
{
PDDocument document = PDDocument.load(new File(inputPath));
try
{
AccessPermission ap = new AccessPermission();
ap.setCanPrint(true);
ap.setCanExtractContent(false);
StandardProtectionPolicy spp = new StandardProtectionPolicy(password, password, ap);
spp.setEncryptionKeyLength(128);
document.protect(spp);
document.save(outputPath);
}
finally
{
document.close();
}
}
Imports org.apache.pdfbox.pdmodel
Imports org.apache.pdfbox.pdmodel.encryption
Public Sub ProtectPdf(inputPath As String, outputPath As String, password As String)
Dim document As PDDocument = PDDocument.load(New File(inputPath))
Try
Dim ap As New AccessPermission()
ap.setCanPrint(True)
ap.setCanExtractContent(False)
Dim spp As New StandardProtectionPolicy(password, password, ap)
spp.setEncryptionKeyLength(128)
document.protect(spp)
document.save(outputPath)
Finally
document.close()
End Try
End Sub
IronPDF 實作:
using IronPdf;
public void ProtectPdf(string inputPath, string outputPath, string password)
{
using var pdf = PdfDocument.FromFile(inputPath);
pdf.SecuritySettings.UserPassword = password;
pdf.SecuritySettings.OwnerPassword = password;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SaveAs(outputPath);
}
using IronPdf;
public void ProtectPdf(string inputPath, string outputPath, string password)
{
using var pdf = PdfDocument.FromFile(inputPath);
pdf.SecuritySettings.UserPassword = password;
pdf.SecuritySettings.OwnerPassword = password;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SaveAs(outputPath);
}
Imports IronPdf
Public Sub ProtectPdf(inputPath As String, outputPath As String, password As String)
Using pdf = PdfDocument.FromFile(inputPath)
pdf.SecuritySettings.UserPassword = password
pdf.SecuritySettings.OwnerPassword = password
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SaveAs(outputPath)
End Using
End Sub
IronPDF 使用強式類型的屬性,而非獨立的權限與政策物件。
新增水印
Apache PDFBox .NET Port 實作:
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.pdmodel.edit;
using org.apache.pdfbox.pdmodel.font;
public void AddWatermark(string inputPath, string outputPath, string watermarkText)
{
PDDocument document = PDDocument.load(new File(inputPath));
try
{
PDFont font = PDType1Font.HELVETICA_BOLD;
for (int i = 0; i < document.getNumberOfPages(); i++)
{
PDPage page = document.getPage(i);
PDPageContentStream cs = new PDPageContentStream(
document, page, PDPageContentStream.AppendMode.APPEND, true, true);
cs.beginText();
cs.setFont(font, 72);
cs.setNonStrokingColor(200, 200, 200);
cs.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(45), 200, 400));
cs.showText(watermarkText);
cs.endText();
cs.close();
}
document.save(outputPath);
}
finally
{
document.close();
}
}
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.pdmodel.edit;
using org.apache.pdfbox.pdmodel.font;
public void AddWatermark(string inputPath, string outputPath, string watermarkText)
{
PDDocument document = PDDocument.load(new File(inputPath));
try
{
PDFont font = PDType1Font.HELVETICA_BOLD;
for (int i = 0; i < document.getNumberOfPages(); i++)
{
PDPage page = document.getPage(i);
PDPageContentStream cs = new PDPageContentStream(
document, page, PDPageContentStream.AppendMode.APPEND, true, true);
cs.beginText();
cs.setFont(font, 72);
cs.setNonStrokingColor(200, 200, 200);
cs.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(45), 200, 400));
cs.showText(watermarkText);
cs.endText();
cs.close();
}
document.save(outputPath);
}
finally
{
document.close();
}
}
Imports org.apache.pdfbox.pdmodel
Imports org.apache.pdfbox.pdmodel.edit
Imports org.apache.pdfbox.pdmodel.font
Public Sub AddWatermark(inputPath As String, outputPath As String, watermarkText As String)
Dim document As PDDocument = PDDocument.load(New File(inputPath))
Try
Dim font As PDFont = PDType1Font.HELVETICA_BOLD
For i As Integer = 0 To document.getNumberOfPages() - 1
Dim page As PDPage = document.getPage(i)
Dim cs As New PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, True, True)
cs.beginText()
cs.setFont(font, 72)
cs.setNonStrokingColor(200, 200, 200)
cs.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(45), 200, 400))
cs.showText(watermarkText)
cs.endText()
cs.close()
Next
document.save(outputPath)
Finally
document.close()
End Try
End Sub
IronPDF 實作:
using IronPdf;
public void AddWatermark(string inputPath, string outputPath, string watermarkText)
{
using var pdf = PdfDocument.FromFile(inputPath);
pdf.ApplyWatermark(
$"<h1 style='color:lightgray;font-size:72px;'>{watermarkText}</h1>",
rotation: 45,
opacity: 50);
pdf.SaveAs(outputPath);
}
using IronPdf;
public void AddWatermark(string inputPath, string outputPath, string watermarkText)
{
using var pdf = PdfDocument.FromFile(inputPath);
pdf.ApplyWatermark(
$"<h1 style='color:lightgray;font-size:72px;'>{watermarkText}</h1>",
rotation: 45,
opacity: 50);
pdf.SaveAs(outputPath);
}
Imports IronPdf
Public Sub AddWatermark(inputPath As String, outputPath As String, watermarkText As String)
Using pdf = PdfDocument.FromFile(inputPath)
pdf.ApplyWatermark(
$"<h1 style='color:lightgray;font-size:72px;'>{watermarkText}</h1>",
rotation:=45,
opacity:=50)
pdf.SaveAs(outputPath)
End Using
End Sub
IronPDF基於 HTML 的浮水印功能消除了頁面迭代和矩陣計算。
URL 轉 PDF
Apache PDFBox 不支援 URL 至 PDF 的轉換。IronPDF提供本機支援:
using IronPdf;
public void ConvertUrlToPdf(string url, string outputPath)
{
var renderer = new ChromePdfRenderer();
using var pdf = renderer.RenderUrlAsPdf(url);
pdf.SaveAs(outputPath);
}
using IronPdf;
public void ConvertUrlToPdf(string url, string outputPath)
{
var renderer = new ChromePdfRenderer();
using var pdf = renderer.RenderUrlAsPdf(url);
pdf.SaveAs(outputPath);
}
Imports IronPdf
Public Sub ConvertUrlToPdf(url As String, outputPath As String)
Dim renderer As New ChromePdfRenderer()
Using pdf = renderer.RenderUrlAsPdf(url)
pdf.SaveAs(outputPath)
End Using
End Sub
如需完整的 URL 轉換選項,請參閱 URL to PDF 文件。
頁首和頁尾
Apache PDFBox 需要在每個頁面上手動定位,沒有內建的頁首/頁尾支援。IronPDF提供宣告式配置:
using IronPdf;
public void CreatePdfWithHeaderFooter(string html, string outputPath)
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
CenterText = "Document Title",
FontSize = 12
};
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
CenterText = "Page {page} of {total-pages}",
FontSize = 10
};
using var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs(outputPath);
}
using IronPdf;
public void CreatePdfWithHeaderFooter(string html, string outputPath)
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
CenterText = "Document Title",
FontSize = 12
};
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
CenterText = "Page {page} of {total-pages}",
FontSize = 10
};
using var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs(outputPath);
}
Imports IronPdf
Public Sub CreatePdfWithHeaderFooter(html As String, outputPath As String)
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.TextHeader = New TextHeaderFooter With {
.CenterText = "Document Title",
.FontSize = 12
}
renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {
.CenterText = "Page {page} of {total-pages}",
.FontSize = 10
}
Using pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs(outputPath)
End Using
End Sub
如需進階佈局,請參閱 headers and footers 文件。
ASP.NET Core 整合。
IronPDF 可與現代 .NET 網路應用程式自然整合:
[HttpPost]
public IActionResult GeneratePdf([FromBody] ReportRequest request)
{
var renderer = new ChromePdfRenderer();
using var pdf = renderer.RenderHtmlAsPdf(request.Html);
return File(pdf.BinaryData, "application/pdf", "report.pdf");
}
[HttpPost]
public IActionResult GeneratePdf([FromBody] ReportRequest request)
{
var renderer = new ChromePdfRenderer();
using var pdf = renderer.RenderHtmlAsPdf(request.Html);
return File(pdf.BinaryData, "application/pdf", "report.pdf");
}
<HttpPost>
Public Function GeneratePdf(<FromBody> request As ReportRequest) As IActionResult
Dim renderer As New ChromePdfRenderer()
Using pdf = renderer.RenderHtmlAsPdf(request.Html)
Return File(pdf.BinaryData, "application/pdf", "report.pdf")
End Using
End Function
同步支援
Apache PDFBox 連接埠不支援 async 作業。IronPDF提供完整的 async/await 功能:
using IronPdf;
public async Task<byte[]> GeneratePdfAsync(string html)
{
var renderer = new ChromePdfRenderer();
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
return pdf.BinaryData;
}
using IronPdf;
public async Task<byte[]> GeneratePdfAsync(string html)
{
var renderer = new ChromePdfRenderer();
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
return pdf.BinaryData;
}
Imports IronPdf
Public Async Function GeneratePdfAsync(html As String) As Task(Of Byte())
Dim renderer As New ChromePdfRenderer()
Using pdf = Await renderer.RenderHtmlAsPdfAsync(html)
Return pdf.BinaryData
End Using
End Function
依賴注入配置
public interface IPdfService
{
Task<byte[]> GeneratePdfAsync(string html);
string ExtractText(string pdfPath);
}
public class IronPdfService : IPdfService
{
private readonly ChromePdfRenderer _renderer;
public IronPdfService()
{
_renderer = new ChromePdfRenderer();
_renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
}
public async Task<byte[]> GeneratePdfAsync(string html)
{
using var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
return pdf.BinaryData;
}
public string ExtractText(string pdfPath)
{
using var pdf = PdfDocument.FromFile(pdfPath);
return pdf.ExtractAllText();
}
}
public interface IPdfService
{
Task<byte[]> GeneratePdfAsync(string html);
string ExtractText(string pdfPath);
}
public class IronPdfService : IPdfService
{
private readonly ChromePdfRenderer _renderer;
public IronPdfService()
{
_renderer = new ChromePdfRenderer();
_renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
}
public async Task<byte[]> GeneratePdfAsync(string html)
{
using var pdf = await _renderer.RenderHtmlAsPdfAsync(html);
return pdf.BinaryData;
}
public string ExtractText(string pdfPath)
{
using var pdf = PdfDocument.FromFile(pdfPath);
return pdf.ExtractAllText();
}
}
Imports System.Threading.Tasks
Public Interface IPdfService
Function GeneratePdfAsync(html As String) As Task(Of Byte())
Function ExtractText(pdfPath As String) As String
End Interface
Public Class IronPdfService
Implements IPdfService
Private ReadOnly _renderer As ChromePdfRenderer
Public Sub New()
_renderer = New ChromePdfRenderer()
_renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
End Sub
Public Async Function GeneratePdfAsync(html As String) As Task(Of Byte()) Implements IPdfService.GeneratePdfAsync
Using pdf = Await _renderer.RenderHtmlAsPdfAsync(html)
Return pdf.BinaryData
End Using
End Function
Public Function ExtractText(pdfPath As String) As String Implements IPdfService.ExtractText
Using pdf = PdfDocument.FromFile(pdfPath)
Return pdf.ExtractAllText()
End Using
End Function
End Class
效能最佳化
記憶體使用量比較
| 工作場景 | Apache PDFBox .NET 移植 | IronPDF |
|---|---|---|
| 文字擷取 | ~80 MB | ~50 MB |
| PDF 建立 | ~100 MB | ~60 MB |
| 批次 (100 PDF) | 高(手動清理) | ~100 MB |
最佳化提示
使用 using 語句:
//自動化cleanup with IDisposable pattern
using var pdf = PdfDocument.FromFile(path);
//自動化cleanup with IDisposable pattern
using var pdf = PdfDocument.FromFile(path);
Imports PdfDocument
'自動化cleanup with IDisposable pattern
Using pdf = PdfDocument.FromFile(path)
End Using
批次作業的重複使用渲染器:
var renderer = new ChromePdfRenderer();
foreach (var html in htmlList)
{
using var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs($"output_{i}.pdf");
}
var renderer = new ChromePdfRenderer();
foreach (var html in htmlList)
{
using var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs($"output_{i}.pdf");
}
Imports IronPdf
Dim renderer As New ChromePdfRenderer()
For Each html In htmlList
Using pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs($"output_{i}.pdf")
End Using
Next
在 Web 應用程式中使用 Async:
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
排除常見的遷移問題
問題:未找到 Java 型方法名稱
將 Java 方法替換為 .NET 等效方法:
// PDFBox: stripper.getText(document)
// IronPDF: pdf.ExtractAllText()
// PDFBox: document.getNumberOfPages()
// IronPDF: pdf.PageCount
// PDFBox: stripper.getText(document)
// IronPDF: pdf.ExtractAllText()
// PDFBox: document.getNumberOfPages()
// IronPDF: pdf.PageCount
' PDFBox: stripper.getText(document)
' IronPDF: pdf.ExtractAllText()
' PDFBox: document.getNumberOfPages()
' IronPDF: pdf.PageCount
問題:無 close() 方法
IronPDF 使用 IDisposable 模式:
// PDFBox
document.close();
// IronPDF
using var pdf = PdfDocument.FromFile(path);
//自動化disposal at end of scope
// PDFBox
document.close();
// IronPDF
using var pdf = PdfDocument.FromFile(path);
//自動化disposal at end of scope
問題:無 PDFTextStripper 等效項
文字擷取簡化為單一方法:
// IronPDF: Just call ExtractAllText()
string text = pdf.ExtractAllText();
// Per-page extraction:
string pageText = pdf.Pages[0].Text;
// IronPDF: Just call ExtractAllText()
string text = pdf.ExtractAllText();
// Per-page extraction:
string pageText = pdf.Pages[0].Text;
' IronPDF: Just call ExtractAllText()
Dim text As String = pdf.ExtractAllText()
' Per-page extraction:
Dim pageText As String = pdf.Pages(0).Text
問題:PDFMergerUtility 未找到
使用靜態方法:
//IronPDFuses static Merge
var merged = PdfDocument.Merge(pdf1, pdf2, pdf3);
//IronPDFuses static Merge
var merged = PdfDocument.Merge(pdf1, pdf2, pdf3);
' IronPDF uses static Merge
Dim merged = PdfDocument.Merge(pdf1, pdf2, pdf3)
遷移後檢查清單
完成程式碼遷移後,請驗證下列事項:
- 運行所有現有的單元測試和整合測試
- 將 PDF 輸出結果與先前的版本進行直觀比較
- 測試文字擷取準確率
- 驗證許可證是否正常運作 (
IronPdf.License.IsLicensed) - 與先前實現的性能基準測試
- 更新 CI/CD 管線依賴項
- 為您的開發團隊記錄新的模式
讓您的 PDF 基礎架構面向未來
由於 .NET 10 即將推出,而 C# 14 也將引進新的語言功能,因此選擇原生的 .NET PDF 函式庫可確保與不斷演進的執行時功能相容。IronPDFfor .NET 承諾支援最新的 .NET 版本,這意味著當專案延伸至 2025 年和 2026 年時,您的移轉投資將獲得回報。
其他資源
從 Apache PDFBox .NET 移植到 IronPDF,可將您的 PDF 程式碼基礎從 Java 風格的模式轉換為成語化的 C#。 從手動坐標定位轉換到 HTML/CSS 渲染,結合原生的 async 支援和現代的 .NET 整合,提供更乾淨、更易維護的程式碼,並有專業的支援支援您的生產應用程式。

