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.
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.
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);
来源:https://stackoverflow.com/questions/13448853/how-do-i-insert-a-hyperlink-to-another-page-with-itextsharp-in-an-existing-pdf