跳至页脚内容
迁移指南

如何从 Apache PDFBox 迁移到 IronPDF

Apache PDFBox 是一个备受推崇的开源 Java 库,用于处理 PDF 文件。 然而,对于 .NET 开发人员来说,可用的选项是非官方的社区驱动移植版本,这些版本带来了重大挑战——Java 风格的 API、不完整的功能覆盖范围以及有限的 .NET 社区支持。 本指南提供了从 Apache PDFBox .NET 移植版到IronPDF的详细迁移路径,IronPDF 是一个专为 .NET 生态系统构建的原生 .NET PDF 库。

为什么考虑从 Apache PDFBox .NET 端口迁移?

虽然 Apache PDFBox 在 Java 生态系统中表现出色,但其非官方的 .NET 移植版本却给 .NET 开发团队带来了一些挑战。

非官方端口状态

Apache PDFBox 主要是一个 Java 库。 所有 .NET 版本都是社区驱动的移植版本,缺乏 Apache 项目的官方支持。 这些移植版本通常落后于 Java 版本,可能会错过关键功能、错误修复或安全更新。 对于开发应用程序的团队来说,如果需要将应用程序的寿命延长到 2025 年和 2026 年,那么这种不确定性就会带来技术风险。

Java 优先的 API 设计

移植的应用程序接口保留了 Java 的习惯用法,而这些习惯用法在 .NET 代码中感觉很陌生。 开发人员会遇到 camelCase 方法而不是 PascalCase,Java File 对象而不是标准的 .NET 字符串,以及显式 close() 调用而不是 IDisposable 模式。 这种认知负担会影响开发速度和代码可维护性。

无 HTML 渲染功能

Apache PDFBox 设计用于 PDF 操作,而非 HTML 到 PDF 的转换。 创建 PDF 需要手动构建页面并进行精确的坐标定位--这一过程既繁琐又容易出错,无法满足现代文档生成的需求。

有限的 .NET 社区支持

围绕 Apache PDFBox 端口的 .NET 生态系统非常稀少。 与拥有活跃 .NET 社区的库相比,要找到针对 .NET 问题的帮助、示例或最佳实践非常困难。

潜在的 JVM 依赖关系

某些 Apache PDFBox 移植可能需要 Java 运行时组件,从而增加了以 .NET 为重点的基础架构中部署和环境管理的复杂性。

Apache PDFBox 与 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 社区,1000 万次以上下载
支持 仅限社区 提供专业支持
资源清理 显式 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" .
SHELL

值得期待的重大变化

翻译类别 Apache PDFBox .NET 端口 IronPDF 迁移行动
对象模型 PDDocument, PDPage PdfDocument, ChromePdfRenderer 不同的类层次结构
PDF 创建 手动页面/内容流 HTML 渲染 重写创建逻辑
方法风格 camelCase()(Java 风格) PascalCase()(.NET 风格) 更新方法名称
资源清理 document.close() using 语句 更改处理模式
文件访问 Java 对象 标准 .NET 字符串/流 使用 .NET 类型
文本提取 PDFTextStripper pdf.ExtractAllText() 更简单的应用程序接口

逐步迁移过程

步骤 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
SHELL

步骤 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"
$vbLabelText   $csharpLabel

步骤 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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

IronPDF 基于 Chromium 的渲染引擎提供全面的 HTML、CSS 和 JavaScript 支持。 有关高级场景,请参阅 HTML 转 PDF 文档

合并多个 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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

IronPDF 的静态方法直接接受多个文档,消除了实用程序类模式。

从零开始创建 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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

基于 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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

有关完整的 URL 转换选项,请参阅 URL 转 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
$vbLabelText   $csharpLabel

有关高级布局,请参阅页眉和页脚文档

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
$vbLabelText   $csharpLabel

同步支持

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
$vbLabelText   $csharpLabel

依赖注入配置

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
$vbLabelText   $csharpLabel

性能优化

内存使用对比

情景 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);
$vbLabelText   $csharpLabel

批量操作重用渲染器:

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
$vbLabelText   $csharpLabel

在 Web 应用程序中使用 Async:

using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
using var pdf = await renderer.RenderHtmlAsPdfAsync(html);
$vbLabelText   $csharpLabel

常见迁移问题的故障排除

问题:未找到 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
$vbLabelText   $csharpLabel

问题:无 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
$vbLabelText   $csharpLabel

问题:无 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
$vbLabelText   $csharpLabel

问题: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)
$vbLabelText   $csharpLabel

迁移后核对表

完成代码迁移后,请验证以下内容:

  • 运行所有现有的单元测试和集成测试
  • 将 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 渲染的转变,再加上本地异步支持和现代 .NET 集成,为您的生产应用程序提供了更简洁、更易维护的代码和专业支持。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

钢铁支援团队

我们每周 5 天,每天 24 小时在线。
聊天
电子邮件
打电话给我