javaitextdestination

Create pdf outline dest to first page by itext9.0.0, but jump to 2th page


I create a outline "paragraph1" target to first page by createXYZ(pdf.getPage(1), 0, 0, 0),but jump to top of page 2 when click the outline.

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

Solution

  • The point is createXYZ(pdf.getPage(1), 0, 0, 0). It should be createXYZ(firstPage, 0, firstPage.getPageSize().getHeight(), 1).