Search code examples
pdfhyperlinkanchoritextcell

iTextSharp Use Link Inside PdfPCell


I am able to successfully put a link in the pdf with a friendly name:

            Anchor anchor = new Anchor("Google", linkFont);
            anchor.Reference = "https://www.google.com";
            doc.Add(anchor);

enter image description here

However, I cannot get get the anchor to work within a PdfPCell.
Here is what I have tried so far:

            var memberCell = new PdfPCell();
            Anchor anchor = new Anchor("Google", linkFont);
            anchor.Reference = "https://www.google.com";
            memberCell.AddElement(new Anchor(anchor));

That displays the exception: System.ArgumentException: Element not allowed.

I also tried:

            var memberCell = new PdfPCell();
            Anchor anchor = new Anchor("Google", linkFont);
            anchor.Reference = "https://www.google.com";
            memberCell.AddElement(new Phrase(anchor));

This does not throw an exception but it isn't a link it is just the word "Google".

I am using the newest version of iTextSharp at this time v.(5.4.4.0)
Any help on this would be greatly appreciated.


Solution

  • Instead of an Anchor use a Chunk and call the SetAnchor() method:

    var c = new Chunk("Google");
    c.SetAnchor("https://www.google.com");
    memberCell.AddElement(c);