Edit documents with GroupDocs.Editor

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.

using System;
using System.IO;
using GroupDocs.Editor;
using GroupDocs.Editor.Options;

using (Editor editor = new Editor("contract.docx"))
{
    // Stage 1: parse the document into an editable form
    using (EditableDocument document = editor.Edit(new WordProcessingEditOptions()))
    {
        // Stage 2: the HTML your web editor would load
        string bodyHtml = 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.

<HTML><HEAD><META charset="utf-8" /><TITLE>Consulting Services Agreement - Aldergrove Manufacturing</TITLE><META name="author" content="Dana Whitfield" /><STYLE type="text/css">@page Section1 { 
	margin: 72pt; 
	size: 612pt 792pt; 
}
a { 
	text-decoration: none; 
}
.Default_Paragraph_Font, .BuiltIn-style, .Character-style { 
	-aw-style-name: defaultparagraphfont; 
	-gd-style-name: 'Default Paragraph Font'; 
[TRUNCATED]

Download full output

Stage 3: save edited HTML back to Word

EditableDocument.FromMarkup takes the HTML your editor returned. Save writes it back in whatever format the save options name.

using System;
using GroupDocs.Editor;
using GroupDocs.Editor.Formats;
using GroupDocs.Editor.Options;

using (Editor editor = new Editor("contract.docx"))
{
    using (EditableDocument original = editor.Edit(new WordProcessingEditOptions()))
    {
        // Stand in for the user's edit in a web editor
        string editedHtml = original.GetBodyContent()
            .Replace("Consulting Services Agreement", "Consulting Services Agreement (revised)");

        using (EditableDocument edited = EditableDocument.FromMarkup(editedHtml, null))
        {
            editor.Save(edited, "editor-html-to-document.docx",
                new WordProcessingSaveOptions(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.

Binary file (DOCX, 13 KB)

Download full output

Convert format on the way out

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.

using System;
using GroupDocs.Editor;
using GroupDocs.Editor.Formats;
using GroupDocs.Editor.Options;

using (Editor editor = new Editor("contract.docx"))
{
    using (EditableDocument document = editor.Edit(new WordProcessingEditOptions()))
    {
        editor.Save(document, "editor-save-as-rtf.rtf",
            new WordProcessingSaveOptions(WordProcessingFormats.Rtf));
    }
}

Console.WriteLine("Saved editor-save-as-rtf.rtf");

contract.docx is the sample file used in this example. Click here to download it.

Binary file (RTF, 694 KB)

Download full output

Note
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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.