Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

c# - ITextShape Clickable Polygon or path

Anyone out there able to create clickable annotation that is an irregular shape using iTextSharp.

I know I can create a rectangular one like this

  float x1 = 100, x2 = 200, y1 = 150, y2 = 200;

  iTextSharp.text.Rectangle r = new iTextSharp.text.Rectangle(x1, y1, x2, y2);
  PdfName pfn = new PdfName(lnk.LinkID.ToString());
  PdfAction ac = new PdfAction(lnk.linkUrl, false);
  PdfAnnotation anno = PdfAnnotation.CreateLink(stamper.Writer, r, pfn, ac);

  int page = 1;
  stamper.AddAnnotation(anno, page);

Any way to do that with say a graphics path, I have seen this answer Draw a GraphicsPath in a PDF, but that just draws the shape it is not clickable.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...