使用 PDFBox 检测 rotated/inverted 个图像

Detecting rotated/inverted images with PDFBox

我有一些 PDF,其中包含旋转和倒置的图像。图片样本:

页面对象本身没有旋转指令,但看起来他们可能在内容流中使用 cm 运算符来对呈现的 PDF 执行转换:

...snip...
q
  0.05 0 0 -0.05 0 768 cm
  q
    0 0 11880 15360 re
    W*
    n
    /GS0 gs
    1 J
    [ ] 0 d
    2 w
    0 0 0 RG
    /GS0 gs
    1 1 1 rg
    /GS0 gs
    1 1 1 rg
    /GS0 gs
    1 J
    [ ] 0 d
    2 w
    0 0 0 RG
    q
      11865 0 0 15360 0 -3 cm
      /Image1 Do
    Q
...snip...    

我走的路对吗?

我们已经在使用 PDFStreamEngine 来分析图像,所以我想也许使用当前的图形状态可以提供这些:

protected class DrawObjectCounter extends OperatorProcessor {
   @Override
   public void process(Operator operator, List<COSBase> operands) throws IOException {
       System.out.println(getGraphicsState().getCurrentTransformationMatrix());
       ...snip...
   }
...snip...   

输出总是:

[1.0,0.0,0.0,1.0,0.0,0.0]

我是否需要使用另一个 OperatorProcessor 跟踪 CM 运算符,或者我只是没有找对地方?

我在构建 PDFStreamEngine 时遇到问题。我需要注册运算符来执行矩阵修改:

public PdfImageStreamEngine() {
    // pdfbox operators
    addOperator(new Save());
    addOperator(new Concatenate());
    addOperator(new Restore());
    // custom operators
    addOperator(new DrawObjectCounter());
    addOperator(new BeginInlineImageCounter());
    addOperator(new LineToCounter());
}

一旦我注册了正确的运算符处理器,getCurrentTransformationMatrix 就有了正确的值。