gettalong / hexapdf

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

Annotations disappear when you rotate a PDF file with annotations #188

Closed waterfallmouse closed 1 year ago

waterfallmouse commented 2 years ago

After rotating a PDF with annotations, the annotations are not highlighted when viewing the PDF.

The cause is that annotations cannot be taken into account during rotation. https://github.com/gettalong/hexapdf/blob/b9e194418b3b1bf89d6842e264dbff1c348c1332/lib/hexapdf/type/page.rb#L287

Wouldn't it be better to put [:Annots] in this array? For :Annots, I think it's necessary to specify

box = [:Annots][index][:Rect]

, so I know it's not easy.

gettalong commented 2 years ago

Yes, rotating the annotations is not yet implemented. Annotations have several flags that need to be taken into account when rotating them or the page they are on. It also depends on how you rotate the page.

Could you provide a sample PDF and what you would expect when rotating a page?

waterfallmouse commented 2 years ago

@gettalong Thank you for your reply.

I cannot provide it because it is a PDF file that has an NDA with the customer.

What I want to do is rotate the position of the annotation as well. Please refer to the following countermeasures.

    pdf_page.rotate(0, flatten: true)
    if (annots = pdf_page[:Annots])
      annots.each do |annot|
        box = annot[:Rect]
        matrix, llx, lly, urx, ury = \
        case angle
        when 90
          [HexaPDF::Content::TransformationMatrix.new(0, -1, 1, 0), box.right, box.bottom, box.left, box.top]
        when 180
          [HexaPDF::Content::TransformationMatrix.new(-1, 0, 0, -1), box.right, box.top, box.left, box.bottom]
        when 270
          [HexaPDF::Content::TransformationMatrix.new(0, 1, -1, 0), box.left, box.top, box.right, box.bottom]
        end
      end
      box.value = matrix.evaluate(llx, lly).concat(matrix.evaluate(urx, ury))
    end
gettalong commented 2 years ago

@waterfallmouse That might work in some cases but not all. As I said, rotation of annotations also depends on certain annotation flags.

gettalong commented 1 year ago

@waterfallmouse Could you create a sample PDF that shows what is going wrong and share that?

gettalong commented 1 year ago

@waterfallmouse Rotating a page when using flattening now works for annotations, too. So with the next release of HexaPDF you won't need the work-around anymore. Also see https://github.com/gettalong/hexapdf/issues/215#issuecomment-1356826310

waterfallmouse commented 1 year ago

@gettalong Thank you for your response! When is the next release scheduled?

gettalong commented 1 year ago

Some time this week before the new year.