本文的实现方法需要用到 adobeacrobatXpro 来设置锚点(准确的说是域)
链接:https://pan.baidu.com/s/1GebLg8rSPqPBDNqnas9A3A
提取码:pp9b
下载安装
用本软件打开你的pdf
下面开始在PDF上插入锚点



以后你用java找到这个锚点是要靠这个域名的,并且插入图片的大小和域的大小一样

不设置的话打印出来的PDF会出现一个表单框
<!--pdf插图-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
<!-- itextpdf的亚洲字体支持 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
locationPath.java
package com.protectzaizai.schoolcardoa.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* @Author: 动感小⑦
* @Date:2020/2/26 15:21
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class locationPath {
//对应坐标和所在页面是第几页
float x;
float y;
int pageNo;
//图片地址
String path;
//图片大小
float width;
float height;
}
DocUtils.java
package com.protectzaizai.schoolcardoa.utils;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.protectzaizai.schoolcardoa.entity.locationPath;
import com.sun.star.lib.uno.protocols.urp.urp;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.jeecgframework.poi.word.WordExportUtil;
import java.io.*;
import java.net.ConnectException;
import java.util.*;
/**
* @Author: 动感小⑦
* @Date:2020/2/22 11:07
*/
public class DocUtils {
/**
* pdf中插入图片,针对单一pdf,需要用adobe acrobat设置表单域
* filedAndPath 是表单域名与对应图片地址的键值对
*/
public static void pdfInsertImgByfield(String sourceFile, String destFile, HashMap<String, String> filedAndPath) throws IOException, DocumentException {
// 模板文件路径
String templatePath = sourceFile;
// 生成的文件路径
String targetPath = destFile;
// 读取模板文件
InputStream input = new FileInputStream(new File(templatePath));
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(targetPath));
// 提取pdf中的表单
AcroFields form = stamper.getAcroFields();
form.addSubstitutionFont(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
//遍历表单域,插入对应图片
Iterator<Map.Entry<String, String>> iterator = filedAndPath.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
// 书签名
String fieldName = entry.getKey();
// 图片路径
String imagePath = entry.getValue();
// 通过域名获取所在页和坐标,左下角为起点
int pageNo = form.getFieldPositions(fieldName).get(0).page;
Rectangle signRect = form.getFieldPositions(fieldName).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 读图片
Image image = Image.getInstance(imagePath);
// 获取操作的页面
PdfContentByte under = stamper.getOverContent(pageNo);
// 根据域的大小缩放图片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
//这里输出数据
// locationPath locationPath = new locationPath();
// locationPath.setX(x).setY(y).setHeight(signRect.getHeight()).setWidth(signRect.getWidth()).setPageNo(pageNo).setPath(imagePath);
// System.out.println(locationPath);
}
stamper.close();
reader.close();
}
/**
* pdf中插入图片
* 如果数据定位不太详细的话可以用上面一种方法输出一下位置信息,然后确定每个位置的信息
*
* @param sourceFile
* @param destFile
* @param locationPath 自己写的类,参数x,y是地址 path是对应的地址
* @throws IOException
* @throws DocumentException
*/
public static void pdfInsertImgByLocation(String sourceFile, String destFile, List<locationPath> locationPath) throws IOException, DocumentException {
// 模板文件路径
String templatePath = sourceFile;
// 生成的文件路径
String targetPath = destFile;
// 读取模板文件
InputStream input = new FileInputStream(new File(templatePath));
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(targetPath));
for (locationPath l : locationPath) {
// 读图片
Image image = Image.getInstance(l.getPath());
// 获取操作的页面
PdfContentByte under = stamper.getOverContent(l.getPageNo());
// 根据域的大小缩放图片
image.scaleToFit(l.getWidth(), l.getHeight());
// 添加图片
image.setAbsolutePosition(l.getX(), l.getY());
under.addImage(image);
}
stamper.close();
reader.close();
}
// 下面是工具方法
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//获得锚点数据,或者生成一个锚点
public static void main(String args[]) throws IOException, DocumentException {
// HashMap<String, String> map = new HashMap<>();
// map.put("field1", "D:\\test1.png");
// map.put("field2", "D:\\test2.png");
// map.put("field3", "D:\\test1.png");
// map.put("field4", "D:\\test2.png");
// map.put("field5", "D:\\test1.png");
// pdfInsertImgByfield("D:\\schoolCard.pdf", "D:\\schoolCard1.pdf", map);
locationPath l1 = new locationPath();
l1.setX((float) 169.643).setY((float) 335.407).setPageNo(1).setWidth((float) 106.380005).setHeight((float) 94.0).setPath("D:\\test1.png");
locationPath l2 = new locationPath();
l2.setX((float) 284.738).setY((float) 335.407).setPageNo(1).setWidth((float) 106.380005).setHeight((float) 94.0).setPath("D:\\test1.png");
locationPath l3 = new locationPath();
l3.setX((float) 399.417).setY((float) 335.407).setPageNo(1).setWidth((float) 106.380005).setHeight((float) 94.0).setPath("D:\\test1.png");
locationPath l4 = new locationPath();
l4.setX((float) 286.315).setY((float) 225.042).setPageNo(1).setWidth((float) 106.380005).setHeight((float) 94.0).setPath("D:\\test1.png");
locationPath l5 = new locationPath();
l5.setX((float) 400.885).setY((float) 225.042).setPageNo(1).setWidth((float) 106.380005).setHeight((float) 94.0).setPath("D:\\test1.png");
List<locationPath> list = new ArrayList<>();
list.add(l1);
list.add(l2);
list.add(l3);
list.add(l4);
list.add(l5);
pdfInsertImgByLocation("D:\\schoolCard.pdf", "D:\\schoolCard.pdf", list);
}
private static int convertFile(File inputFile, File outputFile, OpenOfficeConnection connection) throws ConnectException {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + outputFile.getPath() + "****");
return 0;
}
}
这个util亲测可用,有问题可以在评论里说
通过Adobe Acrobat X Pro设置PDF锚点,详细步骤包括下载安装软件、打开PDF并创建锚点。锚点的名称用于Java代码中定位插入图片,确保图片大小与锚点区域匹配,防止打印时出现表单框。提供了`locationPath.java`和`DocUtils.java`两个关键文件的提及。
1617

被折叠的 条评论
为什么被折叠?



