Hi,
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"));
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, its 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,
icolomby:
However if I use the com.aspose.pdf.Image class it worksJava
Document doc = new Document(); Page page = doc.getPages().add(); BufferedImage bufferedImage = getBufferedImage(); 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 */
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,
Hi 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,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().
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.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?
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.