索引文档中的 PDFbox 重叠链接

PDFbox overlaped links at index document

我是 PDFBox 的新手,我遇到了一个问题,目前找不到解决方法。

我从我的数据库中获取文件夹和位于这些文件夹中的文档的列表,我遍历所有这些数据以生成一个索引,该索引具有活动的 links 到正确的 folder/document 路径。 (想象一下我有一个根文件夹,我想在该根目录中有一个 pdf 索引,其中包含相对于所有文件夹和文档的 links)

主要代码如下:

    try {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage();

        document.addPage(page);

        PDFont font = PDType1Font.HELVETICA;


        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString("Índice " + expediente.getNombre());
        contentStream.endText();


        int i = 0;
        for (Folder currFol : root.getFolders()) {
            for (Document currDoc : currFol.getDocuments()) {
                i++;

                float x = 50;
                float y = 250;
                String text = currFol.getName() + "/" + currDoc.getName();
                float textWidth = font.getStringWidth(text) / 1000 * 12;

                PDAnnotationLink link = new PDAnnotationLink();
                PDGamma colourBlue = new PDGamma();
                colourBlue.setB(1);
                link.setColour(colourBlue);

                // add an action
                PDActionURI action = new PDActionURI();
                action.setURI(currFol.getName() + "/" + currDoc.getName());

                link.setAction(action);

                contentStream.beginText();
                contentStream.setFont(font, 12);
                contentStream.moveTextPositionByAmount(x, y);
                contentStream.drawString(text);
                contentStream.endText();

                PDRectangle position = new PDRectangle();
                position.setLowerLeftX(x);
                position.setLowerLeftY(y -(i* 5));
                position.setUpperRightX(x + textWidth);
                position.setUpperRightY(y + 50);
                link.setRectangle(position);

                page.getAnnotations().add(link);
            }
        }
        // Make sure that the content stream is closed:

        contentStream.close();

        document.save(output);

        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

我的问题是所有元素都打印重叠,文本和框彼此重叠,目前无法找到如何正确打印列表格式样式中的所有 link 来创建索引。

如有任何想法或建议,我们将不胜感激。

我尝试了一些教程,但目前没有成功,比如 http://www.programcreek.com/java-api-examples/index.php?api=org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink, http://www.massapi.com/class/pd/PDAnnotationLink.html .

我试过没有 PDRectangle,只有文本 link(因为 PDRectangle 在我找到的所有示例中都存在,但我真的不需要它)

谢谢,

在您的内部循环中,您始终将 xy 设置为相同的值

float x = 50;
float y = 250;

此后您不会更改。然后从 xy

开始绘制相应的文本
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(x, y);
contentStream.drawString(text);
contentStream.endText();

因此,您绘制的每个条目都从相同的坐标开始。因此,所有文本重叠也就不足为奇了。

此外,您还可以像这样设置 link 的位置和大小:

PDRectangle position = new PDRectangle();
position.setLowerLeftX(x);
position.setLowerLeftY(y -(i* 5));
position.setUpperRightX(x + textWidth);
position.setUpperRightY(y + 50);
link.setRectangle(position);

因此,每个link的上y坐标总是y + 50,即300,而下 links 的 ]y 坐标每次迭代向下移动 5。因此,您的第一个 link 的垂直范围包含在第二个的垂直范围中,而第二个的垂直范围又包含在第三个的垂直范围中,等等。同样,这些注释重叠也就不足为奇了。

因此,这与成为 PDFBox 新手无关,而仅与正确设置坐标有关...;)


不如这样:

float x = 50;
float y = 650;

for (Folder currFol : root.getFolders()) {
    for (Document currDoc : currFol.getDocuments()) {
        String text = currFol.getName() + "/" + currDoc.getName();
        float textWidth = font.getStringWidth(text) / 1000.0 * 12;

        PDAnnotationLink link = new PDAnnotationLink();
        PDGamma colourBlue = new PDGamma();
        colourBlue.setB(1);
        link.setColour(colourBlue);

        // add an action
        PDActionURI action = new PDActionURI();
        action.setURI(currFol.getName() + "/" + currDoc.getName());

        link.setAction(action);

        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(x, y);
        contentStream.drawString(text);
        contentStream.endText();

        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(x);
        position.setLowerLeftY(y - 3);
        position.setUpperRightX(x + textWidth);
        position.setUpperRightY(y + 12);
        link.setRectangle(position);

        page.getAnnotations().add(link);

        y -= 15;
    }
}