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

╄→尐↘猪︶ㄣ 提交于 2019-11-28 10:05:19

问题


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.


回答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.




回答2:


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

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