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.

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:

  1. 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.
  2. Select a desired output format, into which the modified content should be saved, and optional parameters.
  3. 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:

  1. 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.
  2. 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.
  3. 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).

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

Create and adjust saving options

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.

Format familyExample formatsSave options classFormat class
WordProcessingDOC, DOCX, DOCM, DOT, ODTWordProcessingSaveOptionsWordProcessingFormats
SpreadsheetXLS, XLSX, XLSM, XLSBSpreadsheetSaveOptionsSpreadsheetFormats
Delimiter-Separated Values (DSV)CSV, TSVDelimitedTextSaveOptionsSpreadsheetFormats
PresentationPPT, PPTX, PPS, POTPresentationSaveOptionsPresentationFormats
Plain Text documentsTXTTextSaveOptionsTextualFormats
Fixed-layout formatPDFPdfSaveOptionsFixedLayoutFormats
Fixed-layout formatXPSXpsSaveOptionsFixedLayoutFormats
EmailEML, EMLX, TNEF, MSG, HTML, MHTML, ICS, VCF, PST, MBOX, OFTEmailSaveOptionsEmailFormats
e-BooksePub, Mobi, AZW3EbookSaveOptionsEBookFormats

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:

WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx);

If it is also necessary to encode the resultant document with the password, it can be done using one line of code:

saveOptions.setPassword("some-password");

Of course, different formats have different options. For example, there is a Password property in WordProcessingSaveOptions, SpreadsheetSaveOptions, PresentationSaveOptions, but there is no anything similar in XpsSaveOptions, EmailSaveOptions, TextSaveOptions, and EbookSaveOptions, because these formats do not support password protection.

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.

Here is a signature:

save(EditableDocument inputDocument, String filePath, ISaveOptions saveOptions)
save(EditableDocument inputDocument, OutputStream outputDocument, ISaveOptions saveOptions)

Here:

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

import com.groupdocs.editor.Editor;
import com.groupdocs.editor.EditableDocument;
import com.groupdocs.editor.options.PdfSaveOptions;
import com.groupdocs.editor.options.WordProcessingSaveOptions; 
import com.groupdocs.editor.formats.WordProcessingFormats; 
// ...

// Create Editor class by loading an input document in DOCX format by path
Editor editor = new Editor("input.docx");

// Open document for edit and obtain EditableDocument
EditableDocument original = editor.edit();

// Get the original content as a string
String originalContent = original.getEmbeddedHtml();

// Get the modified content by editing original content
String modifiedContent = originalContent.replace("old", "new");

// Create EditableDocument from modified content
EditableDocument modified = EditableDocument.fromMarkup(modifiedContent, null);

// Create and adjust 2 different saving options
WordProcessingSaveOptions docxSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();

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

Close
Loading

Analyzing your prompt, please hold on...

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