将超链接添加到PDF文档中 [英] Add a hyperlink into a PDF document

查看:88
本文介绍了将超链接添加到PDF文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在扩展自定义PDF编写器,以便能够编写到网站的链接.但是,我有一个问题,因为我在任何地方都找不到如何将链接放置到PDF中.

I'm currently extending our custom PDF writer to be able to write links to websites. However, I have a problem because I can't find anywhere how to place a link into the PDF.

这是打印文本的内容:

BT
70 50 TD
/F1 12 Tf
(visit my website!) Tj
ET

我现在需要的是将其包装为超链接,以便在单击访问我的网站!"时将用户重定向到我的网站.

What I need now is to wrap this into a hyperlink so the user gets redirected to my website, when clicking "visit my website!"

任何想法怎么做?我不能使用某种工具-我需要知道如何将正确的PDF命令写入文件,因为许多文档是使用C#动态生成的.目前,我正在使用iTextSharp-但找不到任何功能来编写超链接,所以我决定添加此功能.

Any idea how to do so? I can't use a tool or so - I need to know how to write the right PDF commands to the file, since a lot of documents are generated dynamically using C#. Currently I'm using iTextSharp - but I couldn't find any functionality to write a hyperlink, so I decided to add this functionality.

推荐答案

这是规范方面的内容:通过在页面上放置链接注释"来创建链接.链接注释由Rect键或一组四边形表示.假设您正在使用矩形.为了放置链接,您至少需要一个这样的字典:

Here's how the spec side of things: Links are created by having Link Annotations placed on the page. A link annotation is represented by either the Rect key or by a set of quadrilaterals. Let's assume that you're working with rectangles. In order to place the link, you'll need a dictionary like this at a minimum:

<< /Type /Annot /Subtype /Link /Rect [ x1 y1 x2 y2 ] >>

(x1,y1)和(x2,y2)描述了链接的活动区域所在的矩形的角.

(x1, y1) and (x2, y2) describe the corners of the rectangle where the link's active area lives.

要对此进行处理,这应该是PDF中的一个间接对象,并应从页面的Annots数组中引用.

To work with this, this should be an indirect object in the PDF and referenced from your page's Annots array.

如果可以创建此链接,则会在页面上无任何链接.

If you can create this, you'll get a link on the page that goes nowhere.

要使链接转到某个地方,您需要在链接提示中添加/Dest或/A条目(但不能同时包含). /Dest是用于页面级导航的较旧的工件-您将不会使用它.而是使用/A条目(它是动作字典).因此,如果您想导航到URL http://www.google.com ,则可以使注释看起来像像这样:

To get the link to go somewhere you'll need either a /Dest or an /A entry in the link annot (but not both). /Dest is an older artifact for page-level navigation - you won't use this. Instead, use the /A entry which is an action dictionary. So if you wanted to navigate to the url http://www.google.com, you would make your annotation look like this:

<< /Type /Annot /Subtype /Link /Rect [ x1 y1 x2 y2 ]
   /A << /Type /Action /S /URI /URI (http://www.google.com) >>
>>

我无法在iTextSharp中专门为您提供帮助.我不特别喜欢它们使用的模型或抽象.我为Atalasoft编写了一个PDF工具包,我将向您展示如何在自己的工具包中做到这一点.再说一次,我毫不掩饰这是一种商业产品,而这正是我谋生的目的.我只希望您看到还有其他可用的选项.

I can't help you specifically with how to do this in iTextSharp. I don't particularly like the model or abstraction that they use. I write a PDF toolkit for Atalasoft and I'll show you how I would do that in my own toolkit. Again, I'm making no effort to conceal that this is a commercial product and it's what I do for a living. I just want you to see that there are other options available.

// make a document, add a font, get its metrics
PdfGeneratedDocument doc = new PdfGeneratedDocument();
string fontResource = doc.Resources.Fonts.AddFromFontName("Times New Roman");
PdfFontMetrics mets = doc.Resources.Fonts.Get(fontResource).Metrics;

// make a page, place a line of text
PdfGeneratedPage page = doc.Pages.AddPage(PdfDefaultPages.Letter);
PdfTextLine line = new PdfTextLine(fontResource, 12.0, "Visit my web site.",
                        new PdfPoint(72, 400));
page.DrawingList.Add(line);

// get the bounds of the text we place, make an annotation
PdfBounds bounds = mets.GetTextBounds(12.0, "Visit my web site.");
bounds = new PdfBounds(72, 400, bounds.Width, bounds.Height);
LinkAnnotation annot = new LinkAnnotation(bounds, new PdfURIAction(new URI("my url")));
page.Annotations.Add(annot);

// save the content
doc.Save("finaldoc.pdf");

唯一的棘手"是页面上的内容与链接注释之间没有关联-但这是因为这是Acrobat建模链接的方式.如果要修改现有文档,则可以从现有文件/流构造PdfGeneratedDocument,添加注释,然后保存.

The only thing that is "tricky" is that there is a disassociation between what content is on the page and the link annotation - but this is because this is how Acrobat models links. If you were modifying an existing document, you would construct a PdfGeneratedDocument from the existing file/stream, add the annotation and then save.

这篇关于将超链接添加到PDF文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆