How do I insert a hyperlink to another page with iTextSharp in an existing PDF?

限于喜欢 提交于 2019-11-29 16:21:52

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!