在PDF指定位置添加數據

来源:https://www.cnblogs.com/lgccrush/archive/2022/06/11/16365550.html
-Advertisement-
Play Games

第一種方案 使用itext填充靜態PDF模板 然後生成PDF新文件 1 使用Adobe Acrobat 編輯原PDF 添加文本域 itext依賴 <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId ...


第一種方案 使用itext填充靜態PDF模板 然後生成PDF新文件

1 使用Adobe Acrobat 編輯原PDF 添加文本域

image-20220611104008064

itext依賴

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.9</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

代碼實現

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
import java.util.ArrayList;

/**
 * 操作PDF
 *
 * @Author liguangcheng
 * @Date 2022/6/11 09:26
 * @Vision 1.0
 **/
public class PDFTest {
    public static void main(String[] args) throws Exception {
        String filePath = "test.pdf";
        PdfReader reader = new PdfReader(filePath);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("test-副本.pdf"));
        //使用中文字體
        BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        ArrayList<BaseFont> fontList = new ArrayList<>();
        fontList.add(bf);

        AcroFields acroFields = stamper.getAcroFields();
        acroFields.setSubstitutionFonts(fontList);

        //設置欄位
        acroFields.setField("name", "測試姓名");
        acroFields.setField("age", "18");
        stamper.setFormFlattening(true);
        stamper.close();
    }
}

test-副本.pdf效果

image-20220611104319882

第二種方案 通過坐標定位 然後添加水印

import com.itextpdf.awt.geom.Rectangle2D;
import com.itextpdf.text.BaseColor;
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.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import lombok.Getter;
import lombok.Setter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 操作PDF
 *
 * @Author liguangcheng
 * @Date 2022/6/11 09:26
 * @Vision 1.0
 **/
public class PDFTest {
    public static void main(String[] args) throws Exception {

        List<String> keywordList = new ArrayList();
        keywordList.add("測試姓名");
        keywordList.add("18");
        List<PdfItextModel> pdfItextModels = new ArrayList<>();
        for (String keyword : keywordList) {
            float[] keyWordsByPath = getKeyWords("test-副本.pdf", keyword);
            PdfItextModel pdfItextModel = new PdfItextModel();
            pdfItextModel.setContent("水印" +keyword);
            for (int i = 0; i < keyWordsByPath.length; i++) {
                pdfItextModel.setXCoordinate(keyWordsByPath[0]);//x坐標
                pdfItextModel.setYCoordinate(keyWordsByPath[1]);//y坐標
                pdfItextModel.setPageNum((int) keyWordsByPath[2]);//頁數
                System.out.println(keyword + "坐標值:" + keyWordsByPath[i]);
            }
            pdfItextModels.add(pdfItextModel);
        }
        signSinglePsw("test.pdf", pdfItextModels);
    }


    /**
     * @param filepath 文件位置
     * @param keyWords 關鍵字
     * @return float[]
     * @Description 獲取關鍵字所在PDF坐標
     */
    private static float[] getKeyWords(String filepath, String keyWords) {
        float[] coordinate = null;
        int page = 0;
        try {
            PdfReader pdfReader = new PdfReader(filepath);
            int pageNum = pdfReader.getNumberOfPages();
            PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(pdfReader);
            CustomRenderListener renderListener = new CustomRenderListener();
            renderListener.setKeyWord(keyWords);
            for (page = 1; page <= pageNum; page++) {
                renderListener.setPage(page);
                pdfReaderContentParser.processContent(page, renderListener);
                coordinate = renderListener.getPcoordinate();
                if (coordinate != null) break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return coordinate;
    }


    @Getter
    @Setter
    static class CustomRenderListener implements RenderListener {

        private float[] pcoordinate = null;

        private String keyWord;

        private int page;

        @Override
        public void beginTextBlock() {
        }

        @Override
        public void endTextBlock() {
        }

        @Override
        public void renderImage(ImageRenderInfo arg0) {
        }

        @Override
        public void renderText(TextRenderInfo textRenderInfo) {
            String text = textRenderInfo.getText();
            if (null != text && text.contains(keyWord)) {
                Rectangle2D.Float boundingRectange = textRenderInfo.getBaseline().getBoundingRectange();
                pcoordinate = new float[3];
                pcoordinate[0] = boundingRectange.x;
                pcoordinate[1] = boundingRectange.y;
                pcoordinate[2] = page;
            }
        }

    }

    /**
     * @param oldPswFilePath 原來的文件地址
     * @param list           需要添加的詳細信息
     * @return
     */
    public static String signSinglePsw(String oldPswFilePath, List<PdfItextModel> list) throws Exception {

        int lastIndex = oldPswFilePath.lastIndexOf('.');
        // 獲取文件尾碼
        String suffix = oldPswFilePath.substring(lastIndex + 1);
        // 判斷是否為pdf文件
        if (!"pdf".equalsIgnoreCase(suffix)) {
            throw new RuntimeException("Not is PDF file");
        }

        // 生成新的文件路徑
        String newPswPath = oldPswFilePath.substring(0, lastIndex) + "-副本1." + suffix;
        System.out.println("單個psw文件簽名生成的新路徑:" + newPswPath);

        //解析文件
        PdfReader reader = new PdfReader(oldPswFilePath);
        FileOutputStream fOut = new FileOutputStream(newPswPath);
        PdfStamper stp = new PdfStamper(reader, fOut);

        // 總頁數
        System.out.println("PDF總頁數:" + reader.getNumberOfPages());

        for (PdfItextModel model : list) {
            Float xCoordinate = model.getXCoordinate();
            Float yCoordinate = model.getYCoordinate();
            Integer pageNum = model.getPageNum();
            String content = model.getContent();
            if (xCoordinate == null || yCoordinate == null ||
                    pageNum == null || pageNum == 0 || content == null || "".equals(content)) {
                continue;
            }
            PdfContentByte pdfContentByte = stp.getOverContent(pageNum);
            pdfContentByte.beginText();
            // 設置字體及字型大小
            pdfContentByte.setFontAndSize(getBaseFont(), 14);
            addDeptReview(xCoordinate, yCoordinate, pdfContentByte, content);
            pdfContentByte.endText();
        }
        stp.close();
        // 將輸出流關閉
        fOut.close();
        reader.close();
        // 文件讀寫結束
        System.out.println("PSW文件讀寫完畢");

        return newPswPath;
    }

    // 獲取基礎文字
    public static BaseFont getBaseFont() throws Exception {
        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
        return base;
    }

    /**
     * @param content
     * @param keyword
     * @param x       X軸坐標
     * @param y       Y軸坐標
     * @Description 添加水印
     */
    private static void addDeptReview(float x, float y, PdfContentByte content, String keyword) {
        content.setColorFill(BaseColor.BLACK);
        // 設置水印位置和內容
        System.out.println("水印內容:" + keyword);
        System.out.println("列印位置坐標:" + x + "," + y);
        content.showTextAligned(Element.ALIGN_LEFT, keyword, x, y, 0);
    }

    @Getter
    @Setter
    static class PdfItextModel {
        //  X坐標
        private Float xCoordinate;
        //Y坐標
        private Float yCoordinate;
        //頁碼
        private Integer pageNum;
        //內容
        private String content;
        
        public PdfItextModel() {
        }
    }
}

找到test.副本.pdf中關鍵字坐標

image-20220611112309476

通過test.pdf空白模板生成新的文件test.副本1.pdf並添加水印

image-20220611112529514

參考博客[https://blog.51cto.com/u_15127609/3272726]

參考博客[https://www.cnblogs.com/alphajuns/p/12436332.html]
參考博客[https://blog.csdn.net/k_young1997/article/details/103512997]


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 引子 把大象裝進冰箱需要3步:打開冰箱門,把大象裝入冰箱,關閉冰箱門。 擴展一下,我們考慮把動物裝進冰箱的場景。比如,把豬🐷裝進冰箱,把狗🐶裝進冰箱,等等。 怎麼利用面向對象的思想來進行程式設計呢? talk is cheap, show me the code. 把大象裝進冰箱的程式設計及實現 ...
  • Spring容器包含兩個重要的特性:面向切麵編程(AOP)和控制反轉(IOC)。面向切麵編程是面向對象(OOP)的一種補充,在面向對象編程的過程中編程針對的目標是一個個對象,而面向切麵編程中編程針對的目標是一個個切麵。切麵支持跨類型跨對象(如事務的切麵可以加在任何地方)進行模塊化。 前言 AOP是S ...
  • 目錄 一.簡介 二.效果演示 三.源碼下載 四.猜你喜歡 零基礎 OpenGL (ES) 學習路線推薦 : OpenGL (ES) 學習目錄 >> OpenGL ES 基礎 零基礎 OpenGL (ES) 學習路線推薦 : OpenGL (ES) 學習目錄 >> OpenGL ES 轉場 零基礎 O ...
  • 1.背景 在大型項目開發過程中,經常會遇到列印大量日誌,輸出信息和在源碼中寫註釋的情況。對於軟體開發來說,我們一般都是列印輸出英文的日誌(主要考慮軟體在各種環境下的相容性,如果列印中文日誌可能會出現亂碼,另外英文日誌更容易搜索,更容易後續做國際化),但是對於我們中國人來說,很容易就把中文全形的中文標 ...
  • 每天都會分享幾個有趣的Python小知識,現在給大家分享幾個適合新手練習的小項目,好玩不燒腦,提升技能不在話下。等會就叫你的室友跟你一起VS,輕輕鬆松成為捲王。 但是問題有三個: 1、你不知道已經有哪些輪子已經造好了,哪個適合你用。有名有姓的的著名輪子就400多個,更別說沒名沒姓自己在製造中的輪子。 ...
  • 工作很多年後,才發現有很多工具類庫,可以大大簡化代碼量,提升開發效率,初級開發者卻不知道。而這些類庫早就成為了業界標準類庫,大公司的內部也都在使用,如果剛工作的時候就有人告訴我使用這些工具類庫,該多好! 一塊看一下有哪些工具類庫你也用過。 1. Java自帶工具方法 1.1 List集合拼接成以逗號 ...
  • 又到每天Python小技巧分享的時候了,今天給大家分享的是怎麼樣去爬取清純小姐姐照片(沒有人會拒絕美女吧,小聲說),這篇文章好像有點刺激,未成年的小伙伴就不要進來了。快來看看這些清純的小姐姐的容顏,話不多說,上教程。 先來看看效果圖 不好意思,圖片有點辣眼睛,被攔截了,還沒有還給我..... imp ...
  • 介紹了 wait notify notifyAll park unpark ReentrantLock等相關知識 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...