Aspose words output to input stream or integrate with docx4j

Hi,

I need to integrate aspose words with an existing process using docx4j in a web application in AWS in Java. I don’t want to have to save the aspose word document to a physical file.

The 2 ways I am looking to do this are either:

  1. Save docx to output stream and pipe to input stream

  2. Somehow extract what docx4j refers to as the “main processing part”

  3. The following code hangs on the doc save line:

    final PipedInputStream in = new PipedInputStream();
    final OutputStream out = new PipedOutputStream(in);
    System.out.println(“about to save”);

    doc.save(out, SaveFormat.DOCX);
    return in;

  4. The main processing part in docx4j is simply the document.xml file in the docx zip file I think but not clear how to access this in aspose.

The 2 are alternatives so only need to get one of these to work to be able to do the integration.

Thanks,

Chris

Ok appears I’ve got the first approach to work with a ByteArrayOutputStream (code below), still interested on your thoughts about this approach and point 2) in my post above

   final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    doc.save(outStream, SaveFormat.DOCX);
    final ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    System.out.println("saved");

    return inStream;

@chrisbeecham2,

Please check the following Java code:

ByteArrayOutputStream baos = new ByteArrayOutputStream();

Document doc = new Document("C:\\temp\\input.docx");
doc.save(baos, SaveFormat.DOCX);

ByteArrayInputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
// Rewind the stream position back to zero so it is ready for next reader
inputStream.reset();
// Now inputStream is ready
// For example, load it again in Aspose.Words object
Document doc2 = new Document(inputStream); 

P.S. Aspose.Words for Java cannot process individual files like Document.xml inside the DOCX package.