Skip to content

PDFBox 2.0.9 – creating TOC with multiple pages

I’m working on PDF Toc. It generates the first page but when I have more elements I did logic to create a new page for TOC. I’m using PDF Box and that PDPageContentStream. I had to create a function to calculate how many pages I need. Then I’m creating the exact amount of pages in the list and add them to the PDF document before I start PDPageContentStream. That stream is in a loop and it’s only generating the first page. Other pages come blank. I don’t know what exactly is wrong. Here is the code:

PDDocument pdf = new PDDocument();
int numberOfTocPages = calculateTocPages(50, tocStyle, _height, currentYPosition, leading);

    List<PDPage> tocPages = new ArrayList<PDPage>();
    for (int i = 0; i < numberOfTocPages; i++) {
        PDPage toc = new PDPage(new PDRectangle(_width, _height));
        tocPages.add(toc);
    }

    float numberingXPosition = _width - (contentMarginLeft + contentMarginRight) - 100;
    int j = 0;
    
    PDDocument temp = new PDDocument();
    
    
    for (int i = 0; i < numberOfTocPages; i++) {
        PDPage toc = tocPages.get(i);
        pdf.addPage(toc);
        try {
            PDPageContentStream contentStream = new PDPageContentStream(pdf, toc, PDPageContentStream.AppendMode.APPEND, true, false);
            contentStream.beginText();
            if (i == 0) {
                contentStream.setFont(font, fontSize);
                contentStream.setNonStrokingColor(tocStyle.getHeaderFontColor());
                contentStream.newLineAtOffset(headerMarginLeft, currentYPosition);
                contentStream.showText(StylingEnums.TABLE_OF_CONTENTS);
            }
            
            fontSize = tocStyle.getContentFontSize();
            leading = 1.5f * fontSize;
            contentStream.setFont(PDType1Font.HELVETICA, fontSize);
            contentStream.setNonStrokingColor(tocStyle.getContentFontColor());
            contentStream.newLineAtOffset(contentMarginLeft, -leading);
            currentYPosition -= leading;
            contentStream.setLeading(leading);
            // List<TocContentData> tocItems = _tocData.getTocItems();
            while (j < 50) {
                if (currentYPosition >= 2 * tocStyle.getContentMarginBottom()) {
                    // TocContentData item = tocItems.get(i);
                    contentStream.showText("Item " + j);
                    contentStream.newLineAtOffset(numberingXPosition, 0);
                    int pageNumber = j;
                    contentStream.showText(Integer.toString(pageNumber + 1));
                    contentStream.newLineAtOffset(-numberingXPosition, 0);
                    contentStream.newLine();
                    currentYPosition -= leading;
                    j++;
                } else {
                    System.out.println("New page!!!");
                    currentYPosition = initialPosition;
                    break;
                }
                
            }
            
            contentStream.endText();
            contentStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

    FileOutputStream out;
    try {
        out = new FileOutputStream(fileName + ".pdf");
        pdf.save(out);
        out.close();
        
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Answer

Page 2 sets the Y offset with this code because currentYPosition is not used when i > 0

contentStream.newLineAtOffset(contentMarginLeft, -leading);

so your first Y offset value is negative.