【问题标题】:How do I insert a hyperlink to another page with iTextSharp in an existing PDF?如何在现有 PDF 中使用 iTextSharp 插入指向另一个页面的超链接?
【发布时间】:2012-11-07 01:59:18
【问题描述】:

我想添加一个指向现有 pdf 的链接,该链接可以跳转到另一个页面上的坐标。

我可以使用以下代码添加一个矩形:

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();

我该如何做类似的事情来创建一个可点击的链接?谢谢。

【问题讨论】:

  • 考虑标记已接受的答案或评论现有的为什么它们不适合您。

标签: c# .net hyperlink itextsharp


【解决方案1】:

这在"iText in Action - Second Edition" 一书的第 7 章中有解释。你可以在这里找到一个例子:http://itextpdf.com/examples/iia.php?id=150

如果需要C#版本,请看这里:http://kuujinbo.info/iTextInAction2Ed/index.aspx

更具体地说: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);

在此代码示例中,page 是您要添加链接的页面编号,rect 是定义该页面坐标的Rectangle 对象。

【讨论】:

    【解决方案2】:

    我喜欢用表格构建我的 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);
    

    或者如果你想要它没有边界

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多