Editing Existing PDF Files in Java

Editing Existing PDF Files in Java

1. Overview

In this article, we’ll see how to edit the content of an existing PDF file in Java. First, we’ll just add new content. Then, we’ll focus on removing or replacing some pre-existing content.

2. Adding the iText7 Dependency

We’ll use the iText7 library to add content to the PDF file. Later on, we’ll use the pdfSweep add-on to remove or replace content.

Note that iText is licensed under AGPL, which might limit the distribution of a commercial application: iText License Model.

First, let’s add these dependencies to our pom.xml:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext7-core</artifactId>
    <version>7.2.3</version>
    <type>pom</type>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>cleanup</artifactId>
    <version>3.0.1</version>
</dependency>

3. File Handling

Let’s understand the steps for handling our PDF with iText7:

  • First, we open a PdfReader to read the content of the source file. This throws an IOException if an error occurs at any time while reading the file.
  • Then, we open a PdfWriter to the destination file. If this file doesn’t exist or can’t be created, a FileNotFoundException is thrown.
  • After that, we’ll open a PdfDocument which uses our PdfReader and PdfWriter.
  • Finally, closing the PdfDocument closes both the underlying PdfReader and PdfWriter.

Let’s write a main() method that runs our whole treatment. For the sake of simplicity, we’ll just rethrow any Exception that could occur:

public static void main(String[] args) throws IOException {
    PdfReader reader = new PdfReader("src/main/resources/baeldung.pdf");
    PdfWriter writer = new PdfWriter("src/main/resources/baeldung-modified.pdf");
    PdfDocument pdfDocument = new PdfDocument(reader, writer);
    addContentToDocument(pdfDocument);
    pdfDocument.close();
}

In the following section, we’ll complete step-by-step the addContentToDocument() method in order to fill our PDF with new content. The source document’s a PDF file that only contains the text “Hello Baeldung*“* on the top left. The destination file will be created by the program.

请添加图片描述

4. Adding Content to the File

We’ll now add various types of content to the file.

4.1. Adding a Form

We’ll start by adding a form to the file. Our form will be very simple and contain a unique field called name.

Furthermore, we need to tell iText where to place the field. In this case, we’ll put it at the following point: (35,400). The coordinates (0,0) refer to the bottom left of the document. Lastly, we’ll set the dimension of the field to 100×30:

PdfFormField personal = PdfFormField.createEmptyField(pdfDocument);
personal.setFieldName("information");
PdfTextFormField name = PdfFormField.createText(pdfDocument, new Rectangle(35, 400, 100, 30), "name", "");
personal.addKid(name);
PdfAcroForm.getAcroForm(pdfDocument, true)
    .addField(personal, pdfDocument.getFirstPage());

Additionally, we’ve explicitly specified iText to add the form to the first page of the document.

请添加图片描述

4.2. Adding a New Page

Let’s now have a look at how we can add a new page to the document. We’ll use the addNewPage() method.

This method can accept the index of the added page if we want to specify it. For instance, we can add a new page at the beginning of the document:

pdfDocument.addNewPage(1);

请添加图片描述

4.3. Adding an Annotation

We’ll now want to add an annotation to the document. Concretely, an annotation looks like a squared comic bubble.

We’ll add it on top of the form that’s now located on the second page of the document. Consequently, we’ll place it on the coordinates (40,435). Additionally, we’ll give it a simple name and content. These will only show up when hovering over the annotation:

PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(40, 435, 0, 0)).setTitle(new PdfString("name"))
    .setContents("Your name");
pdfDocument.getPage(2)
    .addAnnotation(ann);

Here’s how the middle of our second page now looks:

请添加图片描述

请添加图片描述

4.4. Adding an Image

From now on, we’ll add layout elements to the page. In order to do this, we won’t be able to manipulate the PdfDocument directly anymore. We’ll rather create a Document from it and work with that. Moreover, we’ll need to close the Document in the end. Closing a Document automatically closes the base PdfDocument. So we could remove the part where we closed the PdfDocument earlier:

Document document = new Document(pdfDocument);
// add layout elements
document.close();

Now, to add the image, we’ll need to load it from its location. We’ll do this using the create() method of the ImageDataFactory class. This throws a MalformedURLException if the passed file URL can’t be parsed. In this example, we’ll use an image of Baeldung’s logo placed in the resources directory:

ImageData imageData = ImageDataFactory.create("src/main/resources/baeldung.png");

The next step will be to set the image’s properties in the file. We’ll set its size to 550×100. We’ll put it on the first page of our PDF, at the (10,50) coordinates. Let’s see the code to add the image:

Image image = new Image(imageData).scaleAbsolute(550,100)
    .setFixedPosition(1, 10, 50);
document.add(image);

The image is automatically rescaled to the given size. So here’s how it looks in the document:

请添加图片描述

请添加图片描述

4.5. Adding a Paragraph

The iText library brings some tools to add text to the file. The font can be parameterized on the pieces themselves, or directly on the Paragraph element.

For instance, let’s add the following sentence on top of the first page: This is a demo from Baeldung tutorials. We’ll set the font size of the beginning of this sentence to 16 and the global font size of Paragraph to 8:

Text title = new Text("This is a demo").setFontSize(16);
Text author = new Text("Baeldung tutorials.");
Paragraph p = new Paragraph().setFontSize(8)
    .add(title)
    .add(" from ")
    .add(author);
document.add(p);

请添加图片描述

4.6. Adding a Table

Last but not least, we can also add a table to the file. For example, we’ll define a double-entry table with two cells and two headers on top of them. We won’t specify any position. So it’ll be naturally added on top of the document, right after the Paragraph we just added:

Table table = new Table(UnitValue.createPercentArray(2));
table.addHeaderCell("#");
table.addHeaderCell("company");
table.addCell("name");
table.addCell("baeldung");
document.add(table);

Let’s see the beginning of the first page of the document now:

请添加图片描述

请添加图片描述

package org.example.pdf;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

/**
 * @author: guanglai.zhou
 * @date: 2023/12/13 13:38
 */
public class EditPdfMain {

    public static void main(String[] args) throws IOException {
        File inFile = getFileFromResources("baeldung.pdf");
        PdfReader reader = new PdfReader(inFile);
        File outFile = getFileFromResources("baeldung-modified.pdf");
        PdfWriter writer = new PdfWriter(outFile);
        PdfDocument pdfDocument = new PdfDocument(reader, writer);
        addContentToDocument(pdfDocument);
        pdfDocument.close();
        System.out.println(inFile.getPath());
        System.out.println(outFile.getPath());
    }

    /**
     * 直接读取 src/main/resources目录下的文件
     */
    public static File getFileFromResources(String fileName) {
        String path = OpenPdfMain.class.getResource("/" + fileName).getPath();
        return new File(path);
    }

    private static void addContentToDocument(PdfDocument pdfDocument) throws MalformedURLException {
        // 4.1. add form 添加一个表格到PDF文档当中
        PdfFormField personal = PdfFormField.createEmptyField(pdfDocument);
        personal.setFieldName("information");
        /**
         * 坐标0,0指的是左边底部
         */
        PdfTextFormField name = PdfFormField.createText(pdfDocument,
                new Rectangle(35, 400, 100, 30), "name", "");
        personal.addKid(name);
        /**
         * 明确指定将表格添加到第一页
         */
        PdfAcroForm.getAcroForm(pdfDocument, true)
                .addField(personal, pdfDocument.getFirstPage());

        // 4.2. add new page 通过index指定添加到文档的开头
        pdfDocument.addNewPage(1);

        // 4.3. add annotation 在第一步新增表格的上面添加注释
        PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(40, 435, 0, 0))
                .setTitle(new PdfString("name"))
                .setContents("Your name");
        pdfDocument.getPage(2).addAnnotation(ann);

        /**
         * From now on, we’ll add layout elements to the page.
         * In order to do this, we won’t be able to manipulate the *PdfDocument* directly anymore.
         * We’ll rather create a *Document* from it and work with that.
         * Moreover, we’ll need to close the *Document* in the end.
         * **Closing a Document automatically closes the base PdfDocument.**
         * So we could remove the part where we closed the *PdfDocument* earlier:
         */
        // create document form pdf document
        Document document = new Document(pdfDocument);

        // 4.4. add an image 设置图片大小为 550*100
        final File file = getFileFromResources("baeldung.png");
        ImageData imageData = ImageDataFactory.create(file.toURI().toURL());
        Image image = new Image(imageData).scaleAbsolute(550, 100)
                // 存放在第一页 坐标为 10*50
                .setFixedPosition(1, 10, 50);
        document.add(image);

        // 4.5. add a paragraph 添加文件片段
        Text title = new Text("This is a demo").setFontSize(16);
        Text author = new Text("Baeldung tutorials.");
        Paragraph p = new Paragraph().setFontSize(8)
                .add(title)
                .add(" from ")
                .add(author);
        document.add(p);

        // 4.6. add a table 添加表格
        Table table = new Table(UnitValue.createPercentArray(2));
        table.addHeaderCell("#");
        table.addHeaderCell("company");
        table.addCell("name");
        table.addCell("baeldung");
        document.add(table);

        // close the document
        // this automatically closes the pdfDocument, which then closes automatically the pdfReader and pdfWriter
        document.close();
    }

}

5. Removing Content From the File

Let’s now see how we can remove content from the PDF file. To keep things simple, we’ll write another main() method.

Our source PDF file will be the baeldung-modified.pdf file and the destination will be a new baeldung-cleaned.pdf file. We’ll work directly on the PdfDocument object. From now on, we’ll use iText’s pdfSweep add-on.

5.1. Removing Text From the File

To remove a given text from the file, we’ll need to define a cleanup strategy. In this example, the strategy will simply be to find all text matching Baeldung. The last step is to call the autoSweepCleanUp() static method of PdfCleaner. This method will create a custom PdfCleanUpTool which will throw an IOException if any error happens during file handling:

CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
strategy.add(new RegexBasedCleanupStrategy("Baeldung"));
PdfCleaner.autoSweepCleanUp(pdfDocument, strategy);

As we can see, the occurrences of the Baeldung word in the source file are overlayed with a black rectangle in the result file. This behavior is suitable, for instance, for data anonymization:

请添加图片描述

5.2. Removing Other Content From the File

Unfortunately, it’s very difficult to detect any non-text content in the file. However, pdfSweep offers the possibility to erase the content of a portion of the file. Thus, if we know where the content we want to remove is located, we’ll be able to take advantage of this possibility.

As an example, we’ll erase the content of the rectangle of size 100×35 located at (35,400) on the second page. This means we’ll get rid of all the content of the form and the annotation. Furthermore, we’ll erase the rectangle of size 90×70 located at (10,50) of the first page. This basically removes the B from Baeldung’s logo. Using the PdfCleanUpTool class, here’s the code to do all that:

List<PdfCleanUpLocation> cleanUpLocations = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(10, 50, 90,70)), new PdfCleanUpLocation(2, new Rectangle(35, 400, 100, 35)));
PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDocument, cleanUpLocations, new CleanUpProperties());
cleaner.cleanUp();

We can now see the following image in baeldung-cleaned.pdf:

请添加图片描述

请添加图片描述

6. Replacing Content in the File

In this section, we’ll do the same work as earlier, except that we’ll replace the former text with a new text instead of only erasing it.

For more clarity, we’ll use a new main() method again. Our source file will be the baeldung-modified.pdf file. Our destination file will be a new baeldung-fixed.pdf file.

Earlier we saw that the removed text was overlayed with a black background. However, this color is configurable. As we know the background of the text is white in our file, we’ll force the overlay to be white. The beginning of the treatment will be similar to what we did earlier, except that we’ll search for the text Baeldung tutorials.

However, after calling autoSweepCleanUp(), we’ll query the strategy to get the location of the removed code. We’ll then instantiate a PdfCanvas which will contain the replacement text HIDDEN. Additionally, we’ll remove the top margin to have it a bit better aligned with the original text. The default alignment is indeed not so good. Let’s look at the resulting code:

CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
strategy.add(new RegexBasedCleanupStrategy("Baeldung").setRedactionColor(ColorConstants.WHITE));
PdfCleaner.autoSweepCleanUp(pdfDocument, strategy);

for (IPdfTextLocation location : strategy.getResultantLocations()) {
    PdfPage page = pdfDocument.getPage(location.getPageNumber() + 1);
    PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(), page.getResources(), page.getDocument());
    Canvas canvas = new Canvas(pdfCanvas, location.getRectangle());
    canvas.add(new Paragraph("HIDDEN").setFontSize(8).setMarginTop(0f));
}

And we can have a look at the file:

请添加图片描述

7. Conclusion

In this tutorial, we’ve seen how to edit the content of a PDF file. We’ve seen that we can add new content, remove existing content, and even replace text in the original file with a new one.

As always, the code for this article can be found over on GitHub.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/271539.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu20.04-查看GPU的使用情况及输出详解

1. 查看GPU的使用情况 1.1 nvidia-smi # 直接在终端得到显卡的使用情况 # 不会自动刷新 nvidia-smi# 重定向到文件中 nvidia-smi > nvidia_smi_output.txt# 如果输出的内容部分是以省略号表示的&#xff0c;可以-q nvidia-smi -q 1.2 nvidia-smi -l # 会自动刷新&#x…

BED 文件格式 chip-seq m6a数据可视化会用到

General usage — bedtools 2.31.0 documentationhttps://bedtools.readthedocs.io/en/latest/content/general-usage.html BED格式&#xff08;Browser Extensible Data format&#xff09;是一种在生物信息学中广泛使用的文本文件格式&#xff0c;用于描述基因组上的特征和…

MySQL——表的内外连接

目录 一.内连接 二.外连接 1.左外连接 2.右外连接 一.内连接 表的连接分为内连和外连 内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选&#xff0c;我们前面学习的查询都是内连接&#xff0c;也是在开发过程中使用的最多的连接查询。 语法&#xff1a; s…

音频信号处理电路芯片如何选型? --低噪声,高增益

随着智能手机、汽车音频、AI智能音箱&#xff0c;智能家居、家庭影院、平板电脑、笔记本电脑等智能设备的普及&#xff1b;数字音频功放芯片的应用也越来越广泛&#xff1b;同时对音频信号处理的芯片的性能要求越来越高&#xff1b;以下几款就是常用热门音频信号处理电路芯片分…

【网络】华为交换机排查收发光情况以及思路

本站以分享各种运维经验和运维所需要的技能为主 《python零基础入门》&#xff1a;python零基础入门学习 《python运维脚本》&#xff1a; python运维脚本实践 《shell》&#xff1a;shell学习 《terraform》持续更新中&#xff1a;terraform_Aws学习零基础入门到最佳实战 《k8…

智能优化算法应用:基于人工兔算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于人工兔算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于人工兔算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.人工兔算法4.实验参数设定5.算法结果6.参考文…

BuildRoot 围炉夜话

文章目录 一、BuildRoot二、Qt 一、BuildRoot Buildroot 是Linux平台上一个构建嵌入式Linux系统的框架。整个Buildroot是由 Makefile 脚本和 Kconfig 配置文件构成的。你可以和编译Linux内核一样&#xff0c;通过buildroot配置&#xff0c;menuconfig修改&#xff0c;编译出一个…

tekton 发布 kubernetes 应用

tekton 发布 kubernetes 应用 基于Kubernetes 服务部署 Tekton Pipeline 实例&#xff0c;部署完成后使用tekton来完成源码拉取、应用打包、镜像推送和应用部署。 本文实现一个 golang-helloworld 项目 CI/CD 的完整流程&#xff0c;具体包括以下步骤&#xff1a; 从 gitee…

基于多反应堆的高并发服务器【C/C++/Reactor】(中)Dispatcher模块的实现思路和定义

&#xff08;四&#xff09;Dispatcher模块的实现思路 关于dispatcher&#xff0c;它应该是反应堆模型里边的核心组成部分&#xff0c;因为如果说这个反应堆模型里边有事件需要处理&#xff0c;或者说有事件需要检测&#xff0c;那么是需要通过这个poll、epoll 或者 select来完…

【数据结构】查找与排序

要查询信息&#xff0c;涉及两个问题&#xff1a; 在哪里查&#xff1f;——查找表 怎么查&#xff1f;——查找方法 一.查找 1.查找表的定义&#xff1a; 查找表是由同类型的数据元素构成的集合 2.对查找表的基本操作&#xff1a; 1&#xff09;查询某个数据元素是否在查…

如何进行实例监控

目录 行实例监控 云监控 云监控核心功能 云监控ECS实例 安装插件 查看监控图表 云监控控制台 云服务器ECS控制台 设置报警规则 行实例监控 方式一&#xff1a;在服务器上自行编写并定时运行&#xff08;计划任务&#xff09;监控脚本&#xff08;shell、python&#x…

众和策略:12月新批国产网游版号数量过百

上星期五&#xff08;22日&#xff09;&#xff0c;A股冲高回落&#xff0c;三大股指挨近午盘拉升走高&#xff0c;午后再度回落走低&#xff0c;沪指尾盘跌幅收窄。到收盘&#xff0c;沪指跌0.13%报2914.78点&#xff0c;深成指跌0.39%报9221.31点&#xff0c;创业板指跌0.37%…

idea Springboot小区紧急事件上报系统VS开发mysql数据库web结构java编程计算机网页源码maven项目

一、源码特点 springboot 小区紧急事件上报系统是一套完善的信息系统&#xff0c;结合springboot框架和jsp完成本系统&#xff0c;对理解JSP java编程开发语言有帮助系统采用springboot框架&#xff08;MVC模式开发&#xff09;&#xff0c;系统具 有完整的源代码和数据库&am…

408数据结构错题知识点拾遗

408相关&#xff1a; 408数据结构错题知识点拾遗 408计算机网络错题知识点拾遗 对于数据结构的学习&#xff0c;个人认为要对概念性的东西进行理解&#xff0c;特别是树的性质、图的相关性质和考察的相应算法。应用题强化的话&#xff0c;对于每一章节尾的应用小节&#xff0c…

鸿蒙系列--组件介绍之其他基础组件(下)

​​​​​​鸿蒙系列--组件介绍之其他基础组件&#xff08;上&#xff09; 一、 ScrollBar 描述&#xff1a; 滚动条组件 功能&#xff1a; 用于配合可滚动组件使用&#xff0c;如List、Grid、Scroll 子组件&#xff1a;可以包含单个子组件 ScrollBar(value: { scroller…

自学SLAM(9)《第五讲:特征点法视觉里程计》作业

文章目录 1.ORB特征点1.1 ORB提取1.2 ORB描述1.3 暴力匹配1.4 最后&#xff0c;请结合实验&#xff0c;回答下⾯⼏个问题 2.从 E 恢复 R&#xff0c;t3.用 G-N 实现 Bundle Adjustment4.* 用 ICP 实现轨迹对齐 1.ORB特征点 1.1 ORB提取 ORB(Oriented FAST and BRIEF) 特征是 S…

SM2259XT Intel N18混贴3CH开卡经验分享,SM2259XT2、SM2258XT量产固件参考教程

收了条Intel的512G不认盘的ssd&#xff0c;拆出来两颗29F02T2AMCQH1&#xff0c;这个应该是正品&#xff0c;ID也没问题。然后&#xff0c;还有个山寨的256G SATA&#xff0c;主控2259XT&#xff0c;两个颗粒丝印29F1TB2ALCTH2&#xff0c;但是&#xff0c;ID与CQH1一样&#x…

2024年科技盛宴“上海智博会·上海软博会”招商工作接近尾声

2024年上海智博会和上海软博会即将于3月份在上海跨国采购会展中心盛大召开。作为全球科技和软件行业的盛会&#xff0c;这两大展会汇集了业界顶尖的企业、创新技术和前瞻思想&#xff0c;吸引了来自世界各地的专业人士和参展商。 今年的展会将一如既往地为大家呈现最前沿的科技…

[SWPUCTF 2021 新生赛]error

[SWPUCTF 2021 新生赛]error wp 信息搜集 查看页面&#xff1a; 输个单引号会报错&#xff1a; 显然是 SQL 注入。 提示看看有没有什么捷径&#xff0c;你要说捷径的话&#xff0c;sqlmap&#xff1f;你不说我也会用 sqlmap 先跑一下&#xff0c;哈哈。 sqlmap 的使用 先简…

阿里云S5服务器4核8G和轻量选哪个比较好?

腾讯云4核8G服务器优惠价格表&#xff0c;云服务器CVM标准型S5实例4核8G配置价格15个月1437.3元&#xff0c;5年6490.44元&#xff0c;轻量应用服务器4核8G12M带宽一年446元、529元15个月&#xff0c;阿腾云atengyun.com分享腾讯云4核8G服务器详细配置、优惠价格及限制条件&…