How to Read Barcode from PDF Documents

Hello,

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.

@thenrion,

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.

CODE:

com.aspose.pdf.License license = new com.aspose.pdf.License();
    license.setLicense("\\lic\\Aspose.Pdf.lic");
    
    //Instantiate PdfExtractor object
    com.aspose.pdf.facades.PdfExtractor objextractor = new com.aspose.pdf.facades.PdfExtractor();
    //Bind the input PDF document to extractor
    objextractor.bindPdf("\\Input\\69.pdf");
    
    //Extract images from the input PDF document
    objextractor.extractImage();
    
    String strBarCodeImage = "\\Input\\69_pdf_";
    
    String suffix = ".jpg";
    int imageCount = 1;
    
    while(objextractor.hasNextImage()) 
    {
        System.out.println("Extracting image " + imageCount);
        strBarCodeImage = "tmpbarcode" + imageCount + suffix;
        objextractor.getNextImage(strBarCodeImage);
        
        // 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());
        }
        imageCount++;
        reader.close();            
    }

Hi,

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

@senthil125,

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

CODE:

  // save image to stream
  MemoryStream imageStream = new MemoryStream();
  pdfExtractor.GetNextImage(imageStream);
  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.

@senthil125,

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

CODE:

java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
objextractor.getNextImage(outputStream);
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.

@senthil125,

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.

@senthil125,

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.setCodeText(“0123456”);
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.

@senthil125,

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
bb.setCodeText(“0123456”);

//Set the symbology type to Code128
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
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
bb.save(“BarCodeText.png”);

//Initialize barcode reader
BarCodeReader rd = new BarCodeReader(“BarCodeText.png”);
// read barcode of type Code39Extended
while(rd.read())
{
// 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?

@iqbalsingh7,

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.