We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Read Barcode from PDF Documents


Are the examples in the link bellow are still working?
How to Read Barcode from PDF Documents

I’m using aspose.pdf(17.8) barcode (17.7) and I cannot compile the code.

Thank you.


The code snippet shared in this link shows creating a section in the PDF file, adding a barcode image in it and then reading the same added barcode image. Following is a simple version of the code to read barcode from PDF.


com.aspose.pdf.License license = new com.aspose.pdf.License();
    //Instantiate PdfExtractor object
    com.aspose.pdf.facades.PdfExtractor objextractor = new com.aspose.pdf.facades.PdfExtractor();
    //Bind the input PDF document to extractor
    //Extract images from the input PDF document
    String strBarCodeImage = "\\Input\\69_pdf_";
    String suffix = ".jpg";
    int imageCount = 1;
        System.out.println("Extracting image " + imageCount);
        strBarCodeImage = "tmpbarcode" + imageCount + suffix;
        // Recognize barcode from image
        com.aspose.barcode.barcoderecognition.BarCodeReader reader = new com.aspose.barcode.barcoderecognition.BarCodeReader(strBarCodeImage);
        while (reader.read())
            System.out.println("codetext: " + reader.getCodeText());


Every time you call , objextractor.getNextImage(strBarCodeImage) … it is saving into disk. How to avoid that ?


Yes you can avoid it. Please consider the following lines of code:


  // save image to stream
  MemoryStream imageStream = new MemoryStream();
  imageStream.Position = 0;
  Aspose.BarCode.BarCodeRecognition.BarCodeReader barcodeReader = new Aspose.BarCode.BarCodeRecognition.BarCodeReader(imageStream);

I am looking for Java, I am not finding MemoryStream class in any jar.


Apologies for my mistake. The lines of code was shared mistakenly. Please refer to the Java code given below. It will serve the purpose.


java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
java.io.ByteArrayInputStream inputStream = new java.io.ByteArrayInputStream (outputStream.toByteArray());
com.aspose.barcode.barcoderecognition.BarCodeReader reader = new com.aspose.barcode.barcoderecognition.BarCodeReader(inputStream);

Its working , thank you so much for help.


Thank you for update. It is good to know that issue has been resolved and things are working.

Whenever we try to read a barcode with a code text value that starts with 0 using BarCodeReader we do not get back any result. Although if code text value starts with any other digit then it works fine.


Can you please share a couple of samples to reproduce the issue? This will help us ensure we are on the same page. Sorry for the inconvenience.

While creating the barcode we are using BarCodeBuilder from com.aspose.barcode package
and we are setting encodetype as com.aspose.barcode.EncodeTypes.CODE_128
builder.setCodeTextFont(new Font(“Courier”, Font.PLAIN, 7));

Everything works fine if I setCodeText as say 123456.
While reading the Barcode I am creating an instance of BarCodeReader with ByteArrayInputStream and barcodeRecognition DecodeType as Code_128.
The reader.read() method never returns any data if the code text value starts with 0.


Sorry, we were not able to reproduce this issue at our end using the latet version of Aspose.BarCode for Java. It always returns the correct code text even if we use 0 at the start. Following is the complete code we used.

//Instantiate BarCodeBuilder object
BarCodeBuilder bb = new BarCodeBuilder();

//Set the code text for the barcode

//Set the symbology type to Code128
bb.setCodeTextFont(new Font(“Courier”, Font.PLAIN, 7));
//Set the width of the bars to 0.5 millimeter
// bb.setxDimension(0.5f);

//Save the barcode image to file

//Initialize barcode reader
BarCodeReader rd = new BarCodeReader(“BarCodeText.png”);
// read barcode of type Code39Extended
// print the code text, if barcode found
System.out.println("CodeText: " + rd.getCodeText().toString());
// print the symbology type
System.out.println("Symbology type: " + rd.getCodeType());

Can you please share if you are using the latest version and if you have used a valid license?

Thanks muhammad.

Its working fine now.

Do i need licence for Aspose barcode or Aspose pdf to read barcodes from pdf files?


Thanks for your query.

Well, you need to use both Aspose.PDF and Aspose.BarCode to read barcodes from PDF documents, see the document for your reference:
Recognize Barcode from Pdf Document

Hope, this helps a bit.