//需要填写信息
insertTextToPdf(file, "日期:", "日期:", "日期:", 1, formattedDate, fileName, 3, 40, 0, 0);
/**
* 組裝pdf日期
*
* @param file
* @param searchText
* @param searchText1
* @param textDate
* @param num
* @param formattedDate
* @param fileName
* @param type
* @param x1
* @param x2
* @param x3
*/
private void insertTextToPdf(File file, String searchText, String searchText1, String textDate, int num, String formattedDate, String fileName, int type, int x1, int x2, int x3) {
// 添加日期
try (PDDocument document = PDDocument.load(file)) {
final int[] count = {num};
log.info("count:{}", count[0]);
if (type == 1) {
//争对不需要填写内容的pdf
// 获取文本位置信息
PDFTextStripper textStripper = new PDFTextStripper() {
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
if (text.contains(searchText) && text.contains(searchText1) && text.contains(textDate)) {
for (TextPosition textPosition : textPositions) {
if (text.contains(searchText) && text.contains(searchText1) && textDate.substring(textDate.length() - 2, textDate.length() - 1).equals(textPosition.getUnicode())) {
if (count[0] == 0) {
break;
}
count[0] = count[0] - 1;
// 在这里可以对提取的文本内容进行匹配
// 匹配到了文本内容,获取当前页码
int pageNumber = getCurrentPageNo();
log.info("Matched text found on page: " + pageNumber);
PDPage page = document.getPage(pageNumber - 1);
log.info("text:{}", text);
float x = textPosition.getEndX() + x1;
float y = textPosition.getEndY();
// 获取第N+1页
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream.newLineAtOffset(x, y);
contentStream.showText(formattedDate);
contentStream.endText();
contentStream.close();
break;
}
}
}
}
};
textStripper.setSortByPosition(true);
textStripper.getText(document);
} else if(type == 2){
//争对不需要填写内容的pdf
// 获取文本位置信息
PDFTextStripper textStripper = new PDFTextStripper() {
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
if (text.contains(searchText) && text.contains(searchText1) && text.contains(textDate)) {
for (TextPosition textPosition : textPositions) {
if (text.contains(searchText) && text.contains(searchText1) && textDate.substring(textDate.length() - 2, textDate.length() - 1).equals(textPosition.getUnicode())) {
if (count[0] == 0) {
break;
}
count[0] = count[0] - 1;
// 在这里可以对提取的文本内容进行匹配
// 匹配到了文本内容,获取当前页码
int pageNumber = getCurrentPageNo();
log.info("Matched text found on page: " + pageNumber);
PDPage page = document.getPage(pageNumber - 1);
log.info("text:{}", text);
float x = textPosition.getEndX() + x1;
float y = textPosition.getEndY();
String[] split = formattedDate.split("/");
// 获取第N+1页
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream.newLineAtOffset(x, y);
contentStream.showText(split[0]);
contentStream.endText();
contentStream.close();
x = x + x2;
PDPageContentStream contentStream1 = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream1.beginText();
contentStream1.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream1.newLineAtOffset(x, y);
contentStream1.showText(split[1]);
contentStream1.endText();
contentStream1.close();
x = x + x3;
PDPageContentStream contentStream2 = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream2.beginText();
contentStream2.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream2.newLineAtOffset(x, y);
contentStream2.showText(split[2]);
contentStream2.endText();
contentStream2.close();
break;
}
}
}
}
};
textStripper.setSortByPosition(true);
textStripper.getText(document);
}
else if(type == 3){
//争对只需签一次,日期位于尾部,需要填写内容的pdf
//问卷获取文本位置信息
final float[] x = {0};
final float[] y = {0};
final int[] pageNumber = {0};
PDFTextStripper textStripper = new PDFTextStripper() {
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
if (text.contains(searchText) && text.contains(searchText1) && text.contains(textDate)) {
for (TextPosition textPosition : textPositions) {
if (text.contains(searchText) && text.contains(searchText1) && textDate.substring(textDate.length() - 2, textDate.length() - 1).equals(textPosition.getUnicode())) {
x[0] = textPosition.getEndX() + x1;
y[0] = textPosition.getEndY();
// 在这里可以对提取的文本内容进行匹配
// 匹配到了文本内容,获取当前页码
pageNumber[0] = getCurrentPageNo();
}
}
}
}
};
textStripper.setSortByPosition(true);
textStripper.getText(document);
// 在这里可以对提取的文本内容进行匹配
// 匹配到了文本内容,获取当前页码
log.info("Matched text found on page: " + pageNumber[0]);
PDPage page = document.getPage(pageNumber[0] - 1);
// 获取第N+1页
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream.newLineAtOffset(x[0], y[0]);
contentStream.showText(formattedDate);
contentStream.endText();
contentStream.close();
}else if(type == 4){
//争对只需签一次,日期位于尾部,需要填写内容的pdf
//问卷获取文本位置信息
final float[] x = {0};
final float[] y = {0};
final int[] pageNumber = {0};
PDFTextStripper textStripper = new PDFTextStripper() {
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
if (text.contains(searchText) && text.contains(searchText1) && text.contains(textDate)) {
for (TextPosition textPosition : textPositions) {
if (text.contains(searchText) && text.contains(searchText1) && textDate.substring(textDate.length() - 2, textDate.length() - 1).equals(textPosition.getUnicode())) {
x[0] = textPosition.getEndX() + x1;
y[0] = textPosition.getEndY();
// 在这里可以对提取的文本内容进行匹配
// 匹配到了文本内容,获取当前页码
pageNumber[0] = getCurrentPageNo();
}
}
}
}
};
textStripper.setSortByPosition(true);
textStripper.getText(document);
// 在这里可以对提取的文本内容进行匹配
// 匹配到了文本内容,获取当前页码
log.info("Matched text found on page: " + pageNumber[0]);
PDPage page = document.getPage(pageNumber[0] - 1);
String[] split = formattedDate.split("/");
// 获取第N+1页
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream.newLineAtOffset(x[0], y[0]);
contentStream.showText(split[0]);
contentStream.endText();
contentStream.close();
x[0] = x[0] + x2;
PDPageContentStream contentStream1 = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream1.beginText();
contentStream1.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream1.newLineAtOffset(x[0], y[0]);
contentStream1.showText(split[1]);
contentStream1.endText();
contentStream1.close();
x[0] = x[0] + x3;
PDPageContentStream contentStream2 = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream2.beginText();
contentStream2.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream2.newLineAtOffset(x[0], y[0]);
contentStream2.showText(split[2]);
contentStream2.endText();
contentStream2.close();
}
document.save(fileName);
} catch (Exception e) {
e.printStackTrace();
log.error("addDateToPDF--erroe=={}", e);
throw new RuntimeException("pdf添加日期失败");
}
}