Baxter
Baxter

Reputation: 5835

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.

Upvotes: 7

Views: 8537

Answers (1)

Chris Haas
Chris Haas

Reputation: 55417

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

Upvotes: 18

Related Questions