This article describes how to obtain edited document content from client, process it and save to the resultant document of some specified format.
In a nutshell, the core process of document editing, where a user types some text across the pages of the document, inserts images, makes some edits, removes words or paragraphs, or moves some document parts from one location to another, is performed in some 3rd party software with GUI outside of the GroupDocs.Editor. This software may be, for example, but not limiting to:
a web-based WYSIWYG HTML-editor, that is usually a pure client-side application, written on JavaScript and running in the browser. This may be, for example, a TinyMCE or CKEditor;
desktop (like WinForms or WPF) application;
mobile application, running on Android or iOS.
The core requirement for this application is to be able to open, view and edit HTML content. The GroupDocs.Editor on its side accepts various document formats (WordProcessing, Spreadsheet, Presentation, and many more) and prepares them for editing in external applications by converting to the HTML markup and placing it in the EditableDocument container. So when calling the Editor.Edit() method, the EditableDocument with the original content inside it is generated.
Then the user obtains this original content from the EditableDocument, pushes it to the external HTML-editor, makes edits, and when these edits are done, a user has the modified content. In the GroupDocs.Editor terminology saving a document means obtaining the modified content and generating the document in output format (WordProcessing, Spreadsheet, Presentation, and many more) from it. And this article explains how to do this.
In short, saving a document implies the next three steps:
Obtain the modified content from somewhere (HTML-editor or any other storage or method) and create an instance of EditableDocument from it. EditableDocument actually serves as an object-oriented wrapper of the document content.
Select a desired output format, into which the modified content should be saved, and optional parameters.
Save the modified content to the document of previously chosen format by specified file path or into specified stream using the Editor.Save() method.
These three steps are explained in detail and with code samples below.
Obtaining the modified content
Different content editors provide the content in different forms. Some may return HTML markup as a String, while the external resources like stylesheet(s) and/or image(s) are located in some specific folder as files. Others may return both main content and resources as a collection of byte streams. Some HTML-editors may generate a single string, which already contains all HTML markup with resources baked into it using base64 encoding. There may be unlimited possibilities to do that, and thus the EditableDocument class has different static factories, which obtain modified content of the HTML document in different forms on input and according to this generate the EditableDocument instance:
fromFile method 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.
fromMarkup method is designed for opening HTML-documents from memory — it obtains HTML-markup as a String and an optional list of IHtmlResource items, like stylesheets, images, fonts, audio resources and so on.
fromMarkupAndResourceFolder method is designed for opening HTML-documents from mixed storages — it obtains the HTML-markup as a String, but resources are obtained from a path to corresponding resource folder, which, unlike previous methods, is mandatory (such directory should exist).
When the EditableDocument instance with the modified content is created, it’s time to save it to the resultant document of some defined format, and the GroupDocs.Editor needs to know this format. Like with load and edit options, every family format has its own class that implements ISaveOptions interface. These classes are listed below.
So, let’s say it is necessary to save the modified content to the document of DOCX format. DOCS is the part of WordProcessing family. So the instance of the WordProcessingSaveOptions class should be created, and the WordProcessingFormats.Docx value should be specified in its constructor. Like this:
Also need to mention that the format of input document with original content and format of the resultant document with modified content may be different. For example, the original document can be some WordProcessing formats, while the output document can be TXT or PDF. Or the 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.
Saving modified content to the document
Finally, when the instance of EditableDocument class with modified content inside is created, and the format of the resultant document is defined, it is possible to generate this resultant document using the Editor.Save() method. This method 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 the same.
The 1st parameter — EditableDocument inputDocument — is a EditableDocument instance with modified content inside, created on the 1st step.
The 2nd parameter is a OutputStream, into which the resultant document of defined format should be written, or a file path, where the resultant document of defined format should be stored.
The 3rd parameter is an instance of some save options, which defines the format of the resultant document and additional adjustments, created on the 2nd step.
Complete code example
Because the WYSIWYG HTML-editor is not a part of the GroupDocs.Editor, it is hard to provide a lightweight code example with fully functional editing. So in this sample the content will be edited programmatically, using a String.Replace method.
importcom.groupdocs.editor.Editor;importcom.groupdocs.editor.EditableDocument;importcom.groupdocs.editor.options.PdfSaveOptions;importcom.groupdocs.editor.options.WordProcessingSaveOptions;importcom.groupdocs.editor.formats.WordProcessingFormats;// ...
// Create Editor class by loading an input document in DOCX format by path
Editoreditor=newEditor("input.docx");// Open document for edit and obtain EditableDocument
EditableDocumentoriginal=editor.edit();// Get the original content as a string
StringoriginalContent=original.getEmbeddedHtml();// Get the modified content by editing original content
StringmodifiedContent=originalContent.replace("old","new");// Create EditableDocument from modified content
EditableDocumentmodified=EditableDocument.fromMarkup(modifiedContent,null);// Create and adjust 2 different saving options
WordProcessingSaveOptionsdocxSaveOptions=newWordProcessingSaveOptions(WordProcessingFormats.Docx);PdfSaveOptionspdfSaveOptions=newPdfSaveOptions();// Save modified content to the 2 documents
editor.save(modified,"output.docx",docxSaveOptions);editor.save(modified,"output.pdf",pdfSaveOptions);// Dispose all
original.dispose();modified.dispose();editor.dispose();
In this example 2 different save options are created and two different Editor.Save() calls are made for saving the modified content to the DOCX and PDF formats.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.