Free Support Forum - aspose.com

How to change Document to inputstream?

how to change com.aspose.words.Document to java.io.InputStream?

@mu_wang

It seems that you want to save your document to OutputStream. Please note that you can import document into Aspose.Words’ DOM from input stream and save the document to output stream. We suggest you please check the code example shared in the following links.
Load from a Stream
Save Document to OutputStream

If your requirement is different, please share some more detail about your query along with your use case. We will then answer your query accordingly.

String newPdfPath = res.get("newPdfPath").toString();
File file = new File(newPdfPath);
InputStream fis = new BufferedInputStream(new FileInputStream(file));
Document doc = new Document(fis);
doc.protect(ProtectionType.READ_ONLY, "****");

then how to change this “doc” to InputStream

@mu_wang

You can use following code example to convert the Document into input stream. Hope this helps you.

Document doc = new Document(MyDir + "template.docx");

// Create a new memory stream.
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// Save the document to stream.
doc.save(outStream, SaveFormat.DOCX);

// Convert the document to byte form.
byte[] docBytes = outStream.toByteArray();

// The bytes are now ready to be stored/transmitted.

// Now reverse the steps to load the bytes back into a document object.
//ByteArrayInputStream inStream = new ByteArrayInputStream(docBytes);
InputStream targetStream = new ByteArrayInputStream(docBytes);
// Load the stream into a new document object.
Document loadDoc = new Document(targetStream);