This demonstration shows how to load, open for editing, and save textual documents, explains edit and save options and their purpose.
How to Edit a TXT File?
Textual documents are simple plain text flat files (TXT) that contain no images, pages, paragraphs, lists, tables, and so on. However, users can create some primitive formatting like lists with leading markers, left indents with whitespaces, tables with pseudo-graphics, paragraphs with line breaks, and so on. GroupDocs.Editor can recognize some of these structures. Another non-obvious feature that GroupDocs.Editor provides is the ability to save an edited TXT document not only back to TXT but also to WordProcessing formats.
Loading Text File for Editing
Unlike WordProcessing and Spreadsheet documents, text documents are loaded into the Editor class without any loading options. They are simple text files by their nature, so there is nothing to adjust:
// Import the necessary modules
constgroupdocsEditor=require('groupdocs-editor');// Specify the input TXT file path
constinputTxtPath="C://input//file.txt";// Create an instance of Editor
consteditor=newgroupdocsEditor.Editor(inputTxtPath);
Edit Text File
In order to open a text document for editing by creating an EditableDocument instance, it is required to use the TextEditOptions class. This class has several options (properties) that are described below:
Encoding — allows setting the character encoding of the input text document. By default, if not specified, it is UTF-8.
RecognizeLists — boolean flag that indicates how exactly numbered list items are recognized. If this option is set to false (default value), the list recognition algorithm detects list paragraphs when list numbers end with either a dot, right bracket, or bullet symbols (such as “•”, “*”, “-” or “o”). If this option is set to true, whitespaces are also used as list number delimiters: the list recognition algorithm for Arabic style numbering (1., 1.1.2.) uses both whitespaces and dot (".") symbols.
LeadingSpaces — enum flag that indicates how consecutive leading spaces should be handled: convert to left indent (default value), preserve as consecutive spaces, or trim.
TrailingSpaces — enum flag that indicates how consecutive trailing spaces should be handled: truncated (default value) or trim.
EnablePagination — boolean flag that allows enabling paged view of the document. By their nature, all text documents are pageless; however, GroupDocs.Editor allows splitting them into pages, like MS Word does.
Direction — enum flag that allows specifying the direction of text flow in the input plain text document. By default, it is Left-to-Right.
The source code below demonstrates using this options class and opening an input text document for editing. Then, when the EditableDocument instance is ready, the example below demonstrates how to emit HTML markup from it, edit it, and create a new EditableDocument instance from the edited content:
// Import the necessary modules
constgroupdocsEditor=require('groupdocs-editor');// Prepare TextEditOptions
consteditOptions=newgroupdocsEditor.TextEditOptions();editOptions.setEncoding('UTF-8');editOptions.setRecognizeLists(true);editOptions.setLeadingSpaces(groupdocsEditor.TextLeadingSpacesOptions.ConvertToIndent);editOptions.setTrailingSpaces(groupdocsEditor.TextTrailingSpacesOptions.Trim);editOptions.setDirection(groupdocsEditor.TextDirection.Auto);// Create EditableDocument instance
constbeforeEdit=editor.edit(editOptions);// Get HTML content
constoriginalTextContent=beforeEdit.getContent();// Edit content
constupdatedTextContent=originalTextContent.replace(/text/g,'EDITED text');// Get resources (only one stylesheet actually in this case)
constallResources=beforeEdit.getAllResources();// Finally, create new EditableDocument
constafterEdit=groupdocsEditor.EditableDocument.fromMarkup(updatedTextContent,allResources);
Save Text File After Editing
After being edited, the text document can be saved back as TXT or as a WordProcessing document. To save back to TXT format, you must use the TextSaveOptions class, which has three properties described below:
Encoding — character encoding of the text document, which will be applied for its saving. By default, if not specified, it is UTF-8.
AddBidiMarks — boolean flag that determines whether to add bi-directional marks before each BiDi run when saving in plain text format.
PreserveTableLayout — boolean flag that specifies whether GroupDocs.Editor should try to preserve the layout of tables when saving in the plain text format. The default value is false.
The source code below shows how to save the EditableDocument to both TXT and WordProcessing formats:
// Import the necessary modules
constgroupdocsEditor=require('groupdocs-editor');// Prepare TextSaveOptions
consttxtSaveOptions=newgroupdocsEditor.TextSaveOptions();txtSaveOptions.setAddBidiMarks(true);txtSaveOptions.setPreserveTableLayout(true);// Prepare WordProcessingSaveOptions
constwordSaveOptions=newgroupdocsEditor.WordProcessingSaveOptions(groupdocsEditor.WordProcessingFormats.Docm);// Specify output paths
constoutputTxtPath="C:\\output\\document.txt";constoutputWordPath="C:\\output\\document.docm";// Save the edited document as TXT
editor.save(afterEdit,outputTxtPath,txtSaveOptions);// Save the edited document as WordProcessing format
editor.save(afterEdit,outputWordPath,wordSaveOptions);
By following this guide, you can effectively edit plain text files using GroupDocs.Editor for Node.js via Java, customizing the editing process according to your specific needs.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.