gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.21k stars 69 forks source link

`acro_form.flatten` stops flattening annotations once a page has no `:Annot`-key #211

Closed andi-dev closed 1 year ago

andi-dev commented 1 year ago

Hi again :)

I hope I am not getting it wrong, but when flattening the form hexapdf goes through the pdf page by page and has each page attempt to flatten the widget-annotations: https://github.com/gettalong/hexapdf/blob/master/lib/hexapdf/type/acro_form/form.rb#L397

the remaining annotations are being reassigned to the not_flattened variable.

However in this line, Page#flatten_annotations returns an empty array when the page has no key?(:Annots) https://github.com/gettalong/hexapdf/blob/master/lib/hexapdf/type/page.rb#L522

If I am not understanding it wrong, this means that once the iteration in Form#flatten hits a page without :Annots it returns an empty array, thus essentially stopping the process of flattening the widgets. In consequence the further implementation of Form#flatten will remove the entire form without having actually flattened all the widgets.

My test case is a pdf with 4 pages, where only the second page is containing any Annotations, namely for two SignatureFields. Running the current implementation of acro_form.flatten removes the signature fields entirely. When I change the https://github.com/gettalong/hexapdf/blob/master/lib/hexapdf/type/page.rb#L522 to from return [] unless key?(:Annots) to return annotations unless key?(:Annots) the appearance of the signature fields remains visible.

What do you think? Let me know if I can help you to clarify any further.

gettalong commented 1 year ago

Thanks for the detailed analysis and yes, you are completely right, the error lies on line 522 of the page.rb file. The method would need to return annotations.to_ary, though, not just annotations because that might be HexaPDF::PDFArray and not a simple Array.

gettalong commented 1 year ago

I have fixed the error and the change is live on the devel branch if you want to try it out.

andi-dev commented 1 year ago

Thank you for the quick fix! I can confirm that the issue is solved :)