Free Support Forum - aspose.com

Setting image using byte in pdf file

Hi,

I am trying to set image in PDF file. Here is my code

byte[] signatureData = userPhoto.getContents();
BinaryFileStream bufferIn = new BinaryFileStream ();
bufferIn.read(signatureData, 0, signatureData.length);

Image img1 =(Image)pdfSection.getObjectByID("signatureImage");
img1.getImageInfo().setImageStream(bufferIn);
bufferIn.close();

MemStream ms = new MemStream();
pdfDoc.save(ms);

XML Template

I am getting following exception when saving to memStream.
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134)

How can I set image using byte[].

Thanks

Mamatha

Hi,

Can you please provide update on this soon.

Thanks

Mamatha

Hi Mamatha,


I am working over this query and will get back to you soon. Sorry for the delay and inconvenience.

Hi Mamatha,


Thanks for your patience.

I have investigated this scenario and as per my understanding, the problem might be occurring because you have called bufferIn.close(); method before saving the PDF document. However you may consider using the following code snippet to load the XML file, read the Image file and place it inside PDF document.

[Java]

// instantiate Pdf object<o:p></o:p>

Pdf pdf1 = new Pdf();

// bind the source XML

pdf1.bindXML("d:/pdftest/Template_new (1).xml", null);

// get Section object already specified in XML file

Section sec1 = pdf1.getSections().getSection("Section1");

// get the Image Object as specified in XML file

aspose.pdf.Image image3 = (Image)sec1.getObjectByID("signatureImage");

// Read the source Image file

BinaryFileStream binarydata = new BinaryFileStream("d:/pdftest/bird_67.png");

// set contents of BinaryFileStream to Image object

image3.getImageInfo().setImageStream(binarydata);

// specify the type of image file

image3.getImageInfo().setImageFileType(ImageFileType.Png);

// save PDF document

pdf1.save("d:/pdftest/StreamImage-Test.pdf");

// close the Binary Stream

binarydata.close();


[XML]

<?xml version="1.0" encoding="utf-8" ?>

<Pdf xmlns="Aspose.Pdf">

<Section ID="Section1">

<Image ID="signatureImage" File=""></Image>

</Section>

</Pdf>

In case I have not properly understood your requirement or you have any further query, please feel free to contact. We are sorry for your inconvenience.

Hi,

I tried like this. But it did not work.

byte[] signatureData;
BinaryFileStream binaryFileStream = new BinaryFileStream (signatureData);

Image img1 =(Image)pdfSection.getObjectByID("signatureImage");

img1.getImageInfo().setImageStream(binaryFileStream);
img1.getImageInfo().setImageFileType(ImageFileType.Jpeg);

//writing pdf document to MemStream
MemStream ms = new MemStream();
pdfDoc.save(ms); //at this place system is hanging

Thanks

Mamatha

Hi Mamatha,


Thanks for your patience.

I have again tested the scenario where I have tried loading the Image file into BufferImage object, then created a Byte array, instantiated the BinaryFileStream object with Byte array contents and then I have passed these contents to setImageStream(…) and as per my observations, I am getting an Exception while saving the PDF document. For
the sake of correction, I have logged it in our issue tracking system as PDFJAVA-33153. We
will investigate this issue in details and will keep you updated on the status
of a correction. I have used the following code snippet to test the scenario.

We
apologize for your inconvenience.

[Java]

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#3F7F5F”>// load the source image file<span style=“font-size:10.0pt;font-family:“Courier New””><o:p></o:p>

BufferedImage img = ImageIO.read(new File("d:/pdftest/Question.png"));

ByteArrayOutputStream bas = new ByteArrayOutputStream();

ImageIO.write(img, "pnm", bas);

// instantiage byte array with image contents

byte[] data = bas.toByteArray();

// instantiate Pdf object

Pdf pdf1 = new Pdf();

// bind the source XML

pdf1.bindXML("d:/pdftest/Template_new (1).xml", null);

// get Section object already specified in XML file

Section sec1 = pdf1.getSections().getSection("Section1");

// get the Image Object as specified in XML file

aspose.pdf.Image image3 = (Image)sec1.getObjectByID("signatureImage");

// Read the source Image file

BinaryFileStream binarydata = new BinaryFileStream(data);

//binarydata.read(data, 0, data.length);

// set contents of BinaryFileStream to Image object

image3.getImageInfo().setImageStream(binarydata);

// specify the type of image file

image3.getImageInfo().setImageFileType(ImageFileType.Png);

// save PDF document

pdf1.save("d:/pdftest/StreamImage-Test.pdf");

// close the Binary Stream

binarydata.close();

The issues you have found earlier (filed as PDFJAVA-33153) have been fixed in Aspose.Pdf for Java 3.3.0.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.