簡體   English   中英

PDFBOX:使用pdfbox編制索引

[英]PDFBOX: Indexing using pdfbox

我需要為PDF文檔創建索引,並基於要顯示頁面的索引。

問題是頁碼是動態創建的,因此無法使用靜態頁碼,任何人都可以建議我提供鏈接或示例

這是我的代碼,當我在索引頁上單擊頁碼時,它可以重定向到其他頁面

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM