How do I flatten PDF annotations?

Q:

We need a server based JAVA tool/SDK that can flatten Cross Out Tool
Annotations as well as Pencil Tool Annotations (terms used in
Acrobat). Can we use PDFNet SDK to flatten both of these Annotation
types?
---------------

A: You can use PDFNet SDK to flatten Cross-Out, Pencil, as well as any
other annotation type.

As a starting point you may want to download the SDK for your platform
(http://www.pdftron.com/pdfnet/downloads.html) and take a look at
Annotation sample project: http://www.pdftron.com/pdfnet/samplecode.html#Annotation

The code would look along the following lines (in Java, but other
languages are similar):

PDFDoc doc=new PDFDoc("my.pdf"));
doc.initSecurityHandler();
for (PageIterator pitr = doc.getPageIterator(); pitr.hasNext(); ) {
  Page page = (Page)(pitr.next());

// Flatten the annotations on the page.
// Note: The annotations must be flattened/erased in the reverse
order.
int num = page.getNumAnnots();
for (int i = num-1; i>=0; --i) {
    Annot ann = page.getAnnot(i);
    Annot.Type t = ann.getType();
    // if (t== Annot. e_StrikeOut || t== Annot.e_Polyline) {
      // Generate the appearance (if it is missing)?:
ann.refreshAppearance();
      ann.flatten(page);
   // }
}
doc.Save(...);
doc.Close();

Q: Thank you very much. This is the information that I was looking
for. Is the reason behind flattening the Annotations in reverse order
for a layering purpose. In other words, will this result in certain
Annotations being forced on top of other Annotations. Just trying to
understand the reason behind the reverse order traversal.

When you flatten an annotation you are effectively removing entries
from the array. The problem when flattening from the start of the
array is that it may get you out of the sync - because the index may
be mismatched. Of course you could also flatten annotations from the
start:

int num = page.getNumAnnots();
int i = 0;
while (i<num) {
    Annot ann = page.getAnnot(i);
    Annot.Type t = ann.getType();
    if (t== Annot. e_StrikeOut || t== Annot.e_Polyline) {
         // Generate the appearance (if it is missing)?:
ann.refreshAppearance();
       ann.flatten(page);
       num = num - 1;
    }
    else {
      i = i + 1;
    }
}

This is probably more correct in terms of layering (if the annotations
overlap) however the code is bit harder to understand.
A: The main reason is that in PDF annotations are stored in an array.

On Mar 14, 3:41 pm, Support <supp...@pdftron.com> wrote:

Q:

We need a server based JAVA tool/SDK that can flatten Cross Out Tool
Annotations as well as Pencil Tool Annotations (terms used in
Acrobat). Can we use PDFNet SDK to flatten both of these Annotation
types?

---------------

A: You can use PDFNet SDK to flatten Cross-Out, Pencil, as well as any
other annotation type.

As a starting point you may want to download the SDK for your platform
(http://www.pdftron.com/pdfnet/downloads.html) and take a look at
Annotation sample project: http://www.pdftron.com/pdfnet/samplecode.html#Annotation

The code would look along the following lines (in Java, but other
languages are similar):

PDFDoc doc=new PDFDoc("my.pdf"));
doc.initSecurityHandler();
for (PageIterator pitr = doc.getPageIterator(); pitr.hasNext(); ) {
Page page = (Page)(pitr.next());

// Flatten the annotations on the page.
// Note: The annotations must be flattened/erased in the reverse
order.
int num = page.getNumAnnots();
for (int i = num-1; i>=0; --i) {
Annot ann = page.getAnnot(i);
Annot.Type t = ann.getType();
// if (t== Annot. e_StrikeOut || t== Annot.e_Polyline) {
// Generate the appearance (if it is missing)?:
ann.refreshAppearance();
ann.flatten(page);
// }}

doc.Save(...);
doc.Close();