1

I am trying to push the content to page 2 once page 1 is full but currently my code is just writing on page 1 and not able to create a second page automatically and thus losing the content.

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();
        }
    }
}

after this i am using outputByteArrayStream to store the content and return the byteArray[].

this is the error I am getting

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
sidgate
  • 14,650
  • 11
  • 68
  • 119
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – sidgate Jul 12 '19 at 00:09
  • Well I know it is becuase of my code for page 2. if the content just fits in page 1 their is no null pointer exception and thus I want to understand the approach towards creating and writing to page 2 in pdf using PDFBOX – Naveen Singh Jul 12 '19 at 00:30
  • at which line are you getting NPE? – sidgate Jul 12 '19 at 00:33
  • 1
    contentStream1.setFont(pdfFont, fontSize); in the last block – Naveen Singh Jul 12 '19 at 00:42
  • 1
    why are you using different contentStream for same pdf? write on the same stream – sidgate Jul 12 '19 at 00:46
  • I tried to look in the source code at PDPageContentStream.java:321 and couldn't find it - what PDFBox version are you using? – Tilman Hausherr Jul 12 '19 at 03:48
  • I suspect that you using "contentStream1.close();" and then continued to write on it in the loop is the cause for the NPE. You can't write into the content stream after closing it. – Tilman Hausherr Jul 12 '19 at 03:50
  • Closing the `contentStream1` in the loop is but one of many issues of the code, even though it most likely is the cause of the exception. You had better start from scratch. – mkl Jul 12 '19 at 06:18
  • The usage of "contentStream" and "contentStream1" is extremely confusing for the eye, even I got confused a few times. Use names that make sense, or try to use only one content stream at a time. And try to split your code into smaller methods. – Tilman Hausherr Jul 12 '19 at 07:01
  • I am new to this PDFBOX so just trying understand whether I need a different content stream or same one. I see post from past and they have mentioned using a new contentsteam . – Naveen Singh Jul 12 '19 at 13:10
  • Yes you do need a different content stream for a different page. The criticism was about the confusing coding style, i.e. after fixing the bug(s) you need to do some refactoring. Did you understand my comment about the cause for the null pointer exception? – Tilman Hausherr Jul 13 '19 at 09:49

0 Answers0