Home » PDFBox Get Location and Image Size

PDFBox Get Location and Image Size

by Online Tutorials Library

Get Location and Size of Image

In this Tutorial, we will learn how to get coordinates or location and size of images in PDF from all the pages. This can be performed by using PDFStreamEngine class. This class handles and executes the operations in processing a PDF document by providing a callback interface.

To get the location and size of images in PDF document, we will extend the PDFStreamEngine class and intercept and implement processOperator() method.

For each object in the PDF document, we will check if the object is an image object and get its properties like (X, Y) coordinates and size. To do this, we can use the processOperator() method which is called in PDFStreamEngine.processPage(page) .

Follow the steps below to get coordinates or location and size of the images in the existing PDF document-

Extend PDFStreamEngine

In this, we have to first create a Java class and extend it with PDFStreamEngine. This can be shown in the below code.

Call processPage()

For each of the pages in PDF document, call the method processPage(). This method accepts the page name as a parameter. It can be shown in the following code.

Override processOperator()

For each object in the PDF page, processOperator is called in processPage() method. We can also override the processOperator() method.

Check for Image

Now, we can check if the object that has been sent to processOperator() method is an image object.

Print Locations and Size

Finally, if the given object is an image object then print the locations and size of the image.

Example-

Output:

PDFBox Get Location and Size of Image

You may also like