Drawing Polygons using the Datalogics PDF Java Toolkit

Drawing Polygons using the Datalogics PDF Java Toolkit

Sample of the Week:

If you are familiar with the Graphics2D class in Java, the code involved with drawing polygons on PDF pages as page content will seem very familiar to you. If you are trying to draw polygon annotations… not so much. The Gist referenced in this post demonstrates both ways of drawing polygons, in this case, a star, but it can be instructive in helping you draw any shape. The Datalogics PDF Java Toolkit makes drawing both types of objects pretty simple. There’s just a few things you need to know first.

What You Need to Know First:
Both Java’s Graphics2D class and PDF have marking operators that are equivalent to the commands to…

  1. Move to a set of coordinates; the origin point.
  2. Draw a line from the current point to another point.
  3. Close the shape by drawing a line from the current point to the origin point.

There are more commands but these three simple ones let you create a lot of interesting shapes. For drawing shapes as page content, there is a near 1 to 1 correspondence in the naming between the marking operators in Graphics2D and PDF… which you’ll see in the code below.

Not so with annotations. Polygon annotations in PDF also use a series of x, y coordinates as vertex pairs, but these are stored in a single, flat array. It is the annotation type that determines how the lines are connected – straight lines or curved (more on that in a future article).

The differences between how PDF page content gets drawn and how annotations get defined become very apparent when you try to use a Java Shape Object to draw onto a PDF page.

Drawing Polygons using the Datalogics PDF Java Toolkit

Drawing the PDF content is pretty easy. You just map the Graphics2D PathIterator Segment types to the equivalent PDF Instructions and that’s it.

To create the same shape as a polygon annotation, you need to create an array to hold the vertices. In the case of a star, the length of the array equals the number of arms times two, one for each inner vertex and the outer vertex of the star, plus one to return to the origin, then double that number since we need the x and y coordinates to be in the same array. So for a five pointed star we want an array that looks like this…

X1,Y1,X2,Y2,X3,Y3,X4,Y4,X5,Y5,X6,Y6,X7,Y7,X8,Y8,X9,Y9,X10,Y10,X1,Y1

So the code will look like this…

From here, you can add the annotation to the page and generate it’s appearance. Now here’s the funny part. Once you generate the appearance, the content of that appearance is identical (other than position) to the Instructions created for the page content… because that’s exactly what an appearance is… page content.

The fact that annotation appearances are, essentially, page content is how PDF annotations can at least be rendered properly by viewers that don’t natively support annotations; it’s one of my favorite things about PDF.

To get started working with PDF, download this Gist and request an evaluation copy of The Datalogics PDF Java Toolkit.

Leave a Reply

Your email address will not be published. Required fields are marked *