This example shows loading and editing a content of the input DOCX file programmatically by replacing text content on another. After that the modified document content is saved back as a new DOCX document.
Edit input DOCX by replacing text and save it back to DOCX
// Load input document by path and specify load options if necessary
Editor editor = new Editor("input.docx", new WordProcessingLoadOptions());
// Open document for edit and obtain the "EditableDocument"
EditableDocument original = editor.edit();
// Replace text - this emulates the content editing
String modifiedContent = original.getEmbeddedHtml().replace("old text", "new text");
// Create new "EditableDocument" instance from edited content
EditableDocument edited = EditableDocument.fromMarkup(modifiedContent, null);
// Prepare save options with desired output formatX
WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx);
// Save edited document content to DOCX
editor.save(edited, "output.docx", saveOptions);
// Dispose all resources
edited.dispose(); original.dispose(); editor.dispose();
The Spreadsheet document (like XLS, XLSX, XLSM, ODS and so on) may have one or more worksheets (tabs). GroupDocs.Editor allows to edit content of one worksheet at a time. After being edited, this worksheet may be saved to the separate Spreadsheet document (where only this specific worksheet will be saved), or the edited worksheet can be inserted back to the original document, where it can either replace the original worksheet or be saved together, along with original one. This example shows loading XLSX document, editing its 2nd worksheet and saving it as a new separate document in XLSX and CSV formats.
Edit particular worksheet of XLSX and save as XLSX and CSV
// Load input XLSX by path and specify load options if necessary
Editor editor = new Editor("input.xlsx", new SpreadsheetLoadOptions());
// Create and adjust the edit options - set 2nd worksheet to edit
SpreadsheetEditOptions editOptions = new SpreadsheetEditOptions();
editOptions.setWorksheetIndex(1);
// Open this 2nd worksheet for edit and obtain the "EditableDocument"
EditableDocument originalWorksheet = editor.edit(editOptions);
// Replace text - this emulates the content editing
String modifiedContent = originalWorksheet.getEmbeddedHtml().replace("Cell Text", "Edited Cell Text");
// Create new "EditableDocument" instance from edited worksheet
EditableDocument editedWorksheet = EditableDocument.fromMarkup(modifiedContent, null);
// Save edited worksheet to new XLSX document
editor.save(editedWorksheet, "output.xlsx", new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx));
// Save edited worksheet to new CSV document with comma (,) delimiter/separator
editor.save(editedWorksheet, "output.csv", new DelimitedTextSaveOptions(","));
// Dispose all resources
editedWorksheet.dispose(); originalWorksheet.dispose(); editor.dispose();