/
Form1.cs
77 lines (60 loc) · 2.58 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Commands;
using DevExpress.XtraRichEdit.API.Native;
namespace RichEditEmptyParagraph {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
richEditControl1.CreateCommand(RichEditCommandId.ToggleShowWhitespace).Execute();
}
private void button1_Click(object sender, EventArgs e) {
richEditControl1.BeginUpdate();
RemoveParFromSourceRtf();
richEditControl1.EndUpdate();
}
private void button2_Click(object sender, EventArgs e) {
richEditControl1.BeginUpdate();
RemoveParViaFindReplace();
richEditControl1.EndUpdate();
}
private void button3_Click(object sender, EventArgs e) {
AppendDocumentContent();
}
private void button4_Click(object sender, EventArgs e) {
richEditControl1.BeginUpdate();
RemoveParViaDelete();
richEditControl1.EndUpdate();
}
private void RemoveParFromSourceRtf() {
string sourceRtfText = File.ReadAllText("sample1.rtf");
string modifiedRtfText = sourceRtfText;
int lastParIndex = sourceRtfText.LastIndexOf(@"\par");
if (lastParIndex != -1)
modifiedRtfText = sourceRtfText.Remove(lastParIndex, 4);
richEditControl1.Document.AppendRtfText(modifiedRtfText);
}
private void RemoveParViaFindReplace() {
richEditControl1.Document.AppendRtfText(File.ReadAllText("sample1.rtf"));
DocumentRange[] par = richEditControl1.Document.FindAll("\r", SearchOptions.None);
if (par.Length > 0)
richEditControl1.Document.Replace(par[par.Length - 1], string.Empty);
}
private void AppendDocumentContent() {
richEditControl1.Document.AppendDocumentContent("sample1.rtf", DocumentFormat.Rtf);
}
private void RemoveParViaDelete() {
richEditControl1.LoadDocument("sample2.rtf");
Document document = richEditControl1.Document;
DocumentRange range = document.CreateRange(document.Tables[0].Range.End,
document.Tables[1].Range.Start.ToInt() - document.Tables[0].Range.End.ToInt());
if (document.GetText(range).Trim() == string.Empty)
document.Delete(range);
}
private void ClearDocument() {
richEditControl1.CreateNewDocument();
}
}
}