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

Free Support Forum - aspose.com

Word to image conversion

I am using aspose-words-15.10.0-jdk16.jar with a temporary licence.

The resulted file of the conversion from word doc to a jpg file contains only the first page of the word document. I thought the whole document will be converted. could you please clarify.



Hi Talal,


Thanks for your inquiry. You need to set license before creating Document object. Also, please learn about the limitations of Aspose.Words when using in evaluation mode. Hope, this helps.

Best regards,

Thanks Awais for replying.


Actually I did get the 30-day temporary license.

I just need to know how to convert the whole document ( 5 pages) to one jpg file.

Could you provide me with the code please.

Thanks again.

Hi Talal,


Thanks for your inquiry. Please use the following Java code to save all pages in Word document to a single big JPEG 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.JPEG);
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, “JPG”, new File(“D:\Temp\out.jpg”));

Hope, this helps.

Best regards,

Thank you so much. It did help