ITextShape Clickable Polygon or path

流过昼夜 提交于 2019-11-29 17:42:21

The secret ingredient you are looking for is called QuadPoints ;-)

Allow me to explain how QuadPoints are used by showing you the AddPolygonLink example.

You've already discovered how to construct and draw a path, for instance:

canvas.moveTo(36, 700);
canvas.lineTo(72, 760);
canvas.lineTo(144, 720);
canvas.lineTo(72, 730);
canvas.closePathStroke();

I am using this code snippet only to show the irregular shape that we'll make clickable.

You already know how to create a clickable link with a rectangular shape:

Rectangle linkLocation = new Rectangle(36, 700, 144, 760);
PdfDestination destination = new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
    linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
    1, destination);

This corresponds with the code snippet you already provided in your question.

Now let's introduce some QuadPoints:

PdfArray array = new PdfArray(new int[]{72, 730, 144, 720, 72, 760, 36, 700});
link.put(PdfName.QUADPOINTS, array);

According to ISO-32000-1, QuadPoints are:

An array of 8 × n numbers specifying the coordinates of n quadrilaterals in default user space that comprise the region in which the link should be activated. The coordinates for each quadrilateral are given in the order

x1 y1 x2 y2 x3 y3 x4 y4

specifying the four vertices of the quadrilateral in counterclockwise order. For orientation purposes, such as when applying an underline border style, the bottom of a quadrilateral is the line formed by (x1, y1) and (x2, y2).

If this entry is not present or the conforming reader does not recognize it, the region specified by the Rect entry should be used. QuadPoints shall be ignored if any coordinate in the array lies outside the region specified by Rect.

Note that I defined the linkLocation parameter in such a way that the irregular shape fits inside that rectangle.

Caveat: you can try this functionality by testing this example: link_polygon.pdf, but be aware that while this will work when viewing the file in Adobe Reader, this may not work with inferior PDF viewers that did not implement the QuadPoints functionality.

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