for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); ) {
Page page = (Page)(itr.next());
// need to draw from first to last, but flatten removes the annotation, so getNumAnnots drops.
// therefore don't increment index counter on flattening
for (int i = 0; i < pages.getNumAnnots();) {
try{
Annot annot = page.getAnnot(i);
if( /* your conditional here */ ){ // if you (don't) want to flatten fields, check for annotations of the Widget type.
annot.flatten(page);
} else {
++i;
}
} catch(Exception ex) {
// bad annotation. Remove since flattening failed.
page.annotRemove(i);
}
}
}