Recently I have been working on our annotation code, adding greater support for ink annotations. So I perform the same steps I always do. This time I thought I would give you a quick run down of these steps and what I encountered as I continued.
Read the Specification.
First thing is to read the specification so I have an idea of what it is I have to achieve. In this case I find ink annotations specify an array of arrays specifying points that are used to form the complete line of the ink. These points either are connected by strait or curved lines in an implementation dependent way. If the annotations contains an appearance we use that in place of the array of coordinates. Chasing the implementation notes I find the following. “93. Acrobat viewers always use straight lines to connect the points along each path.” That is going to make life incredibly easy, or so I thought.
Check existing code.
I began to check our existing code. We already have support for appearance streams, we currently needed the coordinates to be drawn. This is beginning to look even easier. I just need to set a color, loop through the coordinates for the ink list within the ink annotation and make a call to a draw line method.
Compare output with adobe reader.
So now I open an example file in adobe reader to confirm what the final output should be. Wait. No. This is wrong. The spec explicitly states all adobe viewers use straight lines. Why are there curves here. Could the ink have an appearance applied to it? No. Four points defining the shape of a “Z”. Hmmm, other viewers display a Z shape built of straight lines, just as the spec states. Right, lets not get too distracted by this. Lets follow the spec and see where to go from there.
Implement changes, to match output as closely as possibly.
Already having access to the four points and drawing a straight line between each point in order, this has been the easiest part yet. Except, many users compare output. If I don’t want this to comeback and haunt me I need to match adobes viewer as closely as I can. But how to add the curve. After a lengthy investigation that I won’t bore you with I came to the conclusion that the curves were bézier curves with control points calculated based on the lines around it. With time and effort I managed to find the best values that can be used to calculate these points.
It turns out that adobe viewer doesn’t always follow it’s own specification. For instance when a file containing an ink annotation with no appearance the viewer builds a shape using curves starting and ending at the points specified in the ink list. It also turns out that should you save the file after modifications or you just use the save as option within adobe reader, the generated curved shape is saved as an appearance for the ink annotation, to be used in place of the points. I should point out that the saving of this shape as an appearance stream is something we do not support.
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.