This demonstration shows how to load, open for editing and save text documents, explains edit and save options and their destination.
How to edit 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. Other non-obvious feature, that GroupDocs.Editor provide, is ability to save edited TXT document not only back to TXT, but also to WordProcessing.
Loading text file for edit
Unlike WordProcessing and Spreadsheet documents, DSV documents are loaded into the Editor class without any loading options. They are simple text files by their nature, so it is nothing to adjust:
In order to open a text document for edit by creating the EditableDocument instance it is required to use the TextEditOptions class. This class has several options (properties), that are described below:
Encoding — allows to set character encoding of the input text document. By default, if not specified, is UTF8.
RecognizeLists — boolean flag, that indicates how exactly numbered list items are recognized. If this option is set to false (default value), lists recognition algorithm detects list paragraphs, when list numbers ends with either 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: 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 to enable paged view of the document. In their nature all text documents are pageless, however, GroupDocs.Editor allows to split them on pages, like MS Word does.
Direction — enum flag, which allows to specify the direction of text flow in the input plain text document. By default is Left-to-Right.
Source code below demonstrates using this option class and opening input text document for editing. Then, when EditableDocument instance is ready, example below demonstrates, how to emit HTML markup from it, edit it and create new EditableDocument instance from edited content:
TextEditOptionseditOptions=newTextEditOptions();editOptions.Encoding=System.Text.Encoding.UTF8;editOptions.RecognizeLists=true;editOptions.LeadingSpaces=TextLeadingSpacesOptions.ConvertToIndent;editOptions.TrailingSpaces=TextTrailingSpacesOptions.Trim;editOptions.Direction=TextDirection.Auto;EditableDocumentbeforeEdit=editor.Edit(editOptions);// Create EditableDocument instancestringoriginalTextContent=beforeEdit.GetContent();// Get HTML contentstringupdatedTextContent=originalTextContent.Replace("text","EDITED text");// Edit contentList<IHtmlResource>allResources=beforeEdit.AllResources;// Get resources (only one stylesheet actually in this case)//Finally, create new EditableDocumentEditableDocumentafterEdit=EditableDocument.FromMarkup(updatedTextContent,allResources);
Save text file after edit
After being edited, text document can be saved back as TXT or as WordProcessing. For saving back to TXT format user must use a TextSaveOptions class, that have three properties, described below:
Encoding — character encoding of the text document, which will be applied for its saving. By default, if not specified, is UTF8.
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 the GroupDocs.Editor should try to preserve layout of tables when saving in the plain text format. The default value is false.
Source code below shows how to save EditableDocument to both TXt and WordProcessing formats: