Hello, im trying to save a document (com.aspose.words.Document) into a png file, but for example for a template with 4 pages, the generated png have 4 document's first page instead of 4 different files.
The code im using is:
final ByteArrayOutputStream out = new ByteArrayOutputStream();
List images = new ArrayList();
int bigImageWidth = 0;
int bigImageHeight = 0;
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
options.setPageCount(1);
ByteArrayOutputStream imgStream = new ByteArrayOutputStream();
for (int i = 0; i < doc.getPageCount(); i++){
options.setPageIndex(i);
doc.save(imgStream, options);
ByteArrayInputStream imgInputStream = new ByteArrayInputStream(imgStream.toByteArray());
BufferedImage read = ImageIO.read(imgInputStream);
images.add(read);
bigImageWidth = (bigImageWidth < read.getWidth()) ? read.getWidth() : bigImageWidth;
bigImageHeight += read.getHeight();
imgInputStream.close();
}
imgStream.close();
BufferedImage finalImage = new BufferedImage(bigImageWidth, bigImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = finalImage.createGraphics();
int x = 0;
int y = 0;
for (BufferedImage bufferedImage : images) {
g2d.drawImage(bufferedImage, null, x, y);
y += bufferedImage.getHeight();
}
ImageIO.write(finalImage, format, out);
am i missing something ? Could you please provide some help ?
Kind Regards,
Fábio Antunes
Hi Fábio,
Thanks for your inquiry. Please use the following Java code to save all pages in Word document to a single big PNG image file:
Document doc = new Document(getMyDir() + “input.docx”);
List<BufferedImage> images = new ArrayList<BufferedImage>();
int bigImageWidth = 0;
int bigImageHeight = 0;
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
options.setPageCount(1);
for (int i = 0; i < doc.getPageCount(); i++){
options.setPageIndex(i);
ByteArrayOutputStream imgStream <font color="BLUE">=</font> <font color="RED"><b>new</b></font> ByteArrayOutputStream<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
doc<font color="BLUE"><b>.</b></font>save<font color="BLUE"><b>(</b></font>imgStream<font color="BLUE"><b>,</b></font> options<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
ByteArrayInputStream imgInputStream <font color="BLUE">=</font> <font color="RED"><b>new</b></font> ByteArrayInputStream<font color="BLUE"><b>(</b></font>imgStream<font color="BLUE"><b>.</b></font>toByteArray<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
BufferedImage read <font color="BLUE">=</font> ImageIO<font color="BLUE"><b>.</b></font>read<font color="BLUE"><b>(</b></font>imgInputStream<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
images<font color="BLUE"><b>.</b></font>add<font color="BLUE"><b>(</b></font>read<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
bigImageWidth <font color="BLUE">=</font> <font color="BLUE"><b>(</b></font>bigImageWidth <font color="BLUE"><</font> read<font color="BLUE"><b>.</b></font>getWidth<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">?</font> read<font color="BLUE"><b>.</b></font>getWidth<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">:</font> bigImageWidth<font color="BLUE"><b>;</b></font>
bigImageHeight <font color="BLUE">+</font><font color="BLUE">=</font> read<font color="BLUE"><b>.</b></font>getHeight<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
imgInputStream<font color="BLUE"><b>.</b></font>close<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
imgStream<font color="BLUE"><b>.</b></font>close<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
}
BufferedImage finalImage = new BufferedImage(bigImageWidth, bigImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = finalImage.createGraphics();
int x = 0;
int y = 0;
for (BufferedImage bufferedImage : images) {
g2d.drawImage(bufferedImage, null, x, y);
y += bufferedImage.getHeight();
}
ImageIO.write(finalImage, “PNG”, new File(“D:\Temp\out.png”));
I hope, this helps.
Best regards,