hexapdf icon indicating copy to clipboard operation
hexapdf copied to clipboard

Annotations disappear when you rotate a PDF file with annotations

Open waterfallmouse opened this issue 3 years ago • 3 comments

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.

waterfallmouse avatar Aug 15 '22 09:08 waterfallmouse

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?

gettalong avatar Aug 15 '22 09:08 gettalong

@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

waterfallmouse avatar Aug 15 '22 10:08 waterfallmouse

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

gettalong avatar Aug 15 '22 12:08 gettalong

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

gettalong avatar Nov 15 '22 21:11 gettalong

@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

gettalong avatar Dec 18 '22 15:12 gettalong

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

waterfallmouse avatar Dec 27 '22 02:12 waterfallmouse

Some time this week before the new year.

gettalong avatar Dec 27 '22 07:12 gettalong