Home > Java > body text

Use PDFBox to determine the scaling of content in a PDF

PHPz
Release: 2024-02-14 14:54:08
forward
375 people have browsed it

在处理PDF文件时,确定内容的缩放比例是一项重要的任务。php小编小新将为您介绍一种使用PDFBox库来实现此目的的方法。PDFBox是一个开源的Java库,它提供了一套强大的API,可以用于处理PDF文件。通过使用PDFBox,我们可以轻松地获取PDF文件中的内容,并确定其适当的缩放比例,以便在不同的设备上正确显示。无论您是在开发一个PDF阅读器还是需要在您的应用程序中处理PDF文件,本文都将为您提供详细的指导。让我们开始吧!

问题内容

我有一个 pdf,其中包含扫描文档的图像。页面内容如下:

第一个内容流

0.36 0 0 0.36 0 0 cm
q
  2200 0 0 1700 0 0 cm
  /im1 do
q
Copy after login
Copy after login

页面似乎缩放至原始大小的 36%。此外,这是在没有保存/恢复的情况下完成的,因此它会影响页面中的后续流。

我需要使用带有新内容流的 pdfbox 将文本添加到页面。为了修复缩放,我计算了缩放的倒数并将其添加到我的文本之前:

第二个内容流

q
  2.77778 0 0 2.77778 0 0 cm
  bt
    0 tr
    /f1 37.75083 tf
    67 531 td
    /devicergb cs
    1 0 0 sc
    1 0 0 1 67 531 tm
    (\000u\000r\000j\000h\000u\000v\000r\000q) tj
  et
q
Copy after login

这工作正常,但我需要根据页面的当前状态而不是硬编码值以编程方式计算缩放。我试图使用 pdfbox 来查找当前的缩放比例,但它始终是 1.0:

@sneakythrows
    @test
    void debug() {
        var source = new classpathresource("pdf/scaling/test.pdf").getfile();
        @cleanup var pdf = pddocument.load(source);
        var page = pdf.getpage(0);
        assertequals(0.36, page.getmatrix().getscalex());
        assertequals(0.36, page.getmatrix().getscaley());
    }
Copy after login
assertionfailederror: 
expected :0.36
actual   :1.0
Copy after login
Copy after login

我假设需要渲染页面才能确定实际的缩放比例。使用pdfstreamengine处理页面,我仍然看到缩放为1:

@sneakythrows
    @test
    void scalingdetection() {
        var source = new classpathresource("pdf/scaling/test.pdf").getfile();
        @cleanup var pdf = pddocument.load(source);
        debugstreamengine engine = new debugstreamengine();
        engine.processpage(pdf.getpage(0));
        assertequals(0.36f, engine.getscalingx());
        assertequals(0.36f, engine.getscalingy());
    }
    

    protected class debugstreamengine extends pdfstreamengine {
        public debugstreamengine() {
            addoperator(new concatenate());
            addoperator(new drawobjectoperator());
            addoperator(new begininlineimageoperator());
            addoperator(new setgraphicsstateparameters());
            addoperator(new save());
            addoperator(new restore());
            addoperator(new setmatrix());
        }

        @override
        protected void processoperator(operator operator, list<cosbase> operands) throws ioexception {
            log.info("processing operator: {}", operator.getname());
        }
        
        protected float getscalingx() {
            return getgraphicsstate().getcurrenttransformationmatrix().getscalex();
        }
        
        protected float getscalingy() {
            return getgraphicsstate().getcurrenttransformationmatrix().getscaley();
        }
    }
Copy after login
assertionfailederror: 
expected :0.36
actual   :1.0
Copy after login
Copy after login

我认为这可能是由 pdfstreamengine.processstream 中的图形堆栈重置引起的。我已经通过维护自己的图形堆栈来解决这个问题,但我想知道我是否错过了更好的解决方案,或者这是否可能是 pdbox 中的错误?

解决方法

根据 mkl 和 kj 的评论,我不再需要确定/恢复缩放。这是通过在创建内容流时添加标志来重置上下文来完成的。这也有助于防止我没有考虑到的其他潜在问题。

添加此标志会在内容流前面添加一个 q 运算符。然后,新的(附加的)流由 q 运算符启动。编译结果为:

预置流

q
Copy after login

原始流

0.36 0 0 0.36 0 0 cm
q
  2200 0 0 1700 0 0 cm
  /im1 do
q
Copy after login
Copy after login

附加流

Q
q
BT
  0 Tr
  /F1 37.75083 Tf
  67 531 Td
  /DeviceRGB cs
  1 0 0 sc
  1 0 0 1 67 531 Tm
  (\000U\000R\000J\000H\000U\000V\000R\000Q) Tj
ET
Q
Copy after login

The above is the detailed content of Use PDFBox to determine the scaling of content in a PDF. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!