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,
Hi Sachin,
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();
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,
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”);
Hi Sachin,
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);
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”);