How to add barcode at a specific location in Java

Hi,

I am new to Aspose, I need help on the following in Java.

1. We are able to create a barcode (code 39) in PNG format and add it on an existing PDF, but the barcode is always at the bottom left corner, can you please advise how to add it on the upper corner, and is there any tool to help us find out the position. We downloaded the demo and uses

mendor.addImage(inImgStream, pages, 50, 50, 100, 100); for add.

2. We created the barcode in PNG format and added on a PDF, please advise the steps to read the barcode (how to read barcode from PDF).

Thanks.

Hi,

Thank you for your post!

You can use Aspose.BarCode to generate barcodes images and recognize barcodes from images. To read barcodes from PDF, you need to extract images from the PDF documents and feed them to BarCodeReader class for processing. I had asked the staffs from pdf team to look in to this post and they will provide details soon.

Thanks again.

Hi Dean,

I only have the evaluation copy of the software and we just realized that the evaluation copy doesnt recognize vertical barcode. Now we are able to add the barcode at a desired location. But we still cant read barcode from a PDF file (reading from PNG is successful).

Can you please provide some sample code on how to read the barcode from PDF? I am not sure if we need to exact barcode image (PNG format) from PDF first and then read it.

We also have scanned PDF and faxed TIFF files, does Aspose read barcode from TIFF files? How do we read barcode from a scanned PDF (in this case the barcode will be part of the PDF file).

Thanks.

Hi GDS,

The BarCodeReader is able to deal with images in jpg/png/gif/tiff formats, you can use PdfExtractor to extract the images from pdf.

Reference:

Aspose.Total for Java|Documentation

Hi,

As shared by Dean, Aspose.BarCode for Java have support for image formats like jpg, gif and png. To read barcodes from tiff and pdf files, you need different methods, which are listed below:

Tiff files:
JAI is required to read barcodes from tiff files. Please refer to http://www.aspose.com/documentation/java-components/aspose.barcode-for-java/how-to-read-barcode-from-multipage-tiff-images.html for sample code.

Pdf files:
To read barcode from pdf files, you need a component that can extract images from pdf file. These images can then be passed to Aspose.BarCode for Java for barcode recognition. Sample code snippet is given below:

try
{
// set license
License licPdfKit = new License();
FileInputStream stream = new FileInputStream(“e:\AsposeLicense.lic”);
licPdfKit.setLicense(stream);

com.aspose.barcode.License licBarCode = new com.aspose.barcode.License();
licBarCode.setLicense(“e:\AsposeLicense.lic”);

String strPdfDoc = “E:\Data\Aspose\temp\BarCode1.pdf”;
String strBarCodeImage = “”;

//Instantiate PdfExtractor object
PdfExtractor extractor = new PdfExtractor();

//Bind the input PDF document to extractor
extractor.bindPdf(strPdfDoc);

//Extract images from the input PDF document
extractor.extractImage();
String suffix = “.jpg”;
int imageCount = 1;
while (extractor.hasNextImage()) {
System.out.println("Extracting image " + imageCount);
strBarCodeImage = “E:\tmpbarcode” + imageCount + suffix;
extractor.getNextImage(strBarCodeImage);

// recognize barcode from image
BarCodeReader reader = new BarCodeReader(Toolkit.getDefaultToolkit().getImage(strBarCodeImage),BarCodeReadType.AllSupportedTypes);
while (reader.read())
{
System.out.println("codetext: " + reader.getCodeText());
}
imageCount++;
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}

Hi,

Attached is our Java code and testing PDF, we are not able to exact the image which means that the counter "imageCount" and "imageCount2" are always printed as "1". They should be printed as "2" is the image can be exacted correctly.

The PDF was created from Aspose, the barcode was created as PNG and added on the PDF.

Thanks.


Hi,

Thanks for posting the sample pdf file. I was also unsuccessful in extracting the images from it.

Could you please also post the code to insert these images in pdf file? I will also check it out with Aspose.Pdf.Kit team about image extraction issue.

Hi,

Below is the Java code

public void addBarCode(String pdfFile,String barCodeImage) throws Exception {<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

FileInputStream inPdfStream = new FileInputStream(pdfFile);

FileInputStream inImgStream = new FileInputStream(barCodeImage);

String outputFile = "c:\\output1.pdf";

FileOutputStream outputStream = new FileOutputStream(outputFile);

int[] pages = new int[] {1};

PdfFileMend mendor = new PdfFileMend(inPdfStream, outputStream);

//mendor.addImage(inImgStream, pages, 400, 190, 610, 1380);

mendor.addImage(inImgStream, pages, 1, 570, 60, 780);

mendor.close();

outputStream.close();

}

Hi Joanna,

I’m a support developer from Aspose.Pdf.Kit team. I would like to update you that I have tested the problem you’re having while extracting the images from the attached PDF; I’m also unable to extract the images.

There seems some problem with the image extraction feature of Aspose.Pdf.Kit for Java. I have logged this issue as
PDFKITJAVA-10835 in our issue tracking system. Our development team will be looking into the matter and you’ll be updated via this forum as the issue is resolved.

If you have any further questions or concerns regarding Aspose.Pdf.Kit, you can post your questions in the Aspose.Pdf.Kit Forum. We’ll be pleased to help you out.

We’re sorry for any inconvenience.
Regards,

GDS:


Hi Shahzad, I'd like to know how soon this will be resolved, is it in days or weeks? We already purchased Aspose and our development cycle will end 10/23, so please resolve this as soon as possible. Do you think this only happens to PDF? Should we use TIFF before the issue is fixed? Thanks. Joanna




Hi Joanna,

I have logged this issue today and our development team hasn't yet investigated the issue in detail. As the team will complete its investigation and share some time frame for this issue, I'll update you accordingly. I would suggest that you use TIFF in the meanwhile.

Regards,

Hi,

We are unable to extract barcode from the TIFF file (see attached). We used the sample code provided in below link (mentioned earlier in this thread). Can you please advise what needs to be added?

Tiff files:
JAI is required to read barcodes from tiff files. Please refer to http://www.aspose.com/documentation/java-components/aspose.barcode-for-java/how-to-read-barcode-from-multipage-tiff-images.html for sample code.

Hi,

You need to download JAI and JAI Image IO jar files and add these in your project for reading tiff images. You can download from:

Installation instruction are available at Java(TM) Advanced Imaging Image I/O Tools Installation.

If you have already setup JAI and JAI imageio, please post the tiff files, so that we may analyze the problem.

Tiff file is attached, it was also attached in my early reply.

Thanks.

Hi,

Can you please send the JAI jar names for the download?

Thanks.

Hi,

The download locations and jar names are:
jai_imageio.jar – can be downloaded from https://download.java.net/media/jai/builds/release/1_1_3/jai-1_1_3-lib-windows-i586-jdk.exe or https://download.java.net/media/jai/builds/release/1_1_3/jai-1_1_3-lib-windows-i586-jre.exe. The jar file will be copied to <JAVA_HOME>/jre/lib/ext folder.
jai_core.jar and jai_codec.jar – can be downloaded from https://download.java.net/media/jai-imageio/builds/release/1.1/jai_imageio-1_1-lib-windows-i586-jdk.exe , https://download.java.net/media/jai-imageio/builds/release/1.1/jai_imageio-1_1-lib-windows-i586-jre.exe. Release 1.1.3 is stable at this time of writing.

Hi,

We have below code for extracting barcode from TIFF(see attchment). Still not able to read. Please advise. Thanks.

System.out.println(System.getProperty("java.class.path"));<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Iterator readers = javax.imageio.ImageIO.getImageReadersBySuffix("tiff");

String fileName="c:/w8imytaxform-vertical.tif";

if (readers.hasNext()) {

File fi = new File(fileName);

ImageInputStream iis = javax.imageio.ImageIO.createImageInputStream(fi);

TIFFDecodeParam param = null;

ImageDecoder dec = ImageCodec.createImageDecoder("tiff", fi, param);

//Get the page count of the tiff image

int pageCount = dec.getNumPages();

ImageReader _imageReader = (ImageReader) (readers.next());

//System.out.println(_imageReader);

if (_imageReader != null) {

_imageReader.setInput(iis, true);

//Feed each page to the BarCodeReader

System.out.println(_imageReader);

for (int i = 0; i < pageCount; i++) {

BufferedImage _bufferedImage = _imageReader.read(i);

BarCodeReader reader = new BarCodeReader(_bufferedImage, BarCodeReadType.Code39Standard);

//Read the barcodes in a single page

while (reader.read()) {

System.out.println(reader.getCodeText());

}

}

}

}

My classpath has the below jars. Still not able to read.

C:\Working\workspace2\WorkSheet\lib\barcode\jai_codec.jar;

C:\Working\workspace2\WorkSheet\lib\barcode\jai_core.jar;

C:\Working\workspace2\WorkSheet\lib\barcode\mlibwrapper_jai.jar;

C:\Working\workspace2\WorkSheet\lib\barcode\jai_imageio.jar

Hi,

Could you please test the attached tif file with the above code and post the results, if there is problem reading the tiff file or recognizing the barcodes.

However, I tested the code with your sample tif file and there seems to be a problem with the recognition of barcode. I have sent the tif file to the development team for further analysis and will get back to you when its fixed.

Hi,

We are able to read your TIFF file. Can you please advise how you generate the TIFF file? We created the TIFF file from MS imaging writter. I will try to read a TIFF file via fax.

Thanks.

Hi,

I created a small program in .NET to create this multi-page tiff file. But, however, the barcode was generated by Aspose.BarCode itself, so there was no problem in recognition.

The barcode from your sample could not be read because it was scanned. However, it can read the barcodes from most of the scan documents. Could you please try to increase the quality/dpi for the document. It might work after that.

In the meanwhile, our development team is also working on the original tiff file sent by you.

Hi,

We don't have a problem reading the barcode image which is generated directly from Aspose.BarCode itself. The problem is when we add that barcode on a TIFF or PDF, the new image with the barcode can not be recognized.

Can you please send over a scanned barcode document which can be read by your program? A document which has barcode and some context, not just a barcode by itself.

Thanks.