How to merge annotations (instead of flattening them) when importing a page overlay?


When testing the ElementBuilder.CreateForm(page) method, it appears
that it automatically flattens any formfields/annotations on the page
being used as an overlay. Is there a way to use the functionality
provided by this method, but have it retain formfields/annotations?
Alternatively, can they be excluded so that separate code can be
written to copy them over manually?

You can use the following pseudocode to merge page overlay annotations
with the target page instead of flattening all annotations when
creating the form XObject:

Page page_overlay = ....

// Copy annotations...
// Obj annots = page_overlay.GetAnnots().Clone();
// Erase the annotation entry so that imported page overlay does not
// flattened annotations.

// ...
// Create a form XObject from the input page
Element element = element_builder.CreateForm(page)
... write element to the targe_page...

... Merge overlay annotations with the target page
int num_annots = annots.Size();
for (int i=0; i<num_annots; ++i) {
  target_page.AnnotPushBack (new Annot(annots.GetAt(i)));

// If necessary, restore the annotations in the input page.
// page_overlay.GetSDFObj().Put("Annots", annots.Clone());