跳至页脚内容
产品比较

IronPDF for Java 与 OpenPDF 的比较

发现最适合您 Java 应用程序的 PDF 库

在本文中,我们将探讨Java PDF库如何增强您的项目,并根据其功能、许可成本和教程比较这两个顶级库。 过去,在Java中处理PDF文件是一项挑战,但随着技术的进步,现在有许多Java API可用于管理这些文件。

我们将要研究的两个库是:

这两个库都提供在Java中创建、编辑和打印PDF文件的能力。 在这个比较中,我们将简要评估其综合能力、代码库和安装的简便性。

首先,让我们看看IronPDF。

IronPDF for Java

IronPDF for Java创建和管理PDF文件。 它允许开发人员读取、创建和编辑PDF文件,而无需Adobe软件。 开发人员可以添加自定义页眉和页脚、添加签名和其他安全限制。 全面的多线程和异步支持帮助IronPDF在开发人员需要时实现高性能。

IronPDF 功能

IronPDF for Java提供多种PDF制作和操作工具,使开发人员能够快速提升其Java应用程序的速度。

  • HTML到PDF转换:此功能允许从HTML文件、MVC视图、Web表单和URL生成PDF文档。

  • PDF成像:允许开发人员从图像创建PDF并从PDF中提取图像。

  • PDF编辑:可以添加水印、页眉、页脚、背景和前景,添加和删除页面等。

  • PDF内容提取:可以从PDF中提取文本和图像(从嵌入的PDF图像中提取文本可能需要使用IronOCR库)。

  • 兼容性:兼容Java 8+和所有现代操作系统和IDE。

OpenPDF

OpenPDF是一个开源、免费的Java库,具有双重LGPL和MPL许可,专为工作于PDF而设计。 它允许生成和操作PDF文档,以及编辑现有文档和提取内容。 OpenPDF是处理PDF文档时的便捷工具,无论是创建新文档还是编辑现有文档。

OpenPDF功能

  • 创建PDF和打印:让您从头创建新的PDF文档,并使用Java打印API打印PDF文件。

  • 拆分和合并:可以将单个PDF拆分成多个文件,或将多个PDF文件合并为一个PDF文件。

  • 提取文本:允许您从PDF文件和PDF表单中提取文本。

  • 签名:允许开发人员数字签署PDF。

  • 保存为图像:可以将PDF保存为JPEG或PNG等图像文件。

  • 办公文档转PDF:可以将MS Word、MS PowerPoint和MS Excel转换为PDF文档。

  • 解析HTML:它还提供将HTML文件解析为PDF文件的功能。

IronPDF for Java安装

安装IronPDF for Java是一个简单的过程,即使对于新的Java开发人员也是如此。

要使用IronPDF for Java,您需要一个IDE。在本文中,我们将使用JetBrains IntelliJ IDEA进行安装和示例。

首先,打开JetBrains IntelliJ IDEA并创建一个新的Maven项目:

  1. 将出现一个新窗口,您需要在其中输入项目名称并点击'Finish'按钮。
  2. 点击'Finish'后,将会打开一个新项目并显示默认pom.xml文件。 这很好,因为我们需要向该文件添加IronPDF for Java的Maven依赖项。

将以下依赖项添加到pom.xml文件中:

<!-- Add IronPDF dependencies here -->
<dependencies>
    <!-- IronPDF Maven Dependency -->
</dependencies>
<!-- Add IronPDF dependencies here -->
<dependencies>
    <!-- IronPDF Maven Dependency -->
</dependencies>
XML

一旦您将依赖项添加到pom.xml文件中,会有一个小图标出现在pom.xml文件的右上角。点击此图标以安装IronPDF for Java的Maven依赖项。 该过程可能需要几分钟,具体取决于您的网络连接。

OpenPDF安装

安装OpenPDF与安装IronPDF for Java类似。 请遵循以下步骤:

  1. 打开JetBrains IntelliJ IDEA并创建一个新的Maven项目。
  2. 输入项目名称并点击'Finish'按钮。
  3. 将打开一个新项目并显示默认pom.xml文件。 这很好,因为您需要添加OpenPDF的Maven依赖项。

将OpenPDF依赖项添加到pom.xml文件的dependencies部分。

<dependencies>
    <dependency>
        <groupId>com.github.librepdf</groupId>
        <artifactId>openpdf</artifactId>
        <version>1.3.30</version>
    </dependency>
</dependencies>
<dependencies>
    <dependency>
        <groupId>com.github.librepdf</groupId>
        <artifactId>openpdf</artifactId>
        <version>1.3.30</version>
    </dependency>
</dependencies>
XML

当您添加仓库和依赖代码时,会有一个小图标出现在pom.xml文件的右上角。点击该图标以安装OpenPDF for Java依赖项。 几分钟后,它将被安装,并准备使用。

创建新的PDF文件

除了用户吸引力外,PDF还提供几个技术优势。 PDF文件是平台无关的,可以在任何操作系统或设备上读取。它们还保持格式和布局,确保无论谁打开文档,文档外观都相同。 PDF文件也很紧凑,便于共享和存储。 通过在您的Java应用程序中使用PDF,您可以为用户提供一种高效且支持良好的文档格式,提供广泛的功能和兼容性。

在这里,我们将讨论如何使用IronPDF和OpenPDF库创建一个新的PDF文件。

使用IronPDF创建新的PDF文件

使用IronPDF for Java创建和编辑PDF文件很简单,只需几行代码。 下面是一个示例:

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set the log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render a simple HTML to PDF
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");

        // Save the rendered PDF
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set the log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render a simple HTML to PDF
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");

        // Save the rendered PDF
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

使用OpenPDF创建新的PDF文件

创建一个新的PDF文件可以使用OpenPDF完成。 以下是代码示例:

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class Main {
    /**
     * Generates a PDF file with the text 'Hello World'
     */
    public static void main(String[] args) {
        System.out.println("Generating Hello World PDF");

        Document document = new Document();

        try (FileOutputStream fileOutputStream = new FileOutputStream("HelloWorld.pdf")) {
            PdfWriter.getInstance(document, fileOutputStream);

            // Open document for writing content
            document.open();

            // Add content to the PDF
            document.add(new Paragraph("Hello World"));

        } catch (DocumentException | IOException e) {
            System.err.println("Error creating PDF: " + e.getMessage());
        } finally {
            // Ensure document is closed
            document.close();
        }
    }
}
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class Main {
    /**
     * Generates a PDF file with the text 'Hello World'
     */
    public static void main(String[] args) {
        System.out.println("Generating Hello World PDF");

        Document document = new Document();

        try (FileOutputStream fileOutputStream = new FileOutputStream("HelloWorld.pdf")) {
            PdfWriter.getInstance(document, fileOutputStream);

            // Open document for writing content
            document.open();

            // Add content to the PDF
            document.add(new Paragraph("Hello World"));

        } catch (DocumentException | IOException e) {
            System.err.println("Error creating PDF: " + e.getMessage());
        } finally {
            // Ensure document is closed
            document.close();
        }
    }
}
JAVA

将 HTML 转换为 PDF

网页经常被人们访问。 有几个网站您可能想定期查看。 每次访问网站可能不可行。 如果您需要频繁访问信息,将其存储为随时可以通过手机或笔记本电脑访问的文档更为方便。PDF格式是更好的选择,因为它提供了密码保护的好处,使文档安全。

将HTML转换为PDF是PDF库中最常用的功能之一,由于其好处,几乎每个开发人员都使用它。 在本节中,我们将讨论IronPDF for Java和OpenPDF的编码示例。

使用IronPDF进行HTML到PDF的转换

IronPDF的最先进渲染器提供三种不同方法的无缝HTML到PDF转换。

HTML文件到PDF

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set the log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render HTML file to PDF
        PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");

        // Save the rendered PDF
        myPdf.saveAs(Paths.get("html_file_saved.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set the log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render HTML file to PDF
        PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");

        // Save the rendered PDF
        myPdf.saveAs(Paths.get("html_file_saved.pdf"));
    }
}
JAVA

HTML字符串到PDF

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set the log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render HTML string to PDF
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> Example of HTML to PDF using IronPDF for Java </h1> IronPDF for Java is a robust Java API for creating, converting, and manipulating PDF files");

        // Save the rendered PDF
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set the log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render HTML string to PDF
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> Example of HTML to PDF using IronPDF for Java </h1> IronPDF for Java is a robust Java API for creating, converting, and manipulating PDF files");

        // Save the rendered PDF
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

URL到PDF

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        // Set your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set the log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render URL to PDF
        PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");

        try {
            // Save the rendered PDF
            myPdf.saveAs(Paths.get("url.pdf"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        // Set your license key for IronPDF
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set the log path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render URL to PDF
        PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");

        try {
            // Save the rendered PDF
            myPdf.saveAs(Paths.get("url.pdf"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
JAVA

使用OpenPDF将HTML转换为PDF

OpenPDF提供了将HTML文件解析为PDF文件的选项,但缺乏将URL转换为PDF的功能。 以下是一个示例:

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {

    /**
     * Parses HTML and generates a PDF file
     */
    public static void main(String[] args) {
        System.out.println("Parsing HTML to create PDF");

        try (Document document = new Document()) {
            FileOutputStream outputStream = new FileOutputStream("contact.pdf");
            PdfWriter.getInstance(document, outputStream);

            // Open the document
            document.open();

            // Parse the HTML document and write to PDF
            HtmlParser.parse(document, Main.class.getClassLoader().getResourceAsStream("contact.html"));

        } catch (DocumentException | IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {

    /**
     * Parses HTML and generates a PDF file
     */
    public static void main(String[] args) {
        System.out.println("Parsing HTML to create PDF");

        try (Document document = new Document()) {
            FileOutputStream outputStream = new FileOutputStream("contact.pdf");
            PdfWriter.getInstance(document, outputStream);

            // Open the document
            document.open();

            // Parse the HTML document and write to PDF
            HtmlParser.parse(document, Main.class.getClassLoader().getResourceAsStream("contact.html"));

        } catch (DocumentException | IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
JAVA

图像到PDF转换器

将图像转换为PDF具有优势。 它可使照片变为更易阅读和传输的格式,并能显著减小文件大小,同时保持图像质量。

使用IronPDF的图像到PDF转换器

使用IronPDF,您可以轻松地将任何图像格式转换为PDF文件。

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Path to the directory containing images
        Path imageDirectory = Paths.get("assets/images");
        List<Path> imageFiles = new ArrayList<>();

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
            for (Path entry : stream) {
                imageFiles.add(entry);
            }

            // Convert images to a single PDF document
            PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
        } catch (IOException exception) {
            throw new RuntimeException("Error converting images to PDF from directory: " + imageDirectory + ": " + exception.getMessage(), exception);
        }
    }
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Path to the directory containing images
        Path imageDirectory = Paths.get("assets/images");
        List<Path> imageFiles = new ArrayList<>();

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
            for (Path entry : stream) {
                imageFiles.add(entry);
            }

            // Convert images to a single PDF document
            PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
        } catch (IOException exception) {
            throw new RuntimeException("Error converting images to PDF from directory: " + imageDirectory + ": " + exception.getMessage(), exception);
        }
    }
}
JAVA

使用OpenPDF的图像到PDF转换器

OpenPDF提供了图像到PDF的转换,但只支持像PNG、JPG和TIFF这样的格式。

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;

public class Main {

    public static void main(String[] args) {
        System.out.println("Converting Images to PDF");

        // Step 1: Create a document object
        Document document = new Document();

        try {
            // Step 2: Create a PdfWriter that listens to the document
            // and directs a PDF-stream to a file
            FileOutputStream fileOutputStream = new FileOutputStream("Images.pdf");
            PdfWriter.getInstance(document, fileOutputStream);

            // Step 3: Open the document
            document.open();

            // Step 4: Add images to the PDF
            Image jpg = Image.getInstance("11.png");
            document.add(jpg);

        } catch (DocumentException | IOException e) {
            System.err.println("Error creating PDF: " + e.getMessage());
        } finally {
            // Step 5: Close the document
            document.close();
        }
    }
}
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;

public class Main {

    public static void main(String[] args) {
        System.out.println("Converting Images to PDF");

        // Step 1: Create a document object
        Document document = new Document();

        try {
            // Step 2: Create a PdfWriter that listens to the document
            // and directs a PDF-stream to a file
            FileOutputStream fileOutputStream = new FileOutputStream("Images.pdf");
            PdfWriter.getInstance(document, fileOutputStream);

            // Step 3: Open the document
            document.open();

            // Step 4: Add images to the PDF
            Image jpg = Image.getInstance("11.png");
            document.add(jpg);

        } catch (DocumentException | IOException e) {
            System.err.println("Error creating PDF: " + e.getMessage());
        } finally {
            // Step 5: Close the document
            document.close();
        }
    }
}
JAVA

定价和许可

IronPDF for Java是一个强大的PDF库,用于个人和商业用途。 它提供多种许可选项,包括单个项目许可、SaaS和OEM重新分发,以及适用于跨国公司的许可。 Lite包的费用从USD开始,包括永久许可证、30天退款保证以及一年软件支持和升级。 IronPDF的一个优势是没有经常性费用,这意味着一旦购买,许可证可以终身使用。

OpenPDF是开源软件,在LGPL和MPL开源许可证条款下授权。 这意味着任何使用OpenPDF应用程序(即使通过商业网络或互联网)的人,可能有权获得程序源代码的完整副本,如果其依据LGPL和MPL许可,这使其非常适合学术用途。 然而,对于商业项目,始终建议联系OpenPDF以估算任何相关成本。

结论

Java开发人员和IT专业人士可以轻松地将PDF功能集成到他们的Java应用程序中,使用IronPDF库。 它提供了一系列广泛的功能,包括格式化PDF文件、生成图表和图形、将HTML和图像转换为PDF、拆分和合并PDF文件,以及修改PDF文档。 IronPDF支持从Java 8开始的所有Java版本,还支持JVM语言如Java、Kotlin和Scala。 该库还为PDF文档配备了增强的安全功能。

OpenPDF是一个开源、免费的Java库,具有LGPL和MPL许可证。 它允许创建和修改PDF文档,以及从中提取内容。 虽然OpenPDF对于生成和编辑PDF文档很有用,但其对于操控PDF文件的功能与IronPDF相比较为有限。

仅仅根据它们的许可来比较IronPDF和OpenPDF是不现实的,因为一个是商业库,另一个是开源的。 然而,就功能而言,OpenPDF在操控PDF文件方面的选项有限。 另一方面,IronPDF for Java为开发人员提供免费试用许可证,以测试库及其高级功能。

IronPDF提供比OpenPDF多得多的功能。 此外,IronPDF提供了广泛的文档,简化了使用该库的编码。 OpenPDF往往会生成冗长而复杂的代码,对其可用的文档非常少。

OpenPDF的HTML到PDF转换能力不适合大HTML文件,并且不支持URL到PDF转换。

请注意OpenPDF 是其各自所有者的注册商标。 该网站与OpenPDF无关,未经其认可或赞助。 所有产品名称、徽标和品牌均为各自所有者的财产。 比较仅供参考,反映的是撰写时的公开信息。

常见问题解答

如何在Java中将HTML转换为PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法将 HTML 字符串转换为 PDF。此外,您可以使用 RenderHtmlFileAsPdf 将 HTML 文件转换为 PDF。

IronPDF和OpenPDF之间的主要区别是什么?

IronPDF提供全面的PDF操作工具,包括HTML到PDF转换、PDF成像、编辑和内容提取,以及多线程和异步支持。OpenPDF由于是开源的,支持PDF创建、基本的HTML解析和文本提取,但缺乏URL到PDF转换。

在Java中将HTML转换为PDF有哪些许可选项?

IronPDF提供各种适合不同需求的许可选项,包括单个项目许可证、SaaS和OEM再分发,以及跨国公司许可。

如何安装支持HTML到PDF转换的Java库?

要安装IronPDF for Java,您需要在IDE中创建一个新的Maven项目,然后将必要的Maven依赖项添加到pom.xml文件中并进行安装。

我可以使用Java PDF库将URL转换为PDF吗?

是的,IronPDF允许您直接将URL转换为PDF,除此之外还可以将HTML文件和字符串转换为PDF,提供灵活的PDF创建选项。

IronPDF提供了哪些PDF操作功能?

IronPDF提供HTML到PDF转换、PDF成像和编辑、内容提取、支持多线程和异步操作等功能,使其成为PDF管理的强大选择。

OpenPDF适合商业用途吗?

虽然OpenPDF是免费使用且开源的,但由于其功能有限,可能更适合学术或基本使用。IronPDF提供广泛的文档、先进的功能和商业许可选项,更适合商业用途。

OpenPDF支持URL到PDF转换吗?

不,OpenPDF不支持URL到PDF转换。它仅提供基本的HTML解析成PDF。

IronPDF可以处理Java中的图像到PDF转换吗?

是的,IronPDF可以将各种图像格式转换为单个PDF文档,在处理不同类型的内容时提供灵活性。

是什么使IronPDF成为Java应用程序中更好的综合PDF功能选择?

IronPDF提供强大的功能、易于集成、广泛的文档、先进的功能和更好的支持,使其成为开发人员寻求Java应用程序中综合PDF能力的优选选择。

Darrius Serrant
全栈软件工程师(WebOps)

Darrius Serrant 拥有迈阿密大学的计算机科学学士学位,目前在 Iron Software 担任全栈 WebOps 市场工程师。从小就被编码吸引,他认为计算机既神秘又易于接触,使其成为创意和问题解决的理想媒介。

在 Iron Software,Darrius 喜欢创造新事物,并简化复杂概念以使其更易理解。作为我们常驻的开发者之一,他还自愿教授学生,与下一代分享他的专业知识。

对于 Darrius 来说,他的工作令人满意,因为它被重视并产生真正的影响。