Creating PDF Files in Java

1. Introduction

In this quick tutorial, we'll focus on creating PDF documents from scratch based on the iText and PdfBox libraries.

2. Maven Dependencies

First, we need to include the following Maven dependencies in our project:

xml 复制代码
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>3.0.0</version>
</dependency>

The latest version of the libraries can be found here: iText and PdfBox.

It's important to know that iText is available under the open-source AGPL license, as well as a commercial license. If we purchase a commercial license, we can keep our source code to ourselves, allowing us to retain our IP. If we use the AGPL version, we'll need to release our source code free of charge. We can follow this link to determine how to make sure our software complies with AGPL.

We'll also need to add one extra dependency in case we need to encrypt our file. The Bouncy Castle Provider package contains implementations of cryptographic algorithms, and is required by both libraries:

xml 复制代码
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcpkix-jdk18on</artifactId>
    <version>1.76</version>
</dependency>

The latest version of the library can be found here: The Bouncy Castle Provider.

3. Overview

iText and PdfBox are both Java libraries that we use for the creation and manipulation of pdf files. Although the final output of the libraries is the same, they operate in a different manner. Let's take a closer look at each of them.

4. Create Pdf in IText

4.1. Insert Text in Pdf

Let's look at how we insert a new file with "Hello World" text into a pdf file:

java 复制代码
package org.example.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @author: guanglai.zhou
 * @date: 2023/12/13 15:23
 */
public class NewPdf {

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        Document document = new Document();
        File file = new File("D:\\data\\pdf\\iTextHelloWorld.pdf");
        PdfWriter.getInstance(document, new FileOutputStream(file));

        document.open();
        Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
        Chunk chunk = new Chunk("Hello World", font);

        document.add(chunk);
        document.close();
        System.out.println(file);
    }

}

Creating a pdf with the use of the iText library is based on manipulating objects implementing the Elements interface in Document (in version 5.5.10 there are 45 of those implementations).

The smallest element we can add to the document and use is Chunk, which is basically a string with the applied font.

Additionally, we can combine Chunk s with other elements, like Paragraphs , Section, etc., resulting in nice looking documents.

4.2. Inserting Image

The iText library provides an easy way to add an image to the document. We simply need to create an Image instance and add it to the Document:

java 复制代码
Path path = Paths.get(ClassLoader.getSystemResource("Java_logo.png").toURI());

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("iTextImageExample.pdf"));
document.open();
Image img = Image.getInstance(path.toAbsolutePath().toString());
document.add(img);

document.close();

4.3. Inserting Table

We may face a problem if we want to add a table to our pdf. Luckily, iText provides this functionality out-of-the-box.

First, we need to create a PdfTable object and provide a number of columns for our table in the constructor.

Then we can simply add new cells by calling the addCell method on the newly created table object. iText will create table rows as long as all the necessary cells are defined. This means that once we create a table with three columns, and add eight cells to it, only two rows with three cells in each will be displayed.

Let's look at the example:

java 复制代码
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("iTextTable.pdf"));

document.open();

PdfPTable table = new PdfPTable(3);
addTableHeader(table);
addRows(table);
addCustomRows(table);

document.add(table);
document.close();

Now we'll create a new table with three columns and three rows. We'll treat the first row as a table header with a changed background color and border width:

java 复制代码
private void addTableHeader(PdfPTable table) {
    Stream.of("column header 1", "column header 2", "column header 3")
      .forEach(columnTitle -> {
        PdfPCell header = new PdfPCell();
        header.setBackgroundColor(BaseColor.LIGHT_GRAY);
        header.setBorderWidth(2);
        header.setPhrase(new Phrase(columnTitle));
        table.addCell(header);
    });
}

The second row will consist of three cells with just text, and no extra formatting:

java 复制代码
private void addRows(PdfPTable table) {
    table.addCell("row 1, col 1");
    table.addCell("row 1, col 2");
    table.addCell("row 1, col 3");
}

We can also include images in cells. Furthermore, we can format each cell individually.

In this example, we're applying horizontal and vertical alignment adjustments:

java 复制代码
private void addCustomRows(PdfPTable table) 
  throws URISyntaxException, BadElementException, IOException {
    Path path = Paths.get(ClassLoader.getSystemResource("Java_logo.png").toURI());
    Image img = Image.getInstance(path.toAbsolutePath().toString());
    img.scalePercent(10);

    PdfPCell imageCell = new PdfPCell(img);
    table.addCell(imageCell);

    PdfPCell horizontalAlignCell = new PdfPCell(new Phrase("row 2, col 2"));
    horizontalAlignCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(horizontalAlignCell);

    PdfPCell verticalAlignCell = new PdfPCell(new Phrase("row 2, col 3"));
    verticalAlignCell.setVerticalAlignment(Element.ALIGN_BOTTOM);
    table.addCell(verticalAlignCell);
}

4.4. File Encryption

In order to apply permissions using the iText library, we need to have already created the pdf document. In our example, we'll use our previously generated iTextHelloWorld.pdf file.

Once we load the file using PdfReader , we need to create a PdfStamper, which we'll use to apply additional content to the file, like metadata, encryption, etc.:

java 复制代码
PdfReader pdfReader = new PdfReader("HelloWorld.pdf");
PdfStamper pdfStamper 
  = new PdfStamper(pdfReader, new FileOutputStream("encryptedPdf.pdf"));

pdfStamper.setEncryption("userpass".getBytes(), "ownerpass".getBytes(), 0, PdfWriter.ENCRYPTION_AES_256);

pdfStamper.close();

In our example, we encrypted the file with two passwords: the user password ("userpass"), where a user has read-only rights with no possibility to print it, and the owner password ("ownerpass"), which is used as a master key to allow a person full access to the pdf.

If we want to allow the user to print the pdf, then instead of 0 (third parameter of setEncryption), we can pass:

java 复制代码
PdfWriter.ALLOW_PRINTING

Of course, we can mix different permissions as well, like:

java 复制代码
PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY

Keep in mind that when using iText to set access permissions, we're also creating a temporary pdf, which should be deleted. If we don't delete it, it could be fully accessible to anyone.

5. Create Pdf in PdfBox

5.1. Insert Text in Pdf

In contrast to iText , the PdfBox library provides an API based on stream manipulation. There are no classes like Chunk /Paragraph, etc. The PDDocument class is an in-memory Pdf representation, where the user writes data by manipulating PDPageContentStream class.

Let's take a look at the code example:

java 复制代码
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);

PDPageContentStream contentStream = new PDPageContentStream(document, page);

// contentStream.setFont(PDType1Font.COURIER, 12);
contentStream.setFont(new PDType1Font(Standard14Fonts.FontName.COURIER), 12);
contentStream.beginText();
contentStream.showText("Hello World");
contentStream.endText();
contentStream.close();

document.save("pdfBoxHelloWorld.pdf");
document.close();

5.2. Inserting Image

Inserting images is also straightforward.

We need to load a file and create a PDImageXObject, subsequently drawing it on the document (need to provide exact x, y coordinates):

java 复制代码
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);

Path path = Paths.get(ClassLoader.getSystemResource("Java_logo.png").toURI());
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDImageXObject image 
  = PDImageXObject.createFromFile(path.toAbsolutePath().toString(), document);
contentStream.drawImage(image, 0, 0);
contentStream.close();

document.save("pdfBoxImage.pdf");
document.close();

5.3. Inserting a Table

Unfortunately, PdfBox doesn't provide any out-of-the-box methods that allow us to create tables. What we can do in this situation is draw it manually, literally drawing each line until our drawing resembles our desired table.

5.4. File Encryption

The PdfBox library provides the ability to encrypt and adjust file permissions for the user. Compared to iText , it doesn't require us to use an already existing file, as we simply use PDDocument . Pdf file permissions are handled by the AccessPermission class, where we can set if a user will be able to modify, extract content, or print a file.

Subsequently, we create a StandardProtectionPolicy object, which adds password-based protection to the document. We can specify two types of passwords. The user password allows the user to open a file with the applied access permissions, and the owner password has no limitations to the file:

java 复制代码
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);

AccessPermission accessPermission = new AccessPermission();
accessPermission.setCanPrint(false);
accessPermission.setCanModify(false);

StandardProtectionPolicy standardProtectionPolicy 
  = new StandardProtectionPolicy("ownerpass", "userpass", accessPermission);
document.protect(standardProtectionPolicy);
document.save("pdfBoxEncryption.pdf");
document.close();
Copy

Our example demonstrates that if the user provides the user password, the file can't be modified or printed.

6. Conclusions

In this article, we learned how to create a pdf file in two popular Java libraries.

Full examples from this article can be found in the Maven-based project over on GitHub.

相关推荐
a587695 小时前
Elasticsearch核心概念与Java实战:从入门到精通
java·es
大模型真好玩6 小时前
深入浅出LangGraph AI Agent智能体开发教程(五)—LangGraph 数据分析助手智能体项目实战
人工智能·python·mcp
测试老哥6 小时前
Selenium 使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
Brookty6 小时前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再6 小时前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame
tellmewhoisi6 小时前
前置配置1:nacos 基本配置(注册与发现)
java
张子夜 iiii7 小时前
4步OpenCV-----扫秒身份证号
人工智能·python·opencv·计算机视觉
会开花的二叉树7 小时前
继承与组合:C++面向对象的核心
java·开发语言·c++
潮汐退涨月冷风霜7 小时前
数字图像处理(1)OpenCV C++ & Opencv Python显示图像和视频
c++·python·opencv
长河8 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言