Convert PDF to QR Code using Java

This short tutorial guides on how to convert PDF to QR code using Java. It has the details to set the IDE for using Aspose.PDF and Aspose.BarCode, a list of steps, and a sample code for converting PDF to QR code using Java. It will share details to read the QR and barcode from a PDF and save each of them as an image if required.

Steps to Change PDF to QR Code using Java

  1. Set up the IDE to use Aspose.PDF and Aspose.BarCode for Java to read QR codes from a PDF
  2. Load the source PDF file into the Document class object for converting PDF to QR code
  3. Iterate through each page and access the collection of image resources on it
  4. Parse through each image on the page and save it as an image in the memory stream
  5. Initialize the barcode reader for each image for reading all supported types of QR and barcodes
  6. Display the barcode or QR code text and type on the screen

These steps describe how to transform a PDF to QR code using Java. Load the input PDF file, iterate through all the pages, access the collection of images from the resources on each page, and use each image to initiate the barcode reader object. It returns the collection of barcodes and QR codes accessed from the image with its text, type and other parameters for display and further processing.

Code to Create QR Code from PDF using Java

// Necessary import statements
import com.aspose.pdf.*;
import com.aspose.barcode.barcoderecognition.*;
import com.aspose.barcode.License;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
// Custom namespace for the application
public class Main {
// Application's entry method
public static void main(String[] args) throws Exception {// For PDF to QR
// Set up licenses for Aspose.PDF and Aspose.BarCode
com.aspose.pdf.License pdfLicense = new com.aspose.pdf.License();// PDF License
pdfLicense.setLicense("License.lic");// Remove watermark from output
License barcodeLicense = new License();
barcodeLicense.setLicense("License.lic");
Document pdfDocument = new Document("bar_qr_code.pdf");
for (int pageIndex = 1; pageIndex <= pdfDocument.getPages().size(); pageIndex++) {
Page page = pdfDocument.getPages().get_Item(pageIndex);
// Check if the page contains images
if (page.getResources().getImages().size() > 0) {
// Process each image in the page
for (XImage image : page.getResources().getImages()) {
ByteArrayOutputStream imgStream = new ByteArrayOutputStream();
// Save the image to a memory stream in JPEG format
image.save(imgStream, ImageType.getJpeg());
byte[] imgBytes = imgStream.toByteArray();
// Initialize the barcode reader for the image
BarCodeReader reader = new BarCodeReader(
new java.io.ByteArrayInputStream(imgBytes),
DecodeType.ALL_SUPPORTED_TYPES);
// Retrieve and display barcode results
for (BarCodeResult result : reader.readBarCodes()) {
String barcodeText = result.getCodeText();
String barcodeType = result.getCodeTypeName();
System.out.println("Detected " + barcodeType + " with content: " + barcodeText);
}
}
}
}
}
}

The above code demonstrates how to transform images in PDF to QR using Java. You may filter the PDF pages based on a bunch of properties to access the target pages only. You can save each image to disk in addition to using it in the barcode reader. To achieve this, you can write the image data to a file on disk after saving it to the ByteArrayOutputStream.

This article has taught us how to read barcodes and QR codes from a PDF. To generate new QR codes, refer to the article on how to generate QR code using Java.

 English