Java 当第1页已满时,自动创建第2页pdf,并在PDFBOX中写入第2页

Java 当第1页已满时,自动创建第2页pdf,并在PDFBOX中写入第2页,java,pdfbox,Java,Pdfbox,我试图在第1页已满时将内容推到第2页,但目前我的代码只是在第1页上编写,无法自动创建第二页,因此会丢失内容 private static void insertStringEmail1(PDDocument doc, PDPage page, PDFont font, float fontSize, float statVarX, float statVarY, String text) throws IOException { PDPageContentStrea

我试图在第1页已满时将内容推到第2页,但目前我的代码只是在第1页上编写,无法自动创建第二页,因此会丢失内容

private static void insertStringEmail1(PDDocument doc, PDPage page, PDFont font,
          float fontSize, float statVarX, float statVarY, String text) throws IOException {

    PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true, true);
    PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
    PDPageContentStream contentStream1 = new PDPageContentStream(doc, page);

    PDFont pdfFont = PDType1Font.COURIER;
    PDFont fontBold = PDType1Font.TIMES_BOLD;
    float leading = 1.5f * fontSize;
    PDRectangle mediabox = page.getMediaBox();
    float margin = 40;
    float width = mediabox.getWidth() - 2 * margin;
    float height = mediabox.getHeight() - 2 * margin;
    float startX = mediabox.getLowerLeftX() + margin - statVarX;
    float startY = mediabox.getUpperRightY() - margin - statVarY;
    float heightcounter = startY;

    List<String> lines = new ArrayList<String>();
    int lastSpace = -1;
    String bold;
    String[] arrOfStr;
    text = text.replace("<BR><BR>", "<BR>");
    System.out.println(text);


    arrOfStr = text.split("<BR>");
    for (String a1 : arrOfStr) {
        String strRegEx = "<[^>]*>";
        String a = a1.replaceAll(strRegEx, " ");
        while (a.length() > 0) {
            int spaceIndex = a.indexOf(' ', lastSpace + 1);
            if (spaceIndex < 0)
                spaceIndex = a.length();
            String subString = a.substring(0, spaceIndex);
            float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
            //System.out.printf("'%s' - %f of %f\n", subString, size, width);
            if (size > width) {
                if (lastSpace < 0)
                    lastSpace = spaceIndex;
                subString = a.substring(0, lastSpace);
                lines.add(subString);
                a = a.substring(lastSpace).trim();
                // System.out.printf("'%s' is line\n", subString);
                lastSpace = -1;
            } else if (spaceIndex == a.length()) {
                lines.add(a);
                //System.out.printf("'%s' is line\n", text);
                a = "";
            } else {
                lastSpace = spaceIndex;
            }
        }
        lines.add("");
    }
    contentStream.beginText();
    contentStream.setFont(pdfFont, fontSize);
    contentStream.moveTextPositionByAmount(startX, startY);

    for (String line : lines) {
        //String parsedline = line.replaceAll(strRegEx, " ");
        line = line.trim();
        float charSpacing = 0;
        if (line.length() > 1) {
            float size = fontSize * pdfFont.getStringWidth(line) / 1000;
            float free = width - size;
            if (free > 0) {
                charSpacing = free / (line.length() - 1);
            }
        }

        contentStream.drawString(line);
        contentStream.moveTextPositionByAmount(0, -leading);
        heightcounter++;


    }//this part is added for page 2
    if (heightcounter > height) {
        contentStream.endText();
        contentStream.close();
        page = new PDPage();
        doc.addPage(page);
        contentStream = new PDPageContentStream(doc, page);
        contentStream.beginText();


        for (String line : lines) {
            doc.addPage(page);
            contentStream1.setFont(pdfFont, fontSize);
            contentStream1.beginText();
            contentStream1.moveTextPositionByAmount(startX, startY);
            contentStream1.drawString(line);
            contentStream1.moveTextPositionByAmount(0, -leading);
            contentStream1.endText();
            contentStream1.close();
        }
    }
}

我知道这可能是因为我的代码在第2页。如果内容正好适合第1页,那么他们没有空指针异常,因此我想了解使用PDFBOXat在第2页中创建和写入pdf的方法,您得到的是哪一行NPE?contentStream1.setFont(pdfFont,fontSize);在最后一块中,为什么对同一个pdf使用不同的contentStream?写在同一条流上可能是重复的我知道这是因为我的代码第2页。如果内容正好适合第1页,那么他们没有空指针异常,因此我想了解使用PDFBOXat在第2页中创建和写入pdf的方法,您得到的是哪一行NPE?contentStream1.setFont(pdfFont,fontSize);在最后一块中,为什么对同一个pdf使用不同的contentStream?在同一个流上写入
java.lang.NullPointerException
    at org.apache.pdfbox.pdmodel.edit.PDPageContentStream.setFont(PDPageContentStream.java:321)
    at ca.muhc.ri.account.pdf.PdfGenerator.insertStringEmail1(PdfGenerator.java:1224)
    at ca.muhc.ri.account.pdf.PdfGenerator.generatePdfFromEmail(PdfGenerator.java:1083)
    at ca.muhc.ri.account.manager.TaskTrackingManager.notifyByEmail(TaskTrackingManager.java:512)
    at ca.muhc.ri.account.manager.TaskTrackingManager.onDialogSendNotif(TaskTrackingManager.java:328)
    Truncated. see log file for complete stacktrace