[英]How do I insert a hyperlink to another page with iTextSharp in an existing PDF?
I would like to add a link to an existing pdf that jumps to a coordinate on another page. 我想添加一个指向现有pdf的链接,该链接跳至另一页上的坐标。
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" . 在“ iText in Action-Second Edition”一书的第7章中对此进行了解释。 You can find an example here: http://itextpdf.com/examples/iia.php?id=150
您可以在此处找到示例: 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 如果您需要C#版本,请在此处查看: http : //kuujinbo.info/iTextInAction2Ed/index.aspx
More specifically: http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter07&ex=TimetableAnnotations2 更具体地说: 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. 在此代码示例
page
是您要添加链接的页面的编号,而rect
是定义该页面上坐标的Rectangle
对象。
I like building my PDFs with tables and this is the code I use 我喜欢用表格构建PDF,这是我使用的代码
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.