Getting the error "An attempt to create annotation using invalid dictionary."

I’m using the Aspose.PDF library to combine multiple pdfs into a single pdf. I have a pdf with annotations in it and whenever I go to add those pages to the new combined pdf I get the following error:

Exception: An attempt to create annotation using invalid dictionary.
Stack: System.ArgumentException: An attempt to create annotation using invalid dictionary.
at Aspose.Pdf.Annotations.Annotation.(??? , Page )
at Aspose.Pdf.Annotations.AnnotationCollection.…ctor(List`1 , Page )
at Aspose.Pdf.PageCollection.(Page )
at Aspose.Pdf.PageCollection.(Page , ??? )
at Aspose.Pdf.PageCollection.(ICollection , ??? )

Is there something I have to do before I can combine pdfs with annotations?

@domiecin

No, you need nothing to perform before concatenating PDF document which contain annotations. However, would you please share your sample PDF document(s) with us. We will further test the scenario in our environment and address it accordingly.

We are also experiencing the same issue when merging PDFs. Was there a resolution to this issue?

@user18

Could you please share the sample PDFs for our reference so that we can test the scenario in our environment and address it accordingly?

Unfortunately this is happening only in our prod environment and the documents have NPI data, so we are unable to get them to test with.

Another thing noted is that we are handling the exception to skip such PDF from merge but its not working as expected. Code sample is here (refer capital letters)

try (InputStream is = getFile(1stpdffilename)) {
com.aspose.pdf.Document pdfMergeDoc = new com.aspose.pdf.Document(is);

for (int index = 1; index < arrMergeDocNamesAndPath.size(); index++) {
	// Open the source document
	try (InputStream is1 = getFile(arrMergeDocNamesAndPath.get(index))) {
		com.aspose.pdf.Document pdfDocument2 = new com.aspose.pdf.Document(is1);

		// Add the pages of the source document to the target document //EXCEPTION IS THROWN HERE FIRST
		pdfMergeDoc.getPages().add(pdfDocument2.getPages());

	} catch (Exception e) {
		LOG.error("PDF merge error with File:" + arrMergeDocNamesAndPath.get(index), e);
		lstFailedToMerge.add(arrMergeDocNamesAndPath.get(index));
	}
}

// Save the concatenated output file (the target document) -  IT IS THROWING HERE AS WELL
pdfMergeDoc.save(strMergedPDFFileNameAndPath);

}

@user18

We are afraid that we cannot comment much about the issue without replicating it on our side. You can try opening and merging the files using file paths instead of Streams as there is quite a chance that incoming stream is corrupted. You can also try to save the source file from stream (before merging it) and open it in Adobe Reader to see whether it is a valid document or not. Also, please make sure to test the scenario with 21.7 version of the API. Please try to share sample files with us in case issue keeps happening so that we can address it accordingly.

@asad.ali

We were able to look at the prod PDFs and observed that all failed PDFs have a signature keys (yellow key icon and when we mouse over it pops up a yellow rectangle box with signature info) in them which we think must have caused the problem. Please advise if we can detect the signature keys in the PDF and skip them from the merge?

@user18

You can try flattening the annotations and form fields before merging:

Document doc = new Document(dataDir + "document.pdf");
doc.Flatten();
foreach (Page page in doc.Pages)
{
 foreach(Annotation annot in page.Annotations)
 {
  annot.Flatten();
 }
}

Please share sample PDF documents for our reference if you still notice any issue. We will further proceed to assist you accordingly.