Multistream save problem

Hello!
I am trying to save docx to html converted data to many streams:

InputStream is = new FileInputStream(filePath + "dok1.docx");
ByteArrayOutputStream os = new ByteArrayOutputStream();
Document docx = new Document(is);
File imagesDirr = new File(filePath, "Images");
if (imagesDirr.exists()) imagesDirr.delete();
imagesDirr.mkdir();
HtmlSaveOptions optionss = new HtmlSaveOptions(SaveFormat.HTML);
optionss.setExportTextInputFormFieldAsText(true);
optionss.setImagesFolder(imagesDirr.getPath());
optionss.setExportHeadersFootersMode(ExportHeadersFootersMode.FIRST_PAGE_HEADER_FOOTER_PER_SECTION);
optionss.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK);
docx.save(os, optionss);

When I run code I have this error message for (1) line:

Exception in thread “main” java.lang.IllegalStateException: Document part file cannot be written. When saving the document either output file name should be specified or custom streams should be provided via DocumentPartSavingCallback. Please see documentation for details.

I read that I should set properties to save via DocumentPartSavingCallback (as in error message) but I have no idea how to do this.

Please help.

Hi Radek,

Thanks for your inquiry. In your case, I suggest you please use HtmlSaveOptions.ExportImagesAsBase64 property as shown below instead of saving images on the disk. Hope this helps you. Please let us know if you have any more queries.

InputStream is = new FileInputStream(MyDir + "in.docx");
ByteArrayOutputStream os = new ByteArrayOutputStream();
Document docx = new Document(is);
HtmlSaveOptions optionss = new HtmlSaveOptions(SaveFormat.HTML);
optionss.setExportTextInputFormFieldAsText(true);
optionss.setExportImagesAsBase64(true);
// optionss.setImagesFolder(imagesDirr.getPath());
optionss.setExportHeadersFootersMode(ExportHeadersFootersMode.FIRST_PAGE_HEADER_FOOTER_PER_SECTION);
optionss.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK);
docx.save(os, optionss);

Thanks for responce.
I changed options but error still appears.

One more question:
I have to save docx to html in section parts. But I don’t know how i can set properties to do this. As I shown in code above I’m using HtmlSaveOptions.DocumentSplitCriteria. I tried to change name of part file or part stream but I don’t know how to create DocumentPartSavingArgs needs to
IDocumentPartSavingCallBack.documentPartSaving(com.aspose.words.DocumentPartSavingArgs).

Hi Radek,

Thanks for your inquiry. Please use following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream is = new FileInputStream(MyDir + "in.docx");
Document docx = new Document(is);
HtmlSaveOptions optionss = new HtmlSaveOptions(SaveFormat.HTML);
optionss.setExportTextInputFormFieldAsText(true);
optionss.setExportImagesAsBase64(true);
optionss.setExportHeadersFootersMode(ExportHeadersFootersMode.FIRST_PAGE_HEADER_FOOTER_PER_SECTION);
optionss.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK);
CallbackHandler callback = new CallbackHandler(os);
optionss.setDocumentPartSavingCallback(callback);
docx.save(os, optionss);
class CallbackHandler implements IDocumentPartSavingCallback
{
    public ArrayList stream;
    ByteArrayOutputStream os;
    CallbackHandler(ByteArrayOutputStream os)
    {
        stream = new ArrayList();
        this.os = os;
    }
    @Override
    public void documentPartSaving(DocumentPartSavingArgs arg)
            throws Exception {
        if(stream.size() == 0)
        {
            arg.setDocumentPartStream(os);
            stream.add(os);
        }
        else
        {
            ByteArrayOutputStream newStream = new ByteArrayOutputStream();
            arg.setDocumentPartStream(newStream);
            stream.add(newStream);
        }
    }
}

Thank you very much. I’m beginner in advanced programming, this is what I tried to write. Everything works.

Thanks.

Hi Radek,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.