I am in the process of converting the contents page of my PDF from using page numbers as a hyperlink, to anchors because of a few circumstantial limitations and the linking needs to be more dynamic.
I have omitted outer-loop code, but I am attempting to create a hyperlinked entry in the contents page using the following:
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
Anchor anchor = new Anchor("page18 link");
anchor.setReference("#page18");
p.add(anchor);
cell.addElement(p);
table.addCell(cell);
Once the contents page has been generated (i.e. all the rows have been added), I then use the writeSelectedRows
on the table:
table.writeSelectedRows(0, -1, PageSize.A4.getWidth()*.05f, PageSize.A4.getHeight()-100, stamper.getOverContent(prevSectionPageCount+currentIndexPage+1));
On doing this, I get the following exception:
Cause Exception was: Error in StamperPDFPlugin. null java.lang.NullPointerException at com.itextpdf.text.pdf.internal.PdfAnnotationsImp.addPlainAnnotation(PdfAnnotationsImp.java:125) at com.itextpdf.text.pdf.PdfDocument.localGoto(PdfDocument.java:2115) at com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1612) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1025) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1381) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:882) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:866) at com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:549) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:767) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:897) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:845) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:823) at com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:584) at com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:328) at com.ems.rendition.cts.plugin.StamperPDFPlugin.executeProfile(StamperPDFPlugin.java:171)
On seeing the stack trace entry for localGoto
, I took out the line anchor.setReference("#18.pdf");
and it completed fine without error (but obviously with the absence of the hyperlinks - only plain text).
What is going wrong here? Am I adding the anchor to the cell incorrectly?
Thanks