在 Java 中使用 iText 操作PDF文件


=Start=

缘由:

前段时间看到一篇文章想起来的,最近有时间简单做了测试,以及一些代码和功能的学习,这里简单整理一下,方便后面有需要的时候参考。

正文:

参考解答:

iText 的 PdfStamper 做起这个工作来真的是很方便和快速。

这里简单整理一下常见的PDF文件读写需求的代码片段,其它的功能实现后面按实际需求情况进行补充。

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>
package com.example;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @author ixyzero
 * Created on 2023-12-01
 */
public class ItextWatermark {

    static void printPdfContent(PdfReader pdfReader) throws IOException {
        // 逐页打印 PDF 文件内容
        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            String pageContent = PdfTextExtractor.getTextFromPage(pdfReader, i);
            System.out.println("\nContent on Page " + i + ":\n" + pageContent);
        }
    }

    public static void main(String[] args) throws IOException, DocumentException {
        // 读取原始 PDF 文件
        PdfReader reader = new PdfReader("output-pdfbox.pdf");
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output-itext.pdf"));

        // 逐页打印 PDF 文件内容
        printPdfContent(reader);

        // 查看 PDF 文件的meta信息
        Map<String, String> info = reader.getInfo();
        for (Map.Entry<String, String> entry : info.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        // 获取 PDF 中的页数
        int pageCount = reader.getNumberOfPages();

        // 添加水印
        for (int i = 1; i <= pageCount; i++) {
            PdfContentByte contentByte = stamper.getUnderContent(i); // 或者 getOverContent()
            contentByte.beginText();
            contentByte.setFontAndSize(BaseFont.createFont(), 1f); // 字体和字号
            contentByte.setColorFill(BaseColor.WHITE); // 字体颜色
            // showTextAligned(元素对齐方式,文本内容,x坐标,y坐标,旋转角度)
            contentByte.showTextAligned(Element.ALIGN_BOTTOM, "Watermark-add-here", 100, 1, 0);
            contentByte.endText();
        }

        // 保存修改后的 PDF 文件并关闭文件流
        stamper.close();
        reader.close();
    }

}
参考链接:

SpringBoot 实现 PDF 添加水印,我有 5 种实现方案
https://mp.weixin.qq.com/s/gphcp_L80OzOXxsPAijgig

A Free Java-PDF library
https://mvnrepository.com/artifact/com.itextpdf/itextpdf

showTextAligned
https://www.tabnine.com/code/java/methods/com.itextpdf.text.pdf.PdfContentByte/showTextAligned

iText – Quick Guide
https://www.tutorialspoint.com/itext/itext_quick_guide.htm

iText – Setting Font
https://www.tutorialspoint.com/itext/itext_setting_font.htm

Are there any Java PDF creation alternatives to iText? [closed]
https://stackoverflow.com/questions/1377135/are-there-any-java-pdf-creation-alternatives-to-itext

iText – add content to existing PDF file
https://stackoverflow.com/questions/3335126/itext-add-content-to-existing-pdf-file

Tagged with itext,pdf,java
https://stackoverflow.com/questions/tagged/itext+pdf+java

Reading PDF File Using Java
https://www.baeldung.com/java-pdf-file-read

iText – Read and Write PDF in Java
https://mkyong.com/java/itext-read-and-write-pdf-in-java/

How To Read An Existing Pdf File In Java Using IText Jar?
https://www.w3schools.blog/itext-read-pdf-file-in-java

Difference between getUnderContent and getOverContent of itext
https://stackoverflow.com/questions/66029258/difference-between-getundercontent-and-getovercontent-of-itext

Creating PDF Files in Java
https://www.baeldung.com/java-pdf-creation

Tag: PDF
https://www.baeldung.com/tag/pdf

Get Information About a PDF in Java
https://www.baeldung.com/java-pdf-info

Using Watermarks with iText in Java
https://www.baeldung.com/java-watermarks-with-itext

java生成PDF的几种方法
https://www.jianshu.com/p/e0a54d5e63c3

Using Watermarks in iText 7
https://dzone.com/articles/using-watermarks-in-itext-7

Custom Metadata entry
https://kb.itextpdf.com/itext/custom-metadata-entry

=END=


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注