1

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

Smittey
  • 2,475
  • 10
  • 28
  • 35

1 Answers1

1

Please take a look at LinkInPositionedTable:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    Anchor target = new Anchor("top");
    target.setName("page18");
    document.add(target);
    PdfPTable table = new PdfPTable(1);
    table.setTotalWidth(500);
    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);
    table.writeSelectedRows(0, -1, 36, 700, writer.getDirectContent());
    document.close();
}

In this example, I create an anchor with name page18 (although it just refers to the top of the page) and a reference to that anchor added to a PdfPTable using your code snippet.

You can find the result here: link_in_positioned_table.pdf

This works for me, when using iText 5.5.7 (which is the most recent version).

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks for the code sample, Bruno. I upgraded the project to use v.5.5.7, tested your code locally and it worked (it also worked locally with v.5.4.5). I then went to test the project build to find that the error is still there. It only seems to have an issue when `setRefference()` is included which is bizarre, as the structure is as above! Do you have any ideas what could potentially cause this? I'm going to try building it up from the bare structure to try and identify where the issue lies. – Smittey Nov 11 '15 at 13:52
  • From stripping it down, it appears that the canvas argument of `writeSelectedRows()` is what is causing the problem. The only difference between the example and our solution is the use of `stamper.getOverContent()` as opposed to `writer.getDirectContent()`. – Smittey Nov 11 '15 at 15:54
  • Hi Bruno, there is definitely a problem with `writeSelectedRows()` containing local anchors using `.getOverContent()` as the `canvas`. I may be doing something wrong, but it doesn't look like it. I'll post a new question on StackOverflow for clarity. – Smittey Nov 12 '15 at 13:56