【问题标题】:PDFBOX: Indexing using pdfboxPDFBOX:使用 pdfbox 进行索引
【发布时间】:2017-11-23 08:55:12
【问题描述】:

我需要为 PDF 文档创建索引并根据我想要显示页面的索引。

问题是页码是动态创建的,所以不能使用静态页码,谁能给我建议链接或示例

【问题讨论】:

  • @mkl 你能给我一些例子吗
  • 说实话,我不太了解您的任务以及执行任务时遇到的问题。
  • @mkl 我想用锚链接创建目录。单击链接参考页面时应显示。
  • 您是否尝试过源代码下载中的 AddAnnotations.java 示例?如果是,请重写您的问题以澄清问题所在。

标签: java spring pdf-generation open-source pdfbox


【解决方案1】:

这是我的代码,它可以让我在点击索引页上的页码时重定向到不同的页面

public class LinkPdf {

static final float INCH = 72;
public static List<PDAnnotation> annotations;

@SuppressWarnings("null")
public static void main(String[] args) throws Exception {
    PDRectangle position = null;
    PDDocument document = new PDDocument();
    try {
        for (int i = 0; i < 10; i++) {
            PDPage page = new PDPage();
            document.addPage(page);
        }
        PDPage page1 = document.getPage(0);

        HashMap<Integer, String> pgs = new HashMap<Integer, String>();

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            pgs.put(i, "Jump to Page" + i);

        }

        PDFont font = PDType1Font.HELVETICA_BOLD;
        PDPageContentStream contents = new PDPageContentStream(document,
                page1);
        contents.beginText();
        contents.setFont(font, 18);
        contents.newLineAtOffset(50, 600);
        contents.setLeading(20f);
        contents.showText("PDFBox");

        position = new PDRectangle();
        for (int i = 0; i < document.getNumberOfPages(); i++) {

            contents.newLine();
            contents.showText(pgs.get(i));
            contents.newLine();
        }
        contents.endText();
        contents.close();

        PDRectangle[] pos1 = new PDRectangle[document.getNumberOfPages()];
        for(int i=0;i<document.getNumberOfPages();i++){
            pos1[i]=new PDRectangle();
            pos1[i].setLowerLeftX(50);
            pos1[i].setLowerLeftY(575-(i*40)); 
            pos1[i].setUpperRightX(INCH + 100);
            pos1[i].setUpperRightY(585-(i*40));
            getPage(document, page1, pgs.get(i), i, pos1[i]);
        }

        document.save("D:/link.pdf");
        System.out.println("Completed");
    } finally {
        document.close();
    }
}

public static void getPage(PDDocument document, PDPage page1, String txt,
        int pageno, PDRectangle position) throws IOException {
    annotations = page1.getAnnotations();
    PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
    borderThick.setWidth(INCH / 12); // 12th inch

    PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
    borderThin.setWidth(INCH / 72); // 1 point

    PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
    borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
    borderULine.setWidth(INCH / 72); // 1 point

    float pw = page1.getMediaBox().getUpperRightX();
    float ph = page1.getMediaBox().getUpperRightY();
    float textWidth = PDType1Font.TIMES_BOLD.getStringWidth("PDFBox") / 1000 * 18;

    PDAnnotationLink pageLink = new PDAnnotationLink();
    pageLink.setBorderStyle(borderULine);
    textWidth = PDType1Font.TIMES_BOLD.getStringWidth(txt) / 1000 * 18;

    pageLink.setRectangle(position);
    PDActionGoTo actionGoto = new PDActionGoTo();
    PDPageDestination dest = new PDPageFitWidthDestination();
    dest.setPage(document.getPage(pageno));
    actionGoto.setDestination(dest);
    pageLink.setAction(actionGoto);
    annotations.add(pageLink);
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多