Some of identical Annotations are not "valid"

Hi,

I would like to flatten some of our custom annotations, but somehow not all of the SAME annotations are not recognized/valid.

First, I merge the annotations (as XFDF) with a document, using this code (see here an example XFDF file):

`

public static InputStream mergeDocumentsAndAnnotations(Document document) {
FileInputStream pdf = FileSystem.getFileInputStream(document.getIdAlfrescoObject(), FolderEnum.DOCUMENTS);

PDFDoc in_doc = new PDFDoc(pdf);

in_doc.initSecurityHandler();
FDFDoc fdoc = FDFDoc.createFromXFDF(document.getXfdfData());
in_doc.fdfMerge(fdoc);
flattenAnnotations(in_doc.getPageIterator());

ByteArrayOutputStream merged = new ByteArrayOutputStream();
in_doc.save(merged, SDFDoc.e_linearized, null);
byte[] b = merged.toByteArray();
return new ByteArrayInputStream(b);
}

`

Within this method, I try to flatten the annotations onto the document:

`

public static void flattenAnnotations(PageIterator itr) {
while (itr.hasNext()) {
Page page = (Page) (itr.next());
int num_annots = page.getNumAnnots(); // RETURNS CORRECT AMOUNT OF ANNOTATIONS
play.Logger.info(String.valueOf(num_annots));
for (int i = 0; i < num_annots; i++) {
Annot annot = page.getAnnot(i);

if (annot.isValid()) { // SOME OF THE SAME ANNOTATIONS ARE NOT VALID
Markup markup = new Markup(annot);
annot.flatten(page);
}
}
}
}

`

The exampe XFDF file above contains 4 annotations, but not all annotations are “valid”, although identical. Only 2 are valid and are available for flattening. If I remove the isValid() check, I get NullpointerExceptions.

What defines if a annotation is valid or not? The annotations in the XFDF file are identical (except the creation date), why are some valid and some not?

I use libPDFNetC.so.6.7.1 and the Java library.

What am I missing here? Or is there a bug?

Hello,

I ran the isValid() check by creating a FDFDoc using the XFDF you provided on a blank document, but all of the annotations were valid.

Is it possible for you to send me the document where you extracted the annotation data from?

For reference, I have attached the code that I used:

`
PDFDoc doc=new PDFDoc();
Page pg = doc.pageCreate(new Rect(0, 0, 1000, 900));
doc.pagePushBack(pg);

FDFDoc xfdf = FDFDoc.createFromXFDF("./NyLr4DQ6.xml"); // the XFDF/XML you provided
doc.fdfMerge(xfdf);

PageIterator itr = doc.getPageIterator();
while (itr.hasNext()) {

Page page = (Page) (itr.next());
int num_annots = page.getNumAnnots();
for (int i = 0; i<num_annots; i++) {
Annot annot = page.getAnnot(i);
if (annot.isValid()) {
System.out.println(annot.toString());
}
}
}

`

  • Shakthi

Got it.

annot.flatten(page) removes the annotation from the list/makes it invalid, so every 2nd annotation gets invalid using my code.

Decrementing the index (i–) everytime an annotation gets flattened fixes the problem.