Apache PDFBoxからIronPDFへの移行方法
Apache PDFBox は、PDF を処理するための評判の高いオープンソースの Java ライブラリです。 ただし、 .NET開発者にとって、利用可能なオプションは非公式のコミュニティ主導の移植であり、Java スタイルの API、不完全な機能範囲、限られた.NETコミュニティ サポートなど、大きな課題を伴います。 このガイドでは、Apache PDFBox .NETポートから、 .NETエコシステム専用に構築されたネイティブ.NET PDF ライブラリであるIronPDFへの詳細な移行パスを示します。
Apache PDFBox .NETポートからの移行を検討する理由は何ですか?
Apache PDFBox は Java エコシステムでは優れていますが、非公式 for .NETポートは.NET開発チームに影響を与えるいくつかの課題をもたらします。
非公式ポートの状態
Apache PDFBox は主に Java ライブラリです。 すべて for .NETバージョンはコミュニティ主導のポートであり、Apacheプロジェクトからの公式サポートはありません。 これらの移植版は Java リリースより遅れることが多く、重要な機能、バグ修正、セキュリティ更新が反映されない可能性があります。 2025年や2026年にわたる長寿命が要求されるアプリケーションを構築するチームにとって、この不確実性は技術的なリスクを生み出します。
JavaファーストAPI設計
移植されたAPIは、.NETコードでは異質に感じられるJavaの慣習を維持しています。 開発者は、PascalCase の代わりに camelCase メソッド、標準 for .NET文字列の代わりに Java File オブジェクト、および IDisposable パターンの代わりに明示的な close() 呼び出しに遭遇します。 この認知オーバーヘッドは開発速度とコードの保守性に影響します。
HTMLレンダリング機能はありません
Apache PDFBox は PDF 操作のために設計されており、HTML から PDF への変換はできません。 PDFの作成には、正確な座標位置決めによる手作業でのページ作成が必要です。これは、面倒でエラーが発生しやすいプロセスであり、現代の文書生成ニーズには対応できません。
.NETコミュニティ限定サポート
Apache PDFBox ports を取り巻く .NET エコシステムはまばらです。 .NET特有の問題に対するヘルプ、例、ベストプラクティスを見つけることは、.NETコミュニティが活発なライブラリに比べて困難です。
潜在的な JVM 依存関係
Apache PDFBox のポー ト に よ っ ては、 Java の ラ ン タ イ ム コ ン ポーネ ン ト が必要にな る場合があ り 、 .NET に重点を置 く イ ン フ ラ ス ト ラ ク チ ャ でのデフ ォ メ ン ト と 環境管理が複雑にな り ます。
ApachePDFBoxと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コミュニティ、1,000万ダウンロード以上 |
| サポート | コミュニティ限定 | プロフェッショナルサポート |
| リソースのクリーンアップ。 | 明示的な close() 呼び出し |
using ステートメントを含む IDisposable |
移行前の準備
前提条件
あなたの環境がこれらの要件を満たしていることを確認してください:
- .NET Framework 4.6.2+または.NET Core 3.1 / .NET 5-9
- Visual Studio 2019+またはJetBrains Rider
- NuGetパッケージマネージャへのアクセス
- IronPDFライセンスキー (ironpdf.com にて無料トライアル可能)
ApachePDFBoxの使用状況を監査する
以下の コ マ ン ド を ソ リ ュ ーシ ョ ンデ ィ レ ク ト リ 内で実行 し て、 すべての 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 File オブジェクト |
.NET Standard文字列/ストリーム | .NETタイプを使用 |
| テキスト抽出 | PDFTextStripper クラス |
pdf.ExtractAllText() |
よりシンプルなAPI |
ステップごとの移行プロセス
ステップ 1: NuGet パッケージを更新する
Apache PDFBox .NETポートパッケージを削除し、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 ポート実装:。
// 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 documentationを参照してください。
複数のPDFをマージする
Apache PDFBox .NET ポート実装:。
// 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 ポート実装:。
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 ポート実装:。
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 ポート実装:。
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 documentationを参照してください。
ヘッダーとフッター
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 documentationを参照してください。
.NETコアの統合
IronPdfは最新 for .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 ポー ト は非同期操作に対応 し てい ません。 IronPdfは完全な非同期/待機機能を提供します:
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);
バッチ操作のための再利用レンダラー:。
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アプリケーションで非同期を使用する:
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
よくある移行の問題のトラブルシューティング
問題Java 形式のメソッド名が見つからない
camelCase Java メソッドを PascalCase .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 見つかりません
静的な Merge メソッドを使用します。
//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が新しい言語機能を導入する中、ネイティブ for .NET PDFライブラリを選択することで、進化するランタイム機能との互換性を確保できます。 IronPDFは最新 for .NETバージョンをサポートすることに専念しており、プロジェクトが2025年、2026年に拡張されたとしても、移行への投資は報われます。
その他のリソース
Apache PDFBox .NETポートからIronPDFに移行することで、あなたのPDFコードベースはJavaスタイルのパターンからイディオム的なC#に変換されます。 手動の座標位置決めからHTML/CSSレンダリングへの移行は、ネイティブの非同期サポートと最新 for .NET統合と組み合わされ、プロダクション・アプリケーションをバックアップするプロフェッショナルなサポートとともに、よりクリーンで保守性の高いコードを提供します。

