引入依赖
org.apache.pdfbox
fontbox
2.0.4
org.apache.pdfbox
pdfbox
2.0.4
参数说明
inputPdfFilePath输入文件路径
outPutPdfFilePath输出文件路径
imagePros图片集合
pdfList文字集合
public static void writeToPdf(String inputPdfFilePath, String outPutPdfFilePath, List imagePros, List pdfList)
throws Exception {
//append 追加
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPutPdfFilePath, false));
PdfReader reader = new PdfReader(inputPdfFilePath);
PdfStamper stamper = new PdfStamper(reader, bos);
for (PdfPro pdfPro : pdfList) {
Integer pageNum = pdfPro.getPageNum();
int start;
int end;
if (pageNum == null) {
start = 1;
end = reader.getNumberOfPages();
} else {
start = pageNum;
end = pageNum;
}
PdfContentByte content;
//BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// "c:\\windows\\fonts\\SIMHEI.TTF" 使用windows系统的黑体
BaseFont base = BaseFont.createFont(SsmConstant.FONT_URL, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = start; i <= end; i++) {
//content = stamper.getOverContent(i);// 在内容上方加水印
content = stamper.getUnderContent(i);//在内容下方加水印
gs.setFillOpacity(0.2f);
content.beginText();
//字体大小
content.setFontAndSize(base, 10.5F);
//content.setTextMatrix(390, 810);
//内容居中,横纵坐标,偏移量
content.showTextAligned(Element.ALIGN_CENTER, pdfPro.getText(), pdfPro.getX(), pdfPro.getY(), 0);
content.setFontAndSize(base, 8);
content.endText();
}
}
for (ImagePro imagePro : imagePros) {
Image image = Image.getInstance(imagePro.getImgPath());
/* img.setAlignment(Image.LEFT | Image.TEXTWRAP); img.setBorder(Image.BOX); img.setBorderWidth(10); img.setBorderColor(BaseColor.WHITE); img.scaleToFit(100072);//大小 img.setRotationDegrees(-30);//旋转 */
//图片的位置(坐标)
image.setAbsolutePosition(imagePro.getX(), imagePro.getY());
// image of the absolute
image.scaleToFit(200, 200);
image.scalePercent(imagePro.getScalePercent());//依照比例缩放. 调整缩放,控制图片大小
PdfContentByte content = stamper.getUnderContent(6);
content.addImage(image);
}
stamper.close();
//关闭打开的原来PDF文件,不执行reader.close()删除不了(必须先执行stamper.close(),否则会报错)
reader.close();
//删除原来的PDF文件
/*File targetTemplePDF = new File(inputPDFFilePath); targetTemplePDF.delete();*/
}
pdfPro
@Data
public class PdfPro {
private String text;
private float x;
private float y;
private Integer pageNum;
public PdfPro(float x, float y, String text) {
this.text = text;
this.x = x;
this.y = y;
}
public PdfPro(float x, float y, String text,Integer pageNum) {
this.text = text;
this.x = x;
this.y = y;
this.pageNum = pageNum;
}
}
ImagePro
@Data
public class ImagePro {
private float x; //x 坐标
private float y; //y 坐标
private float scalePercent; //缩放百分比
private String imgPath; //路径
private Integer pageNum;
public ImagePro() {
}
public ImagePro(float x, float y, float scalePercent, String imgPath,Integer pageNum) {
this.x = x;
this.y = y;
this.scalePercent = scalePercent;
this.imgPath = imgPath;
this.pageNum = pageNum;
}
}
使用示例
public static String getSignPdf(String file, String signUrl, float x, float y,float scalePercent,String agreementNo0,String agreementNo1) {
String outPutFile = SsmConstant.WRITE_TO_PDF_URL + UUID.fastUUID() + ".PDF";
List imageList = new ArrayList<>();
//盖章
ImagePro imagePro = new ImagePro(x, y, scalePercent, signUrl, 6);
imageList.add(imagePro);
List pdfList = new ArrayList<>();
// 合同编号
if(agreementNo0 != null && agreementNo1 != null){
pdfList.add(new PdfPro(471.99628f, 769.37f, agreementNo0));
pdfList.add(new PdfPro(500.99628f, 769.37f, agreementNo1));
}
try {
EditorPDF.writeToPdf(file, outPutFile, imageList, pdfList);
} catch (Exception e) {
throw new BizException("PDF生成异常");
}
return outPutFile;
}