通过itext9.0.0创建pdf大纲目标到第一页,但跳转到第二页

问题描述 投票:0回答:1

我通过 createXYZ(pdf.getPage(1), 0, 0, 0) 创建了一个到第一页的大纲“paragraph1”目标,但是当单击大纲时跳转到第 2 页的顶部。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(baos);

        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf, PageSize.A4);

        Paragraph paragraph1 = new Paragraph("paragraph1").setDestination("paragraph1");
        document.add(paragraph1);
        PdfDestination dest = PdfExplicitDestination.createXYZ(pdf.getPage(1), 0, 0, 0);
        PdfOutline coverPageOutline = pdf.getOutlines(true).addOutline("paragraph1 page");
        coverPageOutline.addDestination(dest);

        document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));


        Paragraph paragraph2 = new Paragraph("paragraph2").setDestination("paragraph2");
        document.add(paragraph2);
        dest = new PdfStringDestination("paragraph2");
        PdfOutline outline = pdf.getOutlines(true).addOutline("paragraph2 string");
        outline.addDestination(dest);

        document.flush();
        document.close();
        pdf.close();

谢谢

itext outline
1个回答
0
投票

重点是

createXYZ(pdf.getPage(1), 0, 0, 0)
。 应该是
createXYZ(firstPage, 0, firstPage.getPageSize().getHeight(), 1)

© www.soinside.com 2019 - 2024. All rights reserved.