GroupDocs.Editor has no user interface of its own, and that is the thing to understand before reading the code. It converts a document to HTML so that a third-party WYSIWYG editor — TinyMCE, CKEditor, anything that edits HTML — can display and change it, then converts the edited HTML back into the original format.
So the shape is a three-stage round trip rather than a single call:
Editor.Edit() your web editor Editor.Save()
document ──────────► EditableDocument ──► HTML ──► edited HTML ──────────► document
The examples below follow those three stages in order.
Stage 1 and 2: open a document and get its HTML
Edit returns an EditableDocument. GetBodyContent gives you the markup to hand to the browser; GetEmbeddedHtml gives a self-contained page with images and styles inlined.
usingSystem;usingSystem.IO;usingGroupDocs.Editor;usingGroupDocs.Editor.Options;using(Editoreditor=newEditor("contract.docx")){// Stage 1: parse the document into an editable formusing(EditableDocumentdocument=editor.Edit(newWordProcessingEditOptions())){// Stage 2: the HTML your web editor would loadstringbodyHtml=document.GetBodyContent();File.WriteAllText("editor-document-to-html.html",document.GetEmbeddedHtml());Console.WriteLine("Body HTML length: "+bodyHtml.Length+" characters");}}
contract.docx is the sample file used in this example. Click here to download it.
EditableDocument.FromMarkup takes the HTML your editor returned. Save writes it back in whatever format the save options name.
usingSystem;usingGroupDocs.Editor;usingGroupDocs.Editor.Formats;usingGroupDocs.Editor.Options;using(Editoreditor=newEditor("contract.docx")){using(EditableDocumentoriginal=editor.Edit(newWordProcessingEditOptions())){// Stand in for the user's edit in a web editorstringeditedHtml=original.GetBodyContent().Replace("Consulting Services Agreement","Consulting Services Agreement (revised)");using(EditableDocumentedited=EditableDocument.FromMarkup(editedHtml,null)){editor.Save(edited,"editor-html-to-document.docx",newWordProcessingSaveOptions(WordProcessingFormats.Docx));}}}Console.WriteLine("Saved editor-html-to-document.docx");
contract.docx is the sample file used in this example. Click here to download it.
Because saving is a separate stage with its own options, the document can come back in a different format from the one it went in as — edit a DOCX, save RTF.
Save options are mandatory — they carry the target format. There is no “save as it came in” overload that infers it.
Learn more
GroupDocs.Editor also edits spreadsheets, presentations, PDF, email, ebooks, Markdown, XML and CSV; manages the images and stylesheets a document references as separate HTML resources; reads document metainfo without a full parse; and handles Word form fields.