1.pdfbox对pdf流操作
//这个是获取到的pdf的base64
BASE64Decoder base64Decoder = new BASE64Decoder();
//base64转成的数组
byte[] pdfData = base64Decoder.decodeBuffer(pdfBase64);
//通过pdfbox对pdf流新增空白页
PDDocument document = PDDocument.load(pdfData);
document.addPage(new PDPage());
ByteArrayOutputStream bos=new ByteArrayOutputStream();
document.save(bos);
byte[] pdfDataAddPage = bos.toByteArray();
document.close();
2.pdfbox小demo
public static void main(String[] args) throws IOException {
//通过pdfbox对pdf流新增空白页
File file = new File("Z:\\workSpace\\csdc\\csdc-h5\\src\\main\\resources\\file\\pdfCaSign.pdf");
PDDocument document = PDDocument.load(file);
//新增空白页
PDPage blankPage = new PDPage();
//获取空白页在文档中的内容流
PDPageContentStream contentStream = new PDPageContentStream(document, blankPage);
//设置文本字体格式和大小
File fontFile = new File("C:\\Windows\\Fonts\\simfang.ttf");
PDType0Font font = PDType0Font.load(document, fontFile);
setText(contentStream,font);
//将空白页拼接至pdf尾页
document.addPage(blankPage);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
document.save(bos);
byte[] pdfDataAddPage = bos.toByteArray();
document.close();
PDDocument newDoc = PDDocument.load(pdfDataAddPage);
newDoc.save("Z:\\newDoc.pdf");
}
public static void setText(PDPageContentStream contentStream,PDType0Font font) throws IOException {
//开启文本输入流
contentStream.beginText();
contentStream.setFont(font, 12);
//设置文本页面定位
contentStream.newLineAtOffset(20, 200);
//将文本写入文本流
contentStream.showText("This is a test text ! 用于测试页面是否乱码。");
contentStream.newLine();
contentStream.newLineAtOffset(40,220);
contentStream.showText("投保人签字:______________");
contentStream.newLine();
contentStream.newLineAtOffset(60,230);
contentStream.showText("被保人签字:______________");
contentStream.newLine();
contentStream.newLineAtOffset(60,235);
contentStream.showText("受益人签字:______________");
//结束文本输入流
contentStream.endText();
//关闭流
contentStream.close();
}