Generating barcode image as memorystream and insert stream to PDF

Dear All,
I have searched a lot but not able to get the best prectice around this and heading back to experts now.

1. I want to generate a barcode image that is in memory
2. Next, Insert the memory image to pdf docuemnt(Note that pdf document is also being created by aspose)

Something like this:

getBarCodeImg(String text){}

generatepdf( ){
final aspose.pdf.Image barCodeImage = new aspose.pdf.Image(section);

barCodeImage.getImageInfo().setImageStream(getBarCodeImg(“code-128”));
}

The baseline is that we don’t want to save barcode image to anyfile on the filesystem and at teh same time generate it, keep it in memory, insert memory image to pdf.

Platfrom:
Java1.5 and above,Windows 7


Please provide us the solution how should we achieve this also handling/closing the stream appropriately if applicable.

We have a valid Enterprise for Aspose.

Hi Sachinkumar,


Thanks for your interest in our products.

Please visit the following link for required information on How to Integrate Aspose.BarCode for .NET with Aspose.Pdf?

In the event of any further query, please feel free to contact. We are sorry for your inconvenience.

Hi Sachin,


First of all, please accept my apologies for not attending your request earlier.

I believe your inquiry is for the Java version of the products. I am representing Aspose.BarCode for Java, and I would like to share that BarCodeBuilder class provide the means to save the generated barcode symbol in OutputStream. You can use this stream to inject the image in Pdf files. Also, BarCodeBuilder class has exposed a method getBarCodeImage that returns the BufferedImage object, same can also be used in your scenario.

I am going to quote only Aspose.BarCode related source code for now. I will also check back with Aspose.Pdf team to provide a complete code snippet for your reference.

Java

BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText(“12345”);
bb.setSymbologyType(com.aspose.barcode.Symbology.Code128);

java.io.OutputStream stream = new java.io.FileOutputStream(“output.png”);
bb.save(stream, com.aspose.barcode.RenderFormat.PNG);

BufferedImage bufferedImage = bb.getBarCodeImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, “png”, baos);
baos.flush();
byte[] bytesOut = baos.toByteArray();
baos.close();

Thanks and Regards.

Hello, Thanks for your quick comments.

byte[] bytesOut = baos.toByteArray();

baos.close();
I have tried above method to get the byte[] and then tried to write those byte[] to pdf file using
BinaryInputStream but everytime i have recieved below error. Just wondering if this is possible?

java.lang.RuntimeException: offset is too larger
at
aspose.pdf.BinaryFileStream.read(SourceFile:424)
at
aspose.pdf.MemStream.(SourceFile:101)
at
aspose.pdf.BinaryFileStream.toMemStream(SourceFile:621)
at
aspose.pdf.figure.Abi.a(SourceFile:104)
at
aspose.pdf.figure.Aba.a(SourceFile:152)
at
aspose.pdf.figure.Aba.a(SourceFile:99)
at
aspose.pdf.xml.Abi.a(SourceFile:208)
at
aspose.pdf.xml.Abi.a(SourceFile:618)
at
aspose.pdf.xml.Abj.a(SourceFile:578)
at
aspose.pdf.xml.Acd.a(SourceFile:804)
at
aspose.pdf.xml.Aar.a(SourceFile:104)
at
aspose.pdf.xml.Aba.a(SourceFile:113)
at
aspose.pdf.Pdf.save(SourceFile:1172)
at aspose.pdf.Pdf.save(SourceFile:1118


For your information:
java.io.OutputStream stream = new java.io.FileOutputStream(“output.png”);
bb.save(stream, com.aspose.barcode.RenderFormat.PNG);

Saving the barcode image first to output stream and then setting it to PDF using input stream, is not an option for us since we don’t want to create any temporary file.

However, The writing byte[] of image file to pdf file will work for us since everything is in memory.

Please suggest how we can write this image byte[] to pdf file ?

Hello,
Are you able to help on above?

Hi Sachin,


Please accept our apologies for the delayed response.

Below is the complete source code to generate barcode symbol and to insert it in a PDF file. Please use it on your end and feed us back with your results.

Please note, I have tested this code with Aspose.Pdf for Java v3.0.1 and Aspose.BarCode for Java v4.5.0. With latest version of Aspose.Pdf for Java v3.1.0, there is problem while adding images to PDF, and you may experience an exception with message “This picture is not supported!”. We have already logged it as PDFJAVA-33135. Hopefully a fix will be available soon. If you like, I can attach the ticket to this thread for tracking purposes.

Java

BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText(“12345”);
bb.setSymbologyType(com.aspose.barcode.Symbology.Code128);
BufferedImage bufferedImage = bb.getBarCodeImage();

Pdf pdf = new Pdf();
Section sec = pdf.getSections().add();
Image img = new Image();
img.getImageInfo().setSystemImage(bufferedImage);
sec.getParagraphs().add(img);
pdf.save(“F:/outputPDF.pdf”);


Regards,

Hi Sachin,


Thanks for your patience.

Adding more to Babar’s comments, please note that the This picture is not supported! error occurs when ImageFileType of the image being placed inside the PDF is not specified. Please note that if we specify the image file type, the Barcode image is properly displayed. However we already have logged the problem PDFJAVA-33135 in our issue tracking system so that we do not have to specify the image file type because we might not be certain about the Image type being added. Once this problem gets resolved, we would be more than happy to update you with the status of correction

I have tested the following code with Aspose.Pdf for Java 3.1.0 and Aspose.Barcode for Java 4.5.0 and the Barcode image is properly being displayed. For your reference, I have also attached the resultant Pdf document that I have generated. In case you still face the same problem or you have any further query, please feel free to contact. We apologize for your inconvenience.

[Java]
// create Barcode Builder object
com.aspose.barcode.BarCodeBuilder bb = new com.aspose.barcode.BarCodeBuilder();
// sample text for barcode
bb.setCodeText(“12345”);
// set the symbology for barcode image
bb.setSymbologyType(com.aspose.barcode.Symbology.Code128);

// create a new PDF instance
Pdf pdf = new Pdf();
// add section to pdf document
Section sec = pdf.getSections().add();
// create an image instance
Image img = new Image();
// set the Image Stream information
img.getImageInfo().setSystemImage(bb.getBarCodeImage());
// specify the image file type
img.getImageInfo().setImageFileType(ImageFileType.Png);
// add the image to Paragraphs collection of Section
sec.getParagraphs().add(img);
pdf.save(“d:/pdftest/outputPDF.pdf”);