pdf根据文字定位添加日期

 //需要填写信息
                    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添加日期失败");
        }
    }

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值