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
const loadOptions = new WordProcessingLoadOptions();
const editor = new Editor("input.docx", loadOptions);
// Open document for edit and obtain the "EditableDocument"
const original = editor.edit();
// Replace text - this emulates the content editing
const modifiedContent = original.getEmbeddedHtml().replace("old text", "new text");
// Create new "EditableDocument" instance from edited content
const edited = EditableDocument.fromMarkup(modifiedContent, null);
// Prepare save options with desired output formatX
const saveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx);
// Save edited document content to DOCX
await 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
const loadOptions = new SpreadsheetLoadOptions();
const editor = new Editor("input.xlsx", loadOptions);
// Create and adjust the edit options - set 2nd worksheet to edit
const editOptions = new SpreadsheetEditOptions();
editOptions.setWorksheetIndex(1);
// Open this 2nd worksheet for edit and obtain the "EditableDocument"
const originalWorksheet = editor.edit(editOptions);
// Replace text - this emulates the content editing
const modifiedContent = originalWorksheet.getEmbeddedHtml().replace("Cell Text", "Edited Cell Text");
// Create new "EditableDocument" instance from edited worksheet
const editedWorksheet = EditableDocument.fromMarkup(modifiedContent, null);
// Save edited worksheet to new XLSX document
const saveSpreadsheetOptions = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx);
await editor.save(editedWorksheet, "output.xlsx", saveSpreadsheetOptions);
// Save edited worksheet to new CSV document with comma (,) delimiter/separator
const saveTextOptions = new DelimitedTextSaveOptions(",");
await editor.save(editedWorksheet, "output.csv", saveTextOptions);
// Dispose all resources
editedWorksheet.dispose(); originalWorksheet.dispose(); editor.dispose();