插入“链接的矩形”用itext [英] Inserting a "linked rectangle" with itext

查看:168
本文介绍了插入“链接的矩形”用itext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我事先知道的位置插入现有PDF的超链接:我已经在给定页面上有一个矩形的坐标。我想将此矩形链接到同一PDF的另一页(我也提前知道)。



我如何实现这一目标?

解决方案

请查看 AddLinkAnnotation 示例。



正如您(应该)已经知道的那样(但您没有展示您已尝试过的内容,这在StackOverflow上是强制性的) ,您可以使用 PdfStamper 来操作现有的PDF。将一个页面上的矩形链接添加到另一个页面就像向该页面添加链接注释一样简单:

  PdfReader reader = new PdfReader(src); 
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(dest));
Rectangle linkLocation = new Rectangle(523,770,559,806);
PdfDestination destination = new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation,PdfAnnotation.HIGHLIGHT_INVERT,
3,destination);
link.setBorder(new PdfBorderArray(0,0,0));
stamper.addAnnotation(link,1);
stamper.close();

链接对象是使用以下方式创建的:




  • 绑定到压模的编写实例

  • 矩形(您提前知道的位置,

  • 突出显示选项(选择一个:HIGHLIGHT_NONE,HIGHLIGHT_INVERT, HIGHLIGHT_OUTLINE,HIGHLIGHT_PUSH,HIGHLIGHT_TOGGLE),

  • 您要链接的页面,

  • 目的地(可能有不同的选项,请参阅 PDF的ABC )。



一旦有了 PdfAnnotation 的实例,就可以使用 addAnnotation()方法将其添加到特定页面。 / p>

I want to insert a hyperlink into an existing PDF at a position I know in advance: I already have the coordinates of a rectangle on a given page. I want to link this rectangle to another page of the same PDF (which I also know in advance).

How do I achieve this?

解决方案

Please take a look at the AddLinkAnnotation example.

As you (should) already know (but you didn't show what you've already tried, which is kind of mandatory on StackOverflow), you can use PdfStamper to manipulate an existing PDF. Adding a rectangular link on one page to another page, is as simple as adding a link annotation to that page:

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Rectangle linkLocation = new Rectangle(523, 770, 559, 806);
PdfDestination destination = new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
        linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
        3, destination);
link.setBorder(new PdfBorderArray(0, 0, 0));
stamper.addAnnotation(link, 1);
stamper.close();

The link object is created using:

  • the writer instance tied to the stamper,
  • the rectangle (the position you say you know in advance,
  • a highlighting option (pick one: HIGHLIGHT_NONE, HIGHLIGHT_INVERT, HIGHLIGHT_OUTLINE, HIGHLIGHT_PUSH, HIGHLIGHT_TOGGLE),
  • the page you want to link to,
  • a destination (different options are possible, see The ABC of PDF).

Once you have an instance of PdfAnnotation, you can add it to a specific page using the addAnnotation() method.

这篇关于插入“链接的矩形”用itext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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