Bootstrap

Java使用 PDFBox 获取 PDF 中图像的坐标或位置和大小

在本教程中,我们将学习如何使用 PDFStreamEngine 从所有页面中获取 pdf 中图像的坐标或位置和大小。

org.apache.pdfbox.contentstream 类。PDFStreamEngine通过提供回调接口来处理和执行处理 PDF 文档的一些操作。

要获取pdf中图像的坐标或位置和大小,我们将扩展这个PDFStreamEngine类,拦截并实现 processOperator(Operator operator, List<COSBase>operands)方法。

COSBase是 PDF 文档中所有对象都将扩展的基类。

对于 PDF 文档中的每个对象,在 PDFStreamEngine.processPage(page) 中调用上述方法 processOperator()。对于 PDF 文档中的每个对象,我们将检查该对象是否为图像对象并获取其属性,例如 (X,Y) 坐标和大小。

在 PDF 中获取图像的坐标和大小的步骤

以下是获取 PDF 中图像的坐标或位置和大小的分步过程。

1.扩展PDFStreamEngine

创建一个 Java 类并使用 PDFStreamEngine 对其进行扩展。

public class GetImageLocationsAndSize extends PDFStreamEngine

2.调用processPage()

对于 PDF 文档中的每一页,调用方法 processPage(page)。

for( PDPage page : document.getPages() ) {
    pageNum++;
    printer.processPage(page);
}

3.重写processOperator()

对于 PDF 页面中的每个对象,processOperator 在 processPage() 中调用。我们将覆盖 processOperator()。

@Override
protected void processOperator( Operator operator, List operands) throws IOException{
    . . .
}

4.检查图像

检查已发送到 processOperator() 的对象是否是图像对象。

if( xobject instanceof PDImageXObject){
    . . .
}

5. 打印位置和尺寸

如果对象是图像对象,则打印图像的位置和大小。

示例 1 – 获取 PDF 中图像的位置和大小

在此示例中,我们将获取包含图像的 PDF,并获取图像的位置/位置和大小。

GetImageLocationsAndSize.java

import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.util.Matrix;
import org.apache.pdfbox.contentstream.operator.DrawObject;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.contentstream.PDFStreamEngine;
  
import java.io.File;
import java.io.IOException;
import java.util.List;
  
import org.apache.pdfbox.contentstream.operator.state.Concatenate;
import org.apache.pdfbox.contentstream.operator.state.Restore;
import org.apache.pdfbox.contentstream.operator.state.Save;
import org.apache.pdfbox.contentstream.operator.state.SetGraphicsStateParameters;
import org.apache.pdfbox.contentstream.operator.state.SetMatrix;
  
/**
* This is an example on how to get the x/y coordinates of image location and size of image.
*/
public class GetImageLocationsAndSize extends PDFStreamEngine
{
    /**
     * @throws IOException If there is an error loading text stripper properties.
     */
    public GetImageLocationsAndSize() throws IOException
    {
        // preparing PDFStreamEngine
        addOperator(new Concatenate());
        addOperator(new DrawObject());
        addOperator(new SetGraphicsStateParameters());
        addOperator(new Save());
        addOperator(new Restore());
        addOperator(new SetMatrix());
    }
  
    /**
     * @throws IOException If there is an error parsing the document.
     */
    public static void main( String[] args ) throws IOException
    {
        PDDocument document = null;
        String fileName = "apache.pdf";
        try
        {
            document = PDDocument.load( new File(fileName) );
            GetImageLocationsAndSize printer = new GetImageLocationsAndSize();
            int pageNum = 0;
            for( PDPage page : document.getPages() )
            {
                pageNum++;
                System.out.println( "\n\nProcessing page: " + pageNum +"\n---------------------------------");
                printer.processPage(page);
            }
        }
        finally
        {
            if( document != null )
            {
                document.close();
            }
        }
    }
  
    /**
     * @param operator The operation to perform.
     * @param operands The list of arguments.
     *
     * @throws IOException If there is an error processing the operation.
     */
    @Override
    protected void processOperator( Operator operator, List<COSBase> operands) throws IOException
    {
        String operation = operator.getName();
        if( "Do".equals(operation) )
        {
            COSName objectName = (COSName) operands.get( 0 );
            // get the PDF object
            PDXObject xobject = getResources().getXObject( objectName );
            // check if the object is an image object
            if( xobject instanceof PDImageXObject)
            {
                PDImageXObject image = (PDImageXObject)xobject;
                int imageWidth = image.getWidth();
                int imageHeight = image.getHeight();
                 
                System.out.println("\nImage [" + objectName.getName() + "]");
  
                Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
                float imageXScale = ctmNew.getScalingFactorX();
                float imageYScale = ctmNew.getScalingFactorY();
                 
                // position of image in the pdf in terms of user space units
                System.out.println("position in PDF = " + ctmNew.getTranslateX() + ", " + ctmNew.getTranslateY() + " in user space units");
                // raw size in pixels
                System.out.println("raw image size  = " + imageWidth + ", " + imageHeight + " in pixels");
                // displayed size in user space units
                System.out.println("displayed size  = " + imageXScale + ", " + imageYScale + " in user space units");
            }
            else if(xobject instanceof PDFormXObject)
            {
                PDFormXObject form = (PDFormXObject)xobject;
                showForm(form);
            }
        }
        else
        {
            super.processOperator( operator, operands);
        }
    }
  
}

输出

Processing page: 1
---------------------------------
 
Image [X0]
position in PDF = 36.506977, 695.3907 in user space units
raw image size  = 429, 175 in pixels
displayed size  = 214.69952, 87.58139 in user space units
 
Image [X1]
position in PDF = 36.506977, 617.8186 in user space units
raw image size  = 300, 300 in pixels
displayed size  = 75.06976, 75.06976 in user space units
 
Image [X2]
position in PDF = 36.506977, 138.37305 in user space units
raw image size  = 600, 383 in pixels
displayed size  = 496.96182, 317.29486 in user space units
 
 
Processing page: 2
---------------------------------
 
Image [X0]
position in PDF = 36.506977, 495.70514 in user space units
raw image size  = 600, 383 in pixels
displayed size  = 496.96182, 317.29486 in user space units
 
Image [X1]
position in PDF = 245.20093, 307.53027 in user space units
raw image size  = 212, 146 in pixels
displayed size  = 106.0986, 73.0679 in user space units
 
 
Processing page: 3
---------------------------------
 
 
Processing page: 4
---------------------------------

如果您想使用相同的 PDF 文件,请在此处下载 pdf 文档apache.pdf [icon name=”file-pdf-o” class=”” unprefixed_class=””] 。否则,您可以在 Java 程序中为您的 PDF 文件路径分配文件名。

原始尺寸与显示尺寸

pdf 中显示的图像大小可能与原始(或原始)图像的实际大小不同。

(X,Y) PDF 中图像的位置

图像的左下角是我们从 PDFBox 工具获得的 (X,Y) 位置。

(X,Y) PDF 中图像的位置

结论

在这个Apache PDFBox 教程中,我们学习了获取 pdf 文档中图像的坐标或位置和大小,还了解了 x 和 y 坐标对于 pdf 中的图像的含义。

 

;