3

I would like to add a link to an existing pdf that jumps to a coordinate on another page.

I am able to add a rectangle using this code:

PdfContentByte overContent = stamper.GetOverContent(1);
iTextSharp.text.Rectangle rectangle = new Rectangle(10,10,100,100,0);
rectangle.BackgroundColor = BaseColor.BLUE;
overContent.Rectangle(rectangle);
stamper.Close();

How can I do similar to create a link that is clickable? Thanks.

dazzler77
  • 33
  • 1
  • 5
  • Consider to mark accepted answer or comment on existing why they are not an answer for you. – Sinatr Jul 06 '15 at 08:19

2 Answers2

1

This is explained in chapter 7 of the book "iText in Action - Second Edition". You can find an example here: http://itextpdf.com/examples/iia.php?id=150

If you need the C# version, please take a look here: http://kuujinbo.info/iTextInAction2Ed/index.aspx

More specifically: http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter07&ex=TimetableAnnotations2

PdfAnnotation annotation = PdfAnnotation.CreateLink(
    stamper.Writer, rect, PdfAnnotation.HIGHLIGHT_INVERT,
    new PdfAction("http://itextpdf.com/")
);
stamper.AddAnnotation(annotation, page);

In this code sample page is the number of the page where you want to add the link and rect is the Rectangle object defining the coordinates on that page.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
1

I like building my PDFs with tables and this is the code I use

PdfPCell cell = new Chunk anchor = new Chunk("Name of link", font);
anchor.SetAnchor("PageName.aspx");
cell.AddElement(new Phrase(anchor));
cell.BorderColor = BaseColor.BLACK; 
cell.Padding = 5;
table.AddCell(cell);

or if you want it borderless

PdfPCell cell = new Chunk anchor = new Chunk("Name of link", font);
anchor.SetAnchor("PageName.aspx");
cell.AddElement(new Phrase(anchor));
cell.Border = Rectangle.NO_BORDER;
table.AddCell(cell);