Selective Flattening with the Datalogics PDF Java Toolkit

Selective Flattening with the Datalogics PDF Java Toolkit

Sample of the Week:

Because PDF is so important to regulated industries,  I try to keep up with what’s going on with the various PDF standards like PDF/A which is incredibly strict and easily supported by our products. It is their very strictness that makes it easy to support; the file either conforms or it doesn’t. However, I also have to stay on top of documents issued by governments that have headers that contain phrases like “Contains Nonbinding Recommendations.” Things can get a little squishy here. Yes, the recommendations in these documents are nonbinding and they are just recommendations but I find it’s best to help developers get as close to the recommendations as possible.

One trend I noticed in several of these documents is the recommendation to “not include PDF annotations in documents.” When there are PDF annotations that don’t have appearances included in them, they can display incorrectly in a lot of PDF viewers so I understand why there’s a desire to not use them. But sometimes PDF annotations are used to point out or highlight an area of the document and that area might be important. The context of the information on the page may not make any sense without the annotation or a mistake may be called out by a red circle around it. PDF form fields are also a type of annotation and they often contain important information. For this reason, it may not be a good idea to just delete the annotations. Instead, the annotations and form fields should be flattened; basically turned into page content and removing the ability to interact with them.

But links are annotations too. If we flatten all of the annotations, we lose the links… and these recommendations love the links. The links facilitate navigating long documents… and these documents can get pretty long.

Luckily, selectively flattening annotations with the Datalogics PDF Java Toolkit is simple but the way to do it may not be obvious.

The APContext class can be used to define which annotations will be processed by the FormFlattener, which, contrary to what it’s name implies, flattens annotations as well… as long as you tell it to… which is what this sample shows.

The interesting lines are below. We start by creating our APContext object and then, because we want to flatten everything except the links, we create a set of all of the annotation types. From there, we simply remove the link annotation type from the set. Then we are free to flatten the document. Only annotation types in the set get flattened.

The annotations get flattened, the links get preserved, and now the document is ready for submission to the regulating body.

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

Start Your FREE Trial 

Leave a Reply

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