How to Migrate from Apache PDFBox to IronPDF
Apache PDFBox is a respected open-source Java library for handling PDFs. However, for .NET developers, the available options are unofficial community-driven ports that pose significant challenges—Java-style APIs, incomplete feature coverage, and limited .NET community support. This guide provides a detailed migration path from Apache PDFBox .NET ports to IronPDF, a native .NET PDF library built specifically for the .NET ecosystem.
Why Consider Migrating from Apache PDFBox .NET Ports?
While Apache PDFBox is excellent in the Java ecosystem, its unofficial .NET ports present several challenges that affect .NET development teams.
Unofficial Port Status
Apache PDFBox is primarily a Java library (current line 3.0.7, legacy 2.0.36, Apache License 2.0). All .NET options are community-driven ports, and most are abandoned: Pdfbox 1.1.1 (last published 2013, built against PDFBox 1.8.2), Pdfbox-IKVM 1.8.9 (last published March 2017), and PdfBox_DotNet_Version 2.0.15 (last published July 2019). The only actively maintained option is MASES.NetPDF (3.0.x line, tracking PDFBox 3.0.x), which is a JCOBridge wrapper that requires a JVM at runtime alongside the CLR. These ports often lag behind Java releases and may miss critical features, bug fixes, or security updates — a risk worth weighing for long-lived .NET projects.
Java-First API Design
The ported APIs retain Java conventions that feel foreign in .NET code. Developers encounter camelCase methods instead of PascalCase, Java File objects instead of standard .NET strings, and explicit close() calls instead of IDisposable patterns. This cognitive overhead affects development speed and code maintainability.
No HTML Rendering Capability
Apache PDFBox is designed for PDF manipulation, not HTML-to-PDF conversion. Creating PDFs requires manual page construction with precise coordinate positioning—a tedious and error-prone process that doesn't scale for modern document generation needs.
Limited .NET Community Support
The .NET ecosystem around Apache PDFBox ports is sparse. Finding help, examples, or best practices for .NET-specific issues proves difficult compared to libraries with active .NET communities.
JVM Dependencies
The IKVM-based ports (Pdfbox-IKVM, Pdfbox) bundle a .NET reimplementation of the JVM, while MASES.NetPDF calls into a real JVM via JCOBridge. Either way, the deployment carries Java runtime baggage that idiomatic .NET libraries avoid.
Apache PDFBox vs. IronPDF: Key Differences
Understanding the fundamental differences between these libraries helps plan an effective migration strategy.
| Aspect | Apache PDFBox .NET Ports | IronPDF |
|---|---|---|
| Native Design | Java-centric, unofficial .NET port | Native .NET library |
| API Style | Java conventions (camelCase, close()) |
Idiomatic C# (PascalCase, using) |
| HTML Rendering | Not supported (manual page construction) | Full Chromium-based HTML/CSS/JS |
| PDF Creation | Manual coordinate positioning | CSS-based layout |
| Community | Java-focused, sparse .NET resources | Active .NET community |
| Support | Community-only | Commercial support available |
| Resource Cleanup | Explicit close() calls |
IDisposable with using statements |
Pre-Migration Preparation
Prerequisites
Ensure your environment meets these requirements:
- .NET Framework 4.6.2+ or .NET Core 3.1 / .NET 5-9
- Visual Studio 2019+ or JetBrains Rider
- NuGet Package Manager access
- IronPDF license key (free trial available at ironpdf.com)
Audit Apache PDFBox Usage
Run these commands in your solution directory to identify all Apache PDFBox references:
grep -r "org.apache.pdfbox\|Org.Apache.Pdfbox\|PDDocument\|PDFTextStripper" --include="*.cs" .
grep -rE "Pdfbox|Pdfbox-IKVM|PdfBox_DotNet_Version|MASES\.NetPDF" --include="*.csproj" .
grep -r "org.apache.pdfbox\|Org.Apache.Pdfbox\|PDDocument\|PDFTextStripper" --include="*.cs" .
grep -rE "Pdfbox|Pdfbox-IKVM|PdfBox_DotNet_Version|MASES\.NetPDF" --include="*.csproj" .
Breaking Changes to Anticipate
| Category | Apache PDFBox .NET Port | IronPDF | Migration Action |
|---|---|---|---|
| Object Model | PDDocument, PDPage |
PdfDocument, ChromePdfRenderer |
Different class hierarchy |
| PDF Creation | Manual page/content streams | HTML rendering | Rewrite creation logic |
| Method Style | camelCase() (Java style) |
PascalCase() (.NET style) |
Update method names |
| Resource Cleanup | document.close() |
using statements |
Change disposal pattern |
| File Access | Java File objects |
Standard .NET strings/streams | Use .NET types |
| Text Extraction | PDFTextStripper class |
pdf.ExtractAllText() |
Simpler API |
Step-by-Step Migration Process
Step 1: Update NuGet Packages
Remove the Apache PDFBox .NET port packages and install IronPDF:
# Remove whichever PDFBox .NET port your project uses
dotnet remove package Pdfbox # built against PDFBox 1.8.2 (2013)
dotnet remove package Pdfbox-IKVM # IKVM wrapper, last update 2017
dotnet remove package PdfBox_DotNet_Version # last update 2019
dotnet remove package MASES.NetPDF # JCOBridge wrapper, requires JVM
# Install IronPDF
dotnet add package IronPdf
# Remove whichever PDFBox .NET port your project uses
dotnet remove package Pdfbox # built against PDFBox 1.8.2 (2013)
dotnet remove package Pdfbox-IKVM # IKVM wrapper, last update 2017
dotnet remove package PdfBox_DotNet_Version # last update 2019
dotnet remove package MASES.NetPDF # JCOBridge wrapper, requires JVM
# Install IronPDF
dotnet add package IronPdf
Step 2: Configure the License Key
Add the IronPDF license key at application startup:
// Add at application startup, before any IronPDF operations
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Add at application startup, before any IronPDF operations
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
' Add at application startup, before any IronPDF operations
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Step 3: Update Namespace References
Perform a global find-and-replace across your solution:
All PDFBox .NET ports mirror the Java package hierarchy. The IKVM-based ports keep Java's lowercase form (org.apache.pdfbox.*); MASES.NetPDF title-cases it for C# (Org.Apache.Pdfbox.*).
| Find | Replace With |
|---|---|
using org.apache.pdfbox.pdmodel; (IKVM ports) |
using IronPdf; |
using org.apache.pdfbox.text; (IKVM ports) |
using IronPdf; |
using org.apache.pdfbox.multipdf; (IKVM ports) |
using IronPdf; |
using Org.Apache.Pdfbox.Pdmodel; (MASES.NetPDF) |
using IronPdf; |
using Org.Apache.Pdfbox.Text; (MASES.NetPDF) |
using IronPdf; |
Complete API Migration Reference
Document Operations
| Apache PDFBox Method | IronPDF Method |
|---|---|
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 statement or Dispose() |
document.getNumberOfPages() |
pdf.PageCount |
document.getPage(index) |
pdf.Pages[index] |
document.removePage(index) |
pdf.RemovePages(index) |
Text Extraction
| Apache PDFBox Method | IronPDF Method |
|---|---|
new PDFTextStripper() |
Not needed |
stripper.getText(document) |
pdf.ExtractAllText() |
stripper.setStartPage(n) |
pdf.Pages[n].Text |
stripper.setSortByPosition(true) |
Automatic |
Merge and Split Operations
| Apache PDFBox Method | IronPDF Method |
|---|---|
new PDFMergerUtility() |
Not needed |
merger.addSource(file) |
Load with FromFile() |
merger.mergeDocuments() |
PdfDocument.Merge(pdfs) |
new Splitter() |
Not needed |
splitter.split(document) |
pdf.CopyPages(indices) |
Security and Encryption
| Apache PDFBox Method | IronPDF Method |
|---|---|
StandardProtectionPolicy |
pdf.SecuritySettings |
policy.setUserPassword() |
pdf.SecuritySettings.UserPassword |
policy.setOwnerPassword() |
pdf.SecuritySettings.OwnerPassword |
policy.setPermissions() |
pdf.SecuritySettings.AllowUserXxx |
Code Migration Examples
Text Extraction
The most common Apache PDFBox operation demonstrates the API simplification IronPDF provides.
Apache PDFBox .NET Port Implementation:
// Apache PDFBox is a Java library — there is no official .NET port.
// Example uses Pdfbox-IKVM (last published 2017) on NuGet; namespaces
// mirror the Java packages exactly because IKVM exposes the Java API.
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.text;
using java.io;
using System;
class Program
{
static void Main()
{
PDDocument document = PDDocument.load(new File("document.pdf"));
try
{
PDFTextStripper stripper = new PDFTextStripper();
string text = stripper.getText(document);
Console.WriteLine(text);
}
finally
{
document.close();
}
}
}
// Apache PDFBox is a Java library — there is no official .NET port.
// Example uses Pdfbox-IKVM (last published 2017) on NuGet; namespaces
// mirror the Java packages exactly because IKVM exposes the Java API.
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.text;
using java.io;
using System;
class Program
{
static void Main()
{
PDDocument document = PDDocument.load(new File("document.pdf"));
try
{
PDFTextStripper stripper = new PDFTextStripper();
string text = stripper.getText(document);
Console.WriteLine(text);
}
finally
{
document.close();
}
}
}
Imports org.apache.pdfbox.pdmodel
Imports org.apache.pdfbox.text
Imports java.io
Imports System
Module Program
Sub Main()
Dim document As PDDocument = PDDocument.load(New File("document.pdf"))
Try
Dim stripper As New PDFTextStripper()
Dim text As String = stripper.getText(document)
Console.WriteLine(text)
Finally
document.close()
End Try
End Sub
End Module
IronPDF Implementation:
// 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 eliminates the PDFTextStripper class entirely, replacing multi-step extraction with a single method call.
HTML to PDF Conversion
Apache PDFBox does not support HTML-to-PDF conversion natively—this represents a fundamental capability gap.
IronPDF Implementation:
// 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's Chromium-based rendering engine provides full HTML, CSS, and JavaScript support. For advanced scenarios, see the HTML to PDF documentation.
Merging Multiple PDFs
Apache PDFBox .NET Port Implementation:
// Apache PDFBox via a .NET port (e.g. Pdfbox-IKVM on nuget.org).
// The Java class org.apache.pdfbox.multipdf.PDFMergerUtility is exposed
// directly through IKVM, so method names stay Java-style (camelCase).
using org.apache.pdfbox.multipdf;
using org.apache.pdfbox.io;
using System;
class Program
{
static void Main()
{
PDFMergerUtility merger = new PDFMergerUtility();
merger.addSource("document1.pdf");
merger.addSource("document2.pdf");
merger.setDestinationFileName("merged.pdf");
// MemoryUsageSetting governs heap vs temp-file buffering
merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
Console.WriteLine("PDFs merged");
}
}
// Apache PDFBox via a .NET port (e.g. Pdfbox-IKVM on nuget.org).
// The Java class org.apache.pdfbox.multipdf.PDFMergerUtility is exposed
// directly through IKVM, so method names stay Java-style (camelCase).
using org.apache.pdfbox.multipdf;
using org.apache.pdfbox.io;
using System;
class Program
{
static void Main()
{
PDFMergerUtility merger = new PDFMergerUtility();
merger.addSource("document1.pdf");
merger.addSource("document2.pdf");
merger.setDestinationFileName("merged.pdf");
// MemoryUsageSetting governs heap vs temp-file buffering
merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
Console.WriteLine("PDFs merged");
}
}
Imports org.apache.pdfbox.multipdf
Imports org.apache.pdfbox.io
Imports System
Module Program
Sub Main()
Dim merger As New PDFMergerUtility()
merger.addSource("document1.pdf")
merger.addSource("document2.pdf")
merger.setDestinationFileName("merged.pdf")
' MemoryUsageSetting governs heap vs temp-file buffering
merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly())
Console.WriteLine("PDFs merged")
End Sub
End Module
IronPDF Implementation:
// 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's static Merge method accepts multiple documents directly, eliminating the utility class pattern.
Creating PDFs from Scratch
The most dramatic difference appears when creating PDFs. Apache PDFBox requires manual coordinate positioning.
Apache PDFBox .NET Port Implementation:
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 Implementation:
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-based creation eliminates coordinate calculations, font management, and content stream manipulation.
Adding Password Protection
Apache PDFBox .NET Port Implementation:
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 Implementation:
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 uses strongly-typed properties instead of separate permission and policy objects.
Adding Watermarks
Apache PDFBox .NET Port Implementation:
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 Implementation:
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's HTML-based watermarking eliminates page iteration and matrix calculations.
URL to PDF Conversion
Apache PDFBox does not support URL-to-PDF conversion. IronPDF provides native support:
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
For complete URL conversion options, see the URL to PDF documentation.
Headers and Footers
Apache PDFBox requires manual positioning on each page with no built-in header/footer support. IronPDF provides declarative configuration:
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
For advanced layouts, see the headers and footers documentation.
ASP.NET Core Integration
IronPDF integrates naturally with modern .NET web applications:
[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
Async Support
Apache PDFBox ports don't support async operations. IronPDF provides full async/await capabilities:
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
Dependency Injection Configuration
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
Performance Optimization
Memory Usage Comparison
| Scenario | Apache PDFBox .NET Port | IronPDF |
|---|---|---|
| Text extraction | ~80 MB | ~50 MB |
| PDF creation | ~100 MB | ~60 MB |
| Batch (100 PDFs) | High (manual cleanup) | ~100 MB |
Optimization Tips
Use using Statements:
// Automatic cleanup with IDisposable pattern
using var pdf = PdfDocument.FromFile(path);
// Automatic cleanup with IDisposable pattern
using var pdf = PdfDocument.FromFile(path);
Reuse Renderer for Batch Operations:
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
Use Async in Web Applications:
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
Troubleshooting Common Migration Issues
Issue: Java-Style Method Names Not Found
Replace camelCase Java methods with PascalCase .NET equivalents:
// 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
Issue: No close() Method
IronPDF uses the IDisposable pattern:
// PDFBox
document.close();
// IronPDF
using var pdf = PdfDocument.FromFile(path);
// Automatic disposal at end of scope
// PDFBox
document.close();
// IronPDF
using var pdf = PdfDocument.FromFile(path);
// Automatic disposal at end of scope
Issue: No PDFTextStripper Equivalent
Text extraction is simplified to a single method:
// 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
Issue: PDFMergerUtility Not Found
Use the static Merge method:
// IronPDF uses static Merge
var merged = PdfDocument.Merge(pdf1, pdf2, pdf3);
// IronPDF uses static Merge
var merged = PdfDocument.Merge(pdf1, pdf2, pdf3);
' IronPDF uses static Merge
Dim merged = PdfDocument.Merge(pdf1, pdf2, pdf3)
Post-Migration Checklist
After completing the code migration, verify the following:
- Run all existing unit and integration tests
- Compare PDF outputs visually against previous versions
- Test text extraction accuracy
- Verify licensing works correctly (
IronPdf.License.IsLicensed) - Performance benchmark against previous implementation
- Update CI/CD pipeline dependencies
- Document new patterns for your development team
Additional Resources
Migrating from Apache PDFBox .NET ports to IronPDF transforms your PDF codebase from Java-style patterns to idiomatic C#. The shift from manual coordinate positioning to HTML/CSS rendering, combined with native async support and modern .NET integration, delivers cleaner, more maintainable code with professional support backing your production applications.

