个性化阅读
专注于IT技术分析

获取图像的位置和大小

本文概述

在本教程中, 我们将学习如何从所有页面获取PDF中图像的坐标或位置以及大小。这可以通过使用PDFStreamEngine类执行。此类通过提供回调接口来处理和执行处理PDF文档的操作。

为了获取PDF文档中图像的位置和大小, 我们将扩展PDFStreamEngine类并拦截并实现processOperator()方法。

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

请按照以下步骤获取现有PDF文档中图像的坐标或位置以及尺寸,

扩展PDFStreamEngine

在此, 我们必须首先创建一个Java类, 并使用PDFStreamEngine对其进行扩展。可以在下面的代码中显示。

publicclass GetImageLocationsAndSize extends PDFStreamEngine {

......

}

调用processPage()

对于PDF文档中的每个页面, 调用方法processPage()。此方法接受页面名称作为参数。可以在以下代码中显示。

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

覆盖processOperator()

对于PDF页面中的每个对象, 在processPage()方法中调用processOperator。我们还可以覆盖processOperator()方法。

@Override
protectedvoid processOperator( Operator operator, List<COSBase>operands)
 throws IOException { 

...........

}

检查图像

现在, 我们可以检查已发送到processOperator()方法的对象是否为图像对象。

if( xobjectinstanceof PDImageXObject)
	        {
	            PDImageXObject image = (PDImageXObject)xobject;
	intimageWidth = image.getWidth();
	intimageHeight = image.getHeight();                
	            System.out.println("\nImage [" + objectName.getName() + "]");
              }

打印位置和尺寸

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

// 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");

例-

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;

public class GetImageLocationsAndSize extends PDFStreamEngine {	

public void GetImageLocationsAndSize() throws IOException
    {
// preparing PDFStreamEngine
        addOperator(new Concatenate());
        addOperator(new DrawObject());
        addOperator(new SetGraphicsStateParameters());
        addOperator(new Save());
        addOperator(new Restore());
        addOperator(new SetMatrix());
    }	
		publicstaticvoid main(String[] args)throws IOException {
					
		PDDocument document = null;
	      String fileName = "/eclipse-workspace/Merge.pdf";
	try
	      {
	document = PDDocument.load( new File(fileName) );
	         GetImageLocationsAndSize printer = new GetImageLocationsAndSize();
	intpageNum = 0;
	for( PDPage page : document.getPages() )
	            {
	pageNum++;
	                System.out.println( "\n\nProcessing PDF page:
					" + pageNum +"\n---------------------------------");
	printer.processPage(page);
	            }
	        }
	finally
	        {
	if( document != null )
	            {
	document.close();
	            }
	        }
	    }
	
	protectedvoid 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( xobjectinstanceof PDImageXObject)
	            {
	                PDImageXObject image = (PDImageXObject)xobject;
	intimageWidth = image.getWidth();
	intimageHeight = image.getHeight();
	
	              System.out.println("\nImage [" + objectName.getName() + "]");
	
	       Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
	floatimageXScale = ctmNew.getScalingFactorX();
	floatimageYScale = 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");

	            }
	elseif(xobjectinstanceof PDFormXObject)
	            {
	                PDFormXObject form = (PDFormXObject)xobject;
	                showForm(form);
	            }
	        }
	else
	        {
	super.processOperator( operator, operands );
	        }
	 }
}

输出

PDFBox获取图像的位置和大小
赞(0)
未经允许不得转载:srcmini » 获取图像的位置和大小

评论 抢沙发

评论前必须登录!