1

I've been trying to create a table of contents which dynamically links to other pages in the PDF using anchors. I've ran into an issue with using stamper.getOverContent() as the canvas parameter in the method table.writeSelectedRows().

table.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(4));

The example below will write a table to page 4 containing an anchor destination, and a table to page 8 containing an anchor hyperlink which links back to page 4. (Note that I'm using tables for this because it is just a part of what ultimately needs to be achieved).

String strFileName = "C:\\link\\to\\existing\\PDF\\document.pdf";

PdfReader reader = new PdfReader(strFileName);
FileOutputStream out = new     FileOutputStream("results/tables/link_in_positioned_table.pdf");

PdfStamper stamper = new PdfStamper(reader, out);
stamper.setRotateContents(true);

//Page 4 table containing destination
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();  
table.setTotalWidth(500);

Anchor target = new Anchor("page 4");
target.setName("page4");
p.add(target);
cell.addElement(p);
table.addCell(cell);
table.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(4));


//Page 8 table containing link
PdfPTable table1 = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell();
Paragraph p1 = new Paragraph();  
table1.setTotalWidth(500);

Anchor anchor = new Anchor("page4 link");
anchor.setReference("#page4");
p1.add(anchor);
cell1.addElement(p1);
table1.addCell(cell1);
table1.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(8));

stamper.close();
reader.close();
out.close();

This works fine if anchor.setReference(); is an external hyperlink, but if it is internal it returns with the following error:

at com.itextpdf.text.pdf.internal.PdfAnnotationsImp.addPlainAnnotation(PdfAnnotationsImp.java:126) at com.itextpdf.text.pdf.PdfDocument.localGoto(PdfDocument.java:2178) at com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1643) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1162) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:994) at com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1541) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1000) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:994) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:982) at com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:583) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:833) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:966) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:912) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:891) at com.ems.rendition.cts.plugin.StamperPDFPlugin.createPdf(StamperPDFPlugin.java:1727) at com.ems.rendition.cts.plugin.StamperPDFPlugin.main(StamperPDFPlugin.java:1684)

Am I doing something wrong? Or is this an issue?

Note: I asked a similar question here - iTextPDF - Cannot use writeSelectedRows() on a table where an anchor has been inserted which didn't identify that getOverContent() was the issue and instead focussed on the actual implementation of the anchors.

Community
  • 1
  • 1
Smittey
  • 2,475
  • 10
  • 28
  • 35

0 Answers0