gettalong / hexapdf

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

Text in form fields rotated after `acro_form.flatten` #219

Closed andi-dev closed 1 year ago

andi-dev commented 1 year ago

Hi Thomas,

I have a pdf with an acro form including a dropdown. When I flatten the form, the label text of the dropdown ends up rotated.

Screenshot of original pdf:

image

Screenshot of flattened pdf:

image

The original pdf has a rotation of 90 degrees, so I assume that something went wrong when setting the orientation for the form field.

What do you think?

ps: pdf will follow via e-Mail in a moment.

gettalong commented 1 year ago

I have investigated yesterday and today a bit the problems mentioned in #188 and #215, so, alas, I know exactly what is going on. And yes, it has to do with the page rotation.

When the page gets rotated, the generation of the appearances needs to swap width/height. Otherwise the information in the appearance stream and the widget annotation doesn't fit together.

I'm still exploring all the various permutations that are possible concerning rotate (widget) annotations but I think it is just the width/height swapping.

gettalong commented 1 year ago

After experimenting a bit more this is actually be two different things:

  1. If the page is rotated, the annotation flattening code has to take that rotation into account. It already does this but in a very simplified manner which doesn't handle all situations, just the most common ones.

  2. Appearance generation has to take rotation into account but only if that information is set on a field's widget, not when it is set on the page. Btw. both rotation entries specify multiples of 90 degrees but one is in degrees clockwise and the other in degree counterclockwise :man_shrugging:

I have now implemented the code for handling 2, next up is the code for correctly handling 1.

gettalong commented 1 year ago

@andi-dev I have now implemented the code for handling 1. When using your provided PDF file and running either doc.pages[0].flatten_annotations or doc.acro_form.flatten, the fields now appear correctly:

image