Java實(shí)現(xiàn)圖章或簽名插在pdf的固定位置
提示:以下是本篇文章正文內(nèi)容,下面案例本人親自驗(yàn)證過(guò),并已解決該需求
一、引入pom依賴
- 引入pdf插入圖章的依賴包
<!--pdf轉(zhuǎn)圖片--> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fontbox</artifactId> <version>2.0.12</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.12</version> </dependency>
二、編寫(xiě)插入圖章或者簽名位置的類(lèi)
- 創(chuàng)建PdfBoxKeyWordPosition類(lèi)并繼承PDFTextStripper
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.TextPosition; import java.io.*; import java.util.ArrayList; import java.util.List; public class PdfBoxKeyWordPosition extends PDFTextStripper { // 關(guān)鍵字字符數(shù)組 private char[] key; // PDF文件路徑 private String pdfPath; // 坐標(biāo)信息集合 private List<float[]> list = new ArrayList<float[]>(); // 當(dāng)前頁(yè)信息集合 private List<float[]> pagelist = new ArrayList<float[]>(); // 有參構(gòu)造方法 public PdfBoxKeyWordPosition(String keyWords, String pdfPath) throws IOException { super(); super.setSortByPosition(true); this.pdfPath = pdfPath; char[] key = new char[keyWords.length()]; for (int i = 0; i < keyWords.length(); i++) { key[i] = keyWords.charAt(i); } this.key = key; } public char[] getKey() { return key; } public void setKey(char[] key) { this.key = key; } public String getPdfPath() { return pdfPath; } public void setPdfPath(String pdfPath) { this.pdfPath = pdfPath; } // 獲取坐標(biāo)信息 public List<float[]> getCoordinate() throws IOException { try { document = PDDocument.load(new File(pdfPath)); int pages = document.getNumberOfPages(); for (int i = 1; i <= pages; i++) { pagelist.clear(); super.setSortByPosition(true); super.setStartPage(i); super.setEndPage(i); Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream()); super.writeText(document, dummy); for (float[] li : pagelist) { li[2] = i; } list.addAll(pagelist); } return list; } catch (Exception e) { e.printStackTrace(); } finally { if (document != null) { document.close(); } } return list; } // 獲取坐標(biāo)信息 @Override protected void writeString(String string, List<TextPosition> textPositions) throws IOException { for (int i = 0; i < textPositions.size(); i++) { // text得到pdf這一行中的漢字,同時(shí)下面有判斷這一行字的長(zhǎng)度,防止關(guān)鍵字在文中多次出現(xiàn) String text = textPositions.toString().replaceAll("[^\u4E00-\u9FA5]", ""); String str = textPositions.get(i).getUnicode(); if (str.equals(key[0] + "")) { int count = 0; for (int j = 0; j < key.length; j++) { String s = ""; try { s = textPositions.get(i + j).getUnicode(); } catch (Exception e) { s = ""; } if (s.equals(key[j] + "")) { count++; } } if (count == key.length) { float[] idx = new float[3]; // 需要進(jìn)行一些調(diào)整 使得章蓋在字體上 // X坐標(biāo) 在這里加上了字體的長(zhǎng)度,也可以直接 idx[0] = textPositions.get(i).getX() idx[0] = textPositions.get(i).getX(); // Y坐標(biāo) 在這里減去的字體的長(zhǎng)度,也可以直接 idx[1] = textPositions.get(i).getPageHeight()-textPositions.get(i).getY() idx[1] = textPositions.get(i).getPageHeight()-textPositions.get(i).getY(); System.out.println("x=" + idx[0] + ",y=" + idx[1]); pagelist.add(idx); } } } } }
三、編寫(xiě)插入圖章或者簽名位置的方法
/** * 說(shuō)明:將圖章插入到pdf上 * @param pdfPath pdf的路徑:C:\Users\user\11134731700024.pdf * @param image 圖片路徑:C:\Users\user\131123.jpg * @throws IOException */ public static void operate(String pdfPath,String image) throws IOException { File file = new File(pdfPath); PDDocument doc = PDDocument.load(file); String keyWords = "插入到pdf的固定字體"; // 修改成你需要插入的位置 PDImageXObject pdImage = PDImageXObject.createFromFile(image, doc); PdfBoxKeyWordPosition pdf = new PdfBoxKeyWordPosition(keyWords, pdfPath); PDPageContentStream contentStream = null; List<float[]> list = pdf.getCoordinate(); // 多頁(yè)pdf的處理 for (float[] fs : list) { PDPage page = doc.getPage((int)fs[2]-1); float x = fs[0]; float y = fs[1]; contentStream = new PDPageContentStream(doc, page, true, true); contentStream.drawImage(pdImage, x-11, y-35,112,112); contentStream.close(); } doc.save(pdfPath); doc.close(); } /** * 插入簽名 * @param pdfPath pdf的路徑:C:\Users\user\11134731700024.pdf * @param keyWords 簽名修改成你需要簽名的地方(如:傳入張三就會(huì)蓋在張三上) * @param image 簽名圖片 C:\Users\user\131123.jpg * @throws IOException */ public static void autograph(String pdfPath, String keyWords, String image) throws IOException { File file = new File(pdfPath); PDDocument doc = PDDocument.load(file); PDImageXObject pdImage = PDImageXObject.createFromFile(image, doc); PdfBoxKeyWordPosition pdf = new PdfBoxKeyWordPosition(keyWords, pdfPath); PDPageContentStream contentStream = null; List<float[]> list = pdf.getCoordinate(); // 多頁(yè)pdf的處理 for (float[] fs : list) { PDPage page = doc.getPage((int)fs[2]-1); float x = fs[0]; float y = fs[1]; contentStream = new PDPageContentStream(doc, page, true, true); contentStream.drawImage(pdImage, x+36, y-2,35,20); contentStream.close(); } doc.save(pdfPath); doc.close(); }
四、測(cè)試兩個(gè)方法
public static void main(String[] args) throws IOException { System.out.println("開(kāi)始圖章插入"); operate("C:\\Users\\user\\Desktop\\11134731700024.pdf", "C:\\Users\\user\\Desktop\\131123.jpg"); System.out.println("插入結(jié)束"); System.out.println("開(kāi)始簽名插入"); autograph("C:\\Users\\user\\Desktop\\11134731700024.pdf","插入簽名的位置(pdf上的文字)", "C:\\Users\\user\\123456789\\131123.jpg"); } System.out.println("結(jié)束簽名插入");
出現(xiàn)坐標(biāo)插入成功。打開(kāi)文件就可以看到了
到此這篇關(guān)于Java實(shí)現(xiàn)圖章或簽名插在pdf的固定位置的文章就介紹到這了,更多相關(guān)Java圖章或簽名插在pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring bean對(duì)象實(shí)例化實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了Spring bean對(duì)象實(shí)例化實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07java9學(xué)習(xí)系列之在docker中如何運(yùn)行java9
最近在學(xué)習(xí)java9,所以將學(xué)習(xí)中遇到的一些知識(shí)點(diǎn)分享給大家,下面這篇文章主要給大家介紹了java9學(xué)習(xí)系列之在docker中如何運(yùn)行java9的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-09-09Eclipse的Debug調(diào)試技巧大全(總結(jié))
這篇文章主要介紹了Eclipse的Debug調(diào)試技巧大全(總結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12jetbrain?fleet對(duì)標(biāo)vscode實(shí)際操作
Gradle是一個(gè)基于Apache Ant和Apache Maven概念項(xiàng)目自動(dòng)化構(gòu)建開(kāi)源工具,jetbrain家的fleet(已獲得預(yù)覽權(quán)限)直接對(duì)標(biāo)vscode?,?fleet有望超過(guò)vscode嗎?今天我們實(shí)際操作下2021-12-12微信企業(yè)號(hào) 根據(jù)錯(cuò)誤碼返回錯(cuò)誤信息類(lèi)封裝
這篇文章主要介紹了微信企業(yè)號(hào) 根據(jù)錯(cuò)誤碼返回錯(cuò)誤信息類(lèi)封裝的相關(guān)資料,需要的朋友可以參考下2016-10-10Java多線程實(shí)現(xiàn)簡(jiǎn)易微信發(fā)紅包的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java多線程實(shí)現(xiàn)簡(jiǎn)易微信發(fā)紅包的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java動(dòng)態(tài)規(guī)劃之丑數(shù)問(wèn)題實(shí)例講解
這篇文章主要介紹了Java動(dòng)態(tài)規(guī)劃之丑數(shù)問(wèn)題實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09MyBatis 探秘之#{} 與 ${} 參傳差異解碼(數(shù)據(jù)庫(kù)連接池筑牢數(shù)據(jù)交互
本文詳細(xì)介紹了MyBatis中的`#{}`和`${}`的區(qū)別與使用場(chǎng)景,包括預(yù)編譯SQL和即時(shí)SQL的區(qū)別、安全性問(wèn)題,以及如何正確使用數(shù)據(jù)庫(kù)連接池來(lái)提高性能,感興趣的朋友一起看看吧2024-12-12