Read Barcode from PDF in Java

PDF documents such as invoices, receipts, or reports may contain encoded information in the form of a barcode. We can detect, identify and read barcodes embedded into PDF documents programmatically. In this article, we will learn how to read a barcode from a PDF document using Java. Moreover, we will learn how to extract barcode images from PDF documents in Java.

This article shall cover the following topics:

  1. Java API to Read Barcode from PDF
  2. Read Barcode from PDF
  3. Convert PDF to Image and Read Barcode
  4. Extract and Read Barcode from PDF
  5. Read Barcode from PDF using PngDevice
  6. Find and Read Barcode Image from PDF

Java API to Read Barcode from PDF

To read a barcode from a PDF document, we will be following a two-step procedure. Firstly, we will use the Aspose.PDF for Java API to load a PDF document and render its pages to raster images. After that, we will read the barcode from the rendered images using the Aspose.BarCode for Java API.

Please either download the JAR of the APIs or add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-barcode</artifactId>
    <version>22.8</version>
</dependency>
    
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>22.8</version>
</dependency>

Read Barcode from PDF using Java

Aspose.PDF API provides the Document class that represents a PDF document. The convertToPNGMemoryStream() method of the API renders a PDF page into an image stream in the byte[] array. Aspose.BarCode API provides the BarCodeReader class that enables us to perform readBarCodes operations to detect a barcode. The BarCodeResult class stores detected barcode information such as barcode type, code text, region and other parameters.

We can read barcode images embedded on any of the pages of the PDF document by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, loop through all the pages and render into the stream.
  3. Then, create an instance of the BarCodeReader class with a stream object.
  4. After that, call the readBarCodes() method to get the BarCodeResult object.
  5. Finally, show the barcode information.

The following code example shows how to read a barcode from a PDF document using Java.

CodeText: Aspose.Barcode Pdf417 Example
Symbology type: Pdf417
-------------------------------
CodeText: Aspose.Barcode QR Example
Symbology type: QR
-------------------------------
CodeText: Aspose.Barcode DataMatrix Example
Symbology type: DataMatrix

Please download the input PDF document with Barcode used in this blog post.

Convert PDF to Image and Read Barcode using Java

We can also read a barcode from the PDF document by converting PDF pages into images. The PdfConverter class of the API allows converting each page of a PDF file into an image. After that, we will read the barcode information from the converted image.

We can read barcodes from converted PDF pages by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, create an instance of the PdfConverter class.
  3. Optionally, set rendering options using the setBarcodeOptimization().
  4. Then, set the image resolution.
  5. Next, specify a range of pages to render into images using the setStartPage() and setEndPage().
  6. Then, call the doConvert() method to render selected pages into images.
  7. Next, save the images in a loop.
  8. Then, create an instance of the BarCodeReader class with the saved image file path.
  9. After that, call the readBarCodes() method to get the BarCodeResult object.
  10. Finally, show the barcode information.

The following code example shows how to convert PDF pages into images and read barcodes using Java.

Extract and Read Barcode from PDF using Java

Similarly, we can also recognize barcode images embedded on PDF pages using the PdfExtractor class. It allows extracting images from PDF, and then we will read the barcode information from the extracted image.

We can read barcodes from the extracted images by following the steps given below:

  1. Firstly, create an instance of the PdfExtractor class.
  2. Next, bind the input PDF document using the bindPdf() method.
  3. Then, set the page range for image extraction.
  4. Next, call the extractImage() method to extract the images.
  5. Then, save the image in a loop.
  6. Next, create an instance of the BarCodeReader class with the image path.
  7. After that, call the readBarCodes() method to get the BarCodeResult object.
  8. Finally, show the barcode information.

The following code example shows how to extract and read a barcode image from a PDF document using Java.

Read Barcode from PDF using PngDevice in Java

We can also read barcodes by converting the pages of the PDF document into PNG images using the PngDevice class of the API. It provides the process(Page page, OutputStream output) method that converts the page into PNG and saves it in the output stream. The processToBufferedImage(Page page) method of this class converts the page into BufferedImage.

We can read barcodes from converted PDF pages as PNG images by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, create an instance of the PngDevice class.
  3. Then, loop through all the pages and call the processToBufferedImage() method to render the image.
  4. Next, create an instance of the BarCodeReader class with the BufferedImage object.
  5. After that, call the readBarCodes() method to get the BarCodeResult object.
  6. Finally, show the barcode information.

The following code example shows how to convert PDF pages and read barcodes using Java.

Find and Read Barcode Images from PDF using Java

We can find and extract barcode images from PDF documents using the ImagePlacementAbsorber class. It performs a search of image usage and provides access to search results via the ImagePlacements collection. This method allows recognizing barcodes with original resolution. The only drawback is that it may not recognize vector formats correctly.

We can find and read barcodes from the PDF documents by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, create an instance of the ImagePlacementAbsorber class.
  3. Then, call the visit() method for each page in a loop.
  4. Next, loop through all the images found in the ImagePlacements collection.
  5. Then, save the image to the memory stream.
  6. Next, create an instance of the BarCodeReader class with the stream object.
  7. After that, call the readBarCodes() method to get the BarCodeResult object.
  8. Finally, show the barcode information.

The following code example shows how to find and read barcode images from a PDF using Java.

Get a Free License

You can get a free temporary license to try the libraries without evaluation limitations.

Conclusion

In this article, we have learned

  • how to detect and read barcodes from PDF documents;
  • various methods to extract images from a PDF document;
  • to decode the embedded barcode in Java.

Besides reading a barcode from PDF in Java, you can learn more about the APIs using the documentation for Aspose.BarCode and the documentation for Aspose.PDF. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also