PDFBox 2.0.9 - 创建多页目录

PDFBox 2.0.9 - creating TOC with multiple pages

我正在处理 PDF Toc。它会生成第一页,但当我有更多元素时,我会按照逻辑为 TOC 创建一个新页面。我正在使用 PDF Box 和 PDPageContentStream。 我必须创建一个函数来计算我需要多少页。然后我在列表中创建确切数量的页面,并在我启动 PDPageContentStream 之前将它们添加到 PDF 文档。该流处于循环中,并且仅生成第一页。其他页面空白。我不知道到底出了什么问题。这是代码:

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

第 2 页使用此代码设置 Y 偏移量,因为当 i > 0 时不使用 currentYPosition

contentStream.newLineAtOffset(contentMarginLeft, -leading);

所以你的第一个 Y 偏移值是负数。