This article describes how to obtain edited document content from client, process it and save to the resultant document of some specified format.
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 three static factories, which obtain HTML document in different forms onm input and according to this generate the
FromFilemethod is designed for opening HTML-documents from disk — it obtains path to *.html file (that contains HTML-markup) and an optional path to corresponding resource folder that contains different resources, like stylesheets, images, font files, audio files and so on.
FromMarkupmethod is designed for opening HTML-documents from memory — it obtains the HTML-markup as a
System.Stringand an optional list of IHtmlResource items, like stylesheets, images, fonts, audio resources and so on.
FromMarkupAndResourceFoldermethod is designed for opening HTML-documents from mixed storages — it obtains the HTML-markup as a
System.String, but resources are obtained from a path to corresponding resource folder, which, unlike previous methods, is mandatory (such directory should exist).
When EditableDocument is created, it can be converted to the output document. For doing this, user must use Editor.Save() 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:
- Instance of EditableDocument class, that holds a content of edited document.
- Output document, that is specified as file path (
System.String) or byte stream (
- Mandatory save options, that are represented by one of inheritors of the
Like with load and edit options, every family format has its own class, that implements ISaveOptions interface. These classes are listed below.
|Format family||Example formats||Save options class||Format class|
|WordProcessing||DOC, DOCX, DOCM, DOT, ODT||WordProcessingSaveOptions||WordProcessingFormats|
|Spreadsheet||XLS, XLSX, XLSM, XLSB||SpreadsheetSaveOptions||SpreadsheetFormat|
|Delimiter-Separated Values (DSV)||CSV, TSV||DelimitedTextSaveOptions||TextualFormats|
|Presentation||PPT, PPTX, PPS, POT||PresentationSaveOptions||PresentationFormats|
|Plain Text documents||TXT||TextSaveOptions||TextualFormats|
|EML, EMLX, TNEF, MSG, HTML, MHTML, ICS, VCF, PST, MBOX, OFT||EmailSaveOptions||EmailFormats|
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); editor.Save(document, outputPath, saveOptions1); //save 2nd version to stream MemoryStream outputStream = new MemoryStream(); WordProcessingSaveOptions saveOptions2 = new WordProcessingSaveOptions(WordProcessingFormats.Docm); editor.Save(document, 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. Or original document can be a PDF, while output — DOCX. 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.