Save document

This article describes how to obtain edited document content from client, process it and save to the resultant document of some specified format.

When end-user has finished document editing in the WYSIWYG HTML-editor (this is usually a pure client-side application, written on JavaScript), he submits the editing operation, and HTML markup with stylesheets, images, and maybe other resources are passed to the server-side. In order to generate a document of some output format, these resources should be passed to the EditableDocument.

As it was shown in previous articles, instances of EditableDocument are generated and returned by the Editor.edit() method and then are used for emitting HTML markup and resources for passing them to the WYSIWYG editor.
However EditableDocument also has a second purpose — to obtain edited content from WYSIWYG editor. EditableDocument class has no public constructors; instead of them it has two static factories, that obtain HTML document in different forms:

  1. FromFile method is designed for opening HTML-documents from disk — it obtains path to *.html file and path to corresponding resource folder.
  2. FromMarkup method is designed for opening HTML-documents from memory — it obtains HTML-markup as a String and a list of IHtmlResource items.

More information about creating EditableDocument instances from files or markup with resources can be found in corresponding article “Create EditableDocument from file or markup”.

When EditableDocument is created, it can be converted to the output document. For doing this, user must use method, that has two overloads. These overloads differ only with the way how output document is specified: as path, where file should be created, or as a byte stream, into which the document content should be written. All other parameters are same. They are:

  1. Instance of EditableDocument class, that holds a content of edited document.
  2. Output document, that is specified as file path (String) or byte stream (ByteArrayOutputStream).
  3. Mandatory save options, that are represented by one of inheritors of the ISaveOptions interface.

Like with load and edit options, every family format has its own class, that implements ISaveOptions interface. These classes are listed below.

Format familyExample formatsSave options classFormat class
WordProcessingDOC, DOCX, DOCM, DOT, ODTWordProcessingSaveOptionsWordProcessingFormats
SpreadsheetXLS, XLSX, XLSM, XLSBSpreadsheetSaveOptionsSpreadsheetFormat
Delimiter-Separated Values (DSV)CSV, TSVDelimitedTextSaveOptionsTextualFormats
PresentationPPT, PPTX, PPS, POTPresentationSaveOptionsPresentationFormats
Plain Text documentsTXTTextSaveOptionsTextualFormats

Source code below shows creating an instance of EditableDocument class and consequent saving a two versions of the document: one to the file and second — to the stream.

String inputHtmlPath = "C:\\input\\document.html";
EditableDocument document = EditableDocument.fromFile(inputHtmlPath, null);

Editor editor = new Editor("C:\\path\\original.docx");

//save 1st version to file through path
String outputPath = "C:\\output_path\\document.rtf";
WordProcessingSaveOptions saveOptions1 = new WordProcessingSaveOptions(WordProcessingFormats.Rtf);, outputPath, saveOptions1);

//save 2nd version to stream
OutputStream outputStream = new ByteArrayOutputStream();
WordProcessingSaveOptions saveOptions2 = new WordProcessingSaveOptions(WordProcessingFormats.Docm);, outputStream, saveOptions2);

As you can see from example above, it is possible to create multiple output documents from a single EditableDocument with different save options and different formats. And these output formats should not be strictly same as format of input document.

Even more: in some cases format family can also be different. For example, original document can be some of WordProcessing formats, while output document can be TXT or PDF. Same transitions are allowed between Spreadsheets and DSV (two-ways). But, of course, they are not allowed, where formats are theoretically incompatible in their essence, like WordProcessing and Spreadsheet.