diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ac5081e..a2c52575 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ * [HexaPDF::Type::AcroForm::SignatureField#field_value] to always return a correctly wrapped object * [HexaPDF::Writer] to remove /Type entry from trailer +* [HexaPDF::Type::AcroForm::AppearanceGenerator#create_text_appearances] to + handle invalid appearance streams that are not correct Form XObjects ## 0.46.0 - 2024-08-11 diff --git a/lib/hexapdf/type/acro_form/appearance_generator.rb b/lib/hexapdf/type/acro_form/appearance_generator.rb index 7b8615b7..6b0b1a84 100644 --- a/lib/hexapdf/type/acro_form/appearance_generator.rb +++ b/lib/hexapdf/type/acro_form/appearance_generator.rb @@ -227,8 +227,11 @@ def create_text_appearances form = (@widget[:AP] ||= {})[:N] ||= @document.add({Type: :XObject, Subtype: :Form}) # Wrap existing object in Form class in case the PDF writer didn't include the /Subtype - # key; we can do this since we know this has to be a Form object - form = @document.wrap(form, type: :XObject, subtype: :Form) unless form[:Subtype] == :Form + # key or the type of the object is wrong; we can do this since we know this has to be a + # Form object + unless form.type == :XObject && form[:Subtype] == :Form + form = @document.wrap(form, type: :XObject, subtype: :Form) + end form.value.replace({Type: :XObject, Subtype: :Form, BBox: [0, 0, width, height], Matrix: matrix, Resources: HexaPDF::Object.deep_copy(default_resources)}) form.contents = '' diff --git a/test/hexapdf/type/acro_form/test_appearance_generator.rb b/test/hexapdf/type/acro_form/test_appearance_generator.rb index 69be9605..430c4f7c 100644 --- a/test/hexapdf/type/acro_form/test_appearance_generator.rb +++ b/test/hexapdf/type/acro_form/test_appearance_generator.rb @@ -452,6 +452,12 @@ def check_rotation(angle, width, height, matrix) assert_equal(form, @widget[:AP][:N]) refute(form.key?(:key)) assert_match(/test1/, form.contents) + + form.delete(:Type) + @widget[:AP][:N] = @doc.wrap(form, type: HexaPDF::Type::Annotation) + @field[:V] = 'test2' + @generator.create_appearances + assert_match(/test2/, form.contents) end describe "takes the rotation into account" do