How to selectively flatten certain annotations?

Question:

How do I manually, selectively flatten annotations? There are some I don’t want to flatten, so I can’t use PDFDoc.FlattenAnnotations.

Answer:

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);
        }
    }
}

What can I condition can I put in there? I wanted to check for the form field name.

To get the full form field name use Field.GetName()
https://www.pdftron.com/api/PDFTronSDK/dotnet/pdftron.PDF.Field.html#pdftron_PDF_Field_GetName

Full forms sample code here
https://www.pdftron.com/documentation/samples#interactiveforms