我通过 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();
谢谢
重点是
createXYZ(pdf.getPage(1), 0, 0, 0)
。
应该是createXYZ(firstPage, 0, firstPage.getPageSize().getHeight(), 1)
。