Words to Image?

Hello everyone,

I am new to this forum. Looking for sample code to convert a MS Words document into an image (PNG or JPG), and can't find one in the demo folder of Aspose.Words. Does anyone know where to find it?

Thanks.

Hi,


Thanks for your inquiry.


You can render a Microsoft Word document into an image (PNG or JPG) and Aspose.Words fully supports it and handles everything automatically so that you do not have to worry about internal details. You can convert a Microsoft Word document to an image on a very basic level as follows:


String csPath = "C:/Temp/"; Document document = new Document(csPath+"1.docx");

document.save(csPath + "jpgDoc.jpeg"); document.save(csPath + "pngDoc.png");

You can also have a look at how to save a multipage TIFF image here which could be extremely useful in case of documents with more then one pages.

Hope this helps. Please do let us know if you have any more queries.

Thank you very much for your quick response.

Just one more question. For security reason, we don't want to store the resulted image in the harddrive. Is it possible to generate the resulted image in memory instead of outputing it into a file?

Thanks!

Hi,


Thanks for your reply.


Sure, you can easily save rendered image to memory streams using Aspose.Words for Java. Following code snippet will surely help you:


// Create byte array stream to save resulting image. ByteArrayOutputStream dstStream = new ByteArrayOutputStream();

// Perform save operation, format should be specified in this case necessarily. document.save(dstStream, SaveFormat.JPEG);

// In case you want to read the result into a Document object again, // in Java you need to get the data bytes and wrap into an input stream. ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());


You can find further details here. Hope this helps. Please do let us know if you have any more queries.