We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create PDF from BufferedImage (DOM API)

Hi,


I’m trying to convert java.awt.BufferedImages to PDF using the newer DOM API.

I have working code using the legacy API as its Image object supports BufferedImages via the following code:

aspose.pdf.Image image1 = new aspose.pdf.Image();
image1.getImageInfo().setSystemImage(bufferedImage);

I haven’t seen a similar API method with the com.aspose.pdf.Image object. Is there an such an API that I have missed?

If there isn’t one, can one be added? Are there any issues if I keep using the legacy API?

Thanks, Ian.

Hi Ian,


Thanks for contacting support.

The required method is implemented as XImageCollection.add(BufferedImage image);

[Java]

java.awt.image.BufferedImage
originalImage = ImageIO.read(new File(“c:\image\anyImage.jpg”));<o:p></o:p>

Document pdfDocument1 = new Document();

Page page2 = pdfDocument1.getPages().add();

page2.getResources().getImages().add(originalImage);


You may also consider using any InputStream, not only FileInputStream to add image. So using java.io.ByteArrayInputStream class, it's object not need to store any files over system:


[Java]

com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document();

java.awt.image.BufferedImage originalImage = ImageIO.read(new File("c:\\image\\anyImage.jpg"));

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageIO.write( originalImage, "jpg", baos );

baos.flush();

Page page2 = pdfDocument1.getPages().get_Item(i+1);

page2.getResources().getImages().add(new ByteArrayInputStream(baos.toByteArray()));

Hi,


I tried both of your code examples and they both produced a PDF with a blank page, no image. I’m using the latest Aspose.PDF 10.0

However if I use the com.aspose.pdf.Image class it works

[Java]
Document doc = new Document();
Page page = doc.getPages().add();
BufferedImage bufferedImage = getBufferedImage(); //no access to stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bufferedImage, “jpg”, baos );
baos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
page.getParagraphs().add(image1);
image1.setImageStream(bais);

However, I’d prefer not to perform the extra I/O operation of writing out the BufferedImage to a stream.

Also when using the older Image object I am able to control the scaling with this code:
[Java]
image1.setImageScale(scaleFactor);

Is there a way to do that using the XImageCollection?

Thanks, Ian.

icolomby:
However if I use the com.aspose.pdf.Image class it works

[Java]
Document doc = new Document();
Page page = doc.getPages().add();
BufferedImage bufferedImage = getBufferedImage(); //no access to stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bufferedImage, “jpg”, baos );
baos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
page.getParagraphs().add(image1);
image1.setImageStream(bais);
Hi Ian,

Thanks for sharing the details.

Yes you are correct. When using com.aspose.pdf.Image object, the contents of BufferedImage can be loaded and image can be placed inside paragraphs collection of PDF file.

[Java]

// instantiate Document instance<o:p></o:p>

com.aspose.pdf.Document doc = new com.aspose.pdf.Document();

// add a page to pages collection of Pdf file

com.aspose.pdf.Page page = doc.getPages().add();

// create image instance

com.aspose.pdf.Image image1 = new com.aspose.pdf.Image();

// create BufferedImage instance

java.awt.image.BufferedImage bufferedImage = ImageIO.read(new File("c:\\pdftest\\source.gif"));

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// write buffered Image to OutputStream instance

ImageIO.write( bufferedImage, "gif", baos );

baos.flush();

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

// add image to paragraphs collection of first page

page.getParagraphs().add(image1);

// set image stream as OutputStream holding Buffered image

image1.setImageStream(bais);

// save resultant PDF file

doc.save(“c:/pdftest/BufferedImage.pdf”);


icolomby:
However, I’d prefer not to perform the extra I/O operation of writing out the BufferedImage to a stream.

Also when using the older Image object I am able to control the scaling with this code:
[Java]
image1.setImageScale(scaleFactor);

Is there a way to do that using the XImageCollection?
Please try using com.aspose.pdf.Image object and try using setImageScale(2); method to fulfill this requirement.

Hi,


Is there anyway to use the a BufferedImage without saving it to a stream? This seems very inefficient. If I’m working with multiple images having them all in byte arrays in memory can lead to out of memory issues as well as unnecessary I/O.

I use the BufferedImage to get the images width and height to determine the scale factor (keeping the original aspect ratio) of the image (i.e. whether it should be shrunk down to fit on the page) and also to determine if the I should change the page to landscape to have the image fit better.

Do you know why the code you provided using the XImageCollection created a PDF with blank pages. If this solution worked, I could scale before adding it to the collection, this would achieve the same results as calling image.setImageScale().

I can do all of this with the older API (aspose.pdf package), but not with newer API (com.aspose.pdf package). Are there plans of deprecating or stopping support of the older API, if not can I keep using it. Will there be parity between these 2 APIs?


Thanks, Ian.

icolomby:
Is there anyway to use the a BufferedImage without saving it to a stream? This seems very inefficient. If I'm working with multiple images having them all in byte arrays in memory can lead to out of memory issues as well as unnecessary I/O.
Hi Ian,

Thanks for sharing the details.

I have tested the scenario and have managed to reproduce the same problem that when trying to load BufferedImage contents and placing them inside PDF file, the resultant file being generated is 0KB. For the sake of correction, I have logged it as PDFNEWJAVA-33498 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time.

icolomby:
Do you know why the code you provided using the XImageCollection created a PDF with blank pages. If this solution worked, I could scale before adding it to the collection, this would achieve the same results as calling image.setImageScale().
Hi Ian,

Once the issue related to BufferedImage is resoled, you will be able to scale the image using same object.

icolomby:
I can do all of this with the older API (aspose.pdf package), but not with newer API (com.aspose.pdf package). Are there plans of deprecating or stopping support of the older API, if not can I keep using it. Will there be parity between these 2 APIs?
Currently I cannot share the exact timelines by which legacy aspose.pdf package will be removed from our API but once all the features of legacy aspose.pdf are ported to new DOM object model of com.aspose.pdf package, it will be removed. Therefore its recommended to try migrating to latest release of Aspose.Pdf for Java.

The issues you have found earlier (filed as PDFNEWJAVA-33498) have been fixed in Aspose.Pdf for Java 10.8.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.