protected internal static void ExpandFieldsInBookRange(DocumentRange range, IWorkbook defaultSpreadsheet, Hashtable snippets = null)
        {
            var doc = range.BeginUpdateDocument();

            try
            {
                var fieldRanges = doc.FindAll(new Regex("(?:{{|(?:{(?:}}|[^}])*}(?!})))"), range);
                if (fieldRanges == null || fieldRanges.Length <= 0)
                {
                    return;
                }

                foreach (var fieldRange in fieldRanges)
                {
                    var fieldText = doc.GetText(fieldRange);
                    if (fieldText == "{{")
                    {
                        doc.Replace(fieldRange, "{");
                        continue;
                    }

                    var code = Utils.TrimString(UnpackField(fieldText));

                    if (code.StartsWith("#"))   //Fields starting with # - #FILE, #IMAGE, #LATEX etc. - calculate field and insert its content instead of field itself
                    {
                        var codeName = Regex.Match(code, @"(?<=^#)[\w_\-]+").Value;

                        using var book = new InternalBook()
                              {
                                  DefaultSpreadsheet = defaultSpreadsheet,
                                  NeedSynchronizeDefaultSpreadsheet = true,
                                  Snippets = ConvertSnippets()
                              };
                        var helperFieldRange = book.Document.AppendText($"DOCVARIABLE {code[1..]}");
        private void SaveSelectedRange()
        {
            DocumentRange selection   = richEditControl.Document.Selection;
            SubDocument   subDocument = selection.BeginUpdateDocument();

            sourceSelectedRange = subDocument.CreateRange(selection.Start, richEditControl.Document.Selection.Length);
            selection.EndUpdateDocument(subDocument);
        }
        void ResetRange(DocumentRange r)
        {
            SubDocument         d  = r.BeginUpdateDocument();
            CharacterProperties cp = d.BeginUpdateCharacters(r);

            cp.BackColor = System.Drawing.Color.White;
            d.EndUpdateCharacters(cp);
            r.EndUpdateDocument(d);
        }
        private void btnSetRange_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (coloredRange != null)
            {
                ResetRange(coloredRange);
            }

            coloredRange = richEditControl1.Document.Selection;
            if (coloredRange.Length == 0)
            {
                return;
            }

            // Highlight selected range.
            SubDocument         d  = coloredRange.BeginUpdateDocument();
            CharacterProperties cp = d.BeginUpdateCharacters(coloredRange);

            cp.BackColor = System.Drawing.Color.Yellow;
            d.EndUpdateCharacters(cp);
            coloredRange.EndUpdateDocument(d);

            // Create a new iterator limited to the specified range.
            layoutIterator = new LayoutIterator(richEditControl1.DocumentLayout, coloredRange);

            doc = coloredRange.BeginUpdateDocument();
            richEditControl1.Document.ChangeActiveDocument(doc);
            coloredRange.EndUpdateDocument(doc);

            // Select the first element in the highlighted range.
            RangedLayoutElement el = richEditControl1.DocumentLayout.GetElement(coloredRange.Start, LayoutType.PlainTextBox);

            while (layoutIterator.MoveNext())
            {
                RangedLayoutElement element = layoutIterator.Current as RangedLayoutElement;
                if ((element != null) && (element.Equals(el)))
                {
                    UpdateInfoAndSelection();
                    return;
                }
            }
        }
 private void btnSelectionText_Click(object sender, EventArgs e)
 {
     #region #SelectionText
     DocumentRange range     = richEditControl1.Document.Selection;
     SubDocument   doc       = range.BeginUpdateDocument();
     string        plainText = doc.GetText(range);
     MessageBox.Show(plainText, "Selected Text");
     doc.InsertText(range.Start, "->");
     doc.InsertText(range.End, "<-");
     range.EndUpdateDocument(doc);
     #endregion #SelectionText
 }
        static void buttonCustomAction_ItemClick_Replace(object sender, ItemClickEventArgs e)
        {
            RichEditControl richEdit  = e.Item.Tag as RichEditControl;
            DocumentRange   range     = richEdit.Document.Selection;
            int             selLength = range.Length;
            string          s         = new String('*', selLength);
            SubDocument     doc       = range.BeginUpdateDocument();

            doc.InsertSingleLineText(range.Start, s);
            DocumentRange rangeToRemove = doc.CreateRange(range.Start, selLength);

            doc.Delete(rangeToRemove);
            range.EndUpdateDocument(doc);
        }
        void biClickMe_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            #region #formatting
            DocumentRange range = richEditControl1.Document.Selection;

            SubDocument doc = range.BeginUpdateDocument();

            CharacterProperties charprop = doc.BeginUpdateCharacters(range);
            charprop.BackColor = Colors.Yellow;
            charprop.AllCaps   = true;
            doc.EndUpdateCharacters(charprop);

            ParagraphProperties parprop = doc.BeginUpdateParagraphs(range);
            parprop.Alignment = ParagraphAlignment.Center;
            doc.EndUpdateParagraphs(parprop);

            range.EndUpdateDocument(doc);
            #endregion #formatting
        }
        private void ApplyFormatToSelectedText()
        {
            DocumentRange targetSelectedRange = richEditControl.Document.Selection;

            richEditControl.BeginUpdate();
            SubDocument targetSubDocument = targetSelectedRange.BeginUpdateDocument();
            SubDocument subDocument       = sourceSelectedRange.BeginUpdateDocument();

            DevExpress.XtraRichEdit.API.Native.CharacterProperties targetCharactersProperties = targetSubDocument.BeginUpdateCharacters(targetSelectedRange);
            DevExpress.XtraRichEdit.API.Native.CharacterProperties sourceCharactersProperties = subDocument.BeginUpdateCharacters(sourceSelectedRange);
            targetCharactersProperties.Assign(sourceCharactersProperties);
            subDocument.EndUpdateCharacters(sourceCharactersProperties);
            targetSubDocument.EndUpdateCharacters(targetCharactersProperties);

            DevExpress.XtraRichEdit.API.Native.ParagraphProperties targetParagraphProperties = targetSubDocument.BeginUpdateParagraphs(targetSelectedRange);
            DevExpress.XtraRichEdit.API.Native.ParagraphProperties sourceParagraphProperties = subDocument.BeginUpdateParagraphs(sourceSelectedRange);
            targetParagraphProperties.Assign(sourceParagraphProperties);
            subDocument.EndUpdateParagraphs(sourceParagraphProperties);
            targetSubDocument.EndUpdateParagraphs(targetParagraphProperties);

            sourceSelectedRange.EndUpdateDocument(subDocument);
            targetSelectedRange.EndUpdateDocument(targetSubDocument);
            richEditControl.EndUpdate();
        }