本文概述
- 扩展PDFStreamEngine
- 调用processPage()
- 覆盖processOperator()
- 检查图像
- 打印位置和尺寸
为了获取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()方法。
@Overrideprotectedvoid 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 unitsSystem.out.println("Position in PDF = " + ctmNew.getTranslateX() + ", " + ctmNew.getTranslateY() + " in user space units");
// raw size in pixelsSystem.out.println("Raw image size= " + imageWidth + ", " + imageHeight + " in pixels");
// displayed size in user space unitsSystem.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 PDFStreamEngineaddOperator(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 objectPDXObject 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 unitsSystem.out.println("position in PDF = " + ctmNew.getTranslateX() + ", " + ctmNew.getTranslateY() + " in user space units");
// raw size in pixelsSystem.out.println("raw image size= " + imageWidth + ", " + imageHeight + " in pixels");
// displayed size in user space unitsSystem.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将图像插入PDF文档
- PDFBox环境安装详细步骤
- PDFBox功能
- PDFBox提取电话号码
- PDFBox提取图像
- 技术编辑教您win7放大镜技巧怎样打开
- 本文详细说明win7电脑字体怎样改
- 笔者教你压缩软件怎样给文件夹加密
- 详细教您win7如何整理磁盘碎片