提示:使用Java技术在word转换成pdf过程中实现将图章或者签名插入在pdf中,并生成带图章或者签名的pdf

文章目录

前言一、引入pom依赖二、编写插入图章或者签名位置的类三、编写插入图章或者签名位置的方法四、测试两个方法

前言

使用Java技术在word转换成pdf过程中实现将图章或者签名插入在pdf中,并生成带图章或者签名的pdf,来完成某些特定场景的需求

提示:以下是本篇文章正文内容,下面案例本人亲自验证过,并已解决该需求

一、引入pom依赖

引入pdf插入图章的依赖包

org.apache.pdfbox

fontbox

2.0.12

org.apache.pdfbox

pdfbox

2.0.12

二、编写插入图章或者签名位置的类

创建PdfBoxKeyWordPosition类并继承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 {

// 关键字字符数组

private char[] key;

// PDF文件路径

private String pdfPath;

// 坐标信息集合

private List list = new ArrayList();

// 当前页信息集合

private List pagelist = new ArrayList();

// 有参构造方法

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;

}

// 获取坐标信息

public List 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;

}

// 获取坐标信息

@Override

protected void writeString(String string, List textPositions) throws IOException {

for (int i = 0; i < textPositions.size(); i++) {

// text得到pdf这一行中的汉字,同时下面有判断这一行字的长度,防止关键字在文中多次出现

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];

// 需要进行一些调整 使得章盖在字体上

// X坐标 在这里加上了字体的长度,也可以直接 idx[0] = textPositions.get(i).getX()

idx[0] = textPositions.get(i).getX();

// Y坐标 在这里减去的字体的长度,也可以直接 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);

}

}

}

}

}

三、编写插入图章或者签名位置的方法

/**

* 说明:将图章插入到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 list = pdf.getCoordinate();

// 多页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 签名修改成你需要签名的地方(如:传入张三就会盖在张三上)

* @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 list = pdf.getCoordinate();

// 多页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();

}

四、测试两个方法

public static void main(String[] args) throws IOException {

System.out.println("开始图章插入");

operate("C:\\Users\\user\\Desktop\\11134731700024.pdf", "C:\\Users\\user\\Desktop\\131123.jpg");

System.out.println("插入结束");

System.out.println("开始签名插入");

autograph("C:\\Users\\user\\Desktop\\11134731700024.pdf","插入签名的位置(pdf上的文字)", "C:\\Users\\user\\123456789\\131123.jpg");

}

System.out.println("结束签名插入");

出现坐标插入成功。打开文件就可以看到了。

精彩链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。