Word Editor in WindowsForms

Following sample shows interoperability between GemBox.Document component and Windows Forms RichTextBox control via RTF format.

Sample also implements common rich text editor functionalities and shows how to copy and paste between clipboard and GemBox.Document.

Visual Studio solution can be downloaded from: WindowsFormsRichTextEditor.zip.

Screenshot

Windows Forms RichTextBox / Clipboard Screenshot

See the full code below.

1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Drawing;
5using System.IO;
6using System.Windows.Forms;
7using GemBox.Document;
8
9namespace WindowsFormsRichTextEditor
10{
11    public partial class MainForm : Form
12    {
13        public MainForm()
14        {
15            InitializeComponent();
16
17            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
18        }
19
20        private void menuItemOpen_Click(object sender, EventArgs e)
21        {
22            var dialog = new OpenFileDialog()
23            {
24                AddExtension = true,
25                Filter =
26                    "All Documents (*.docx;*.docm;*.doc;*.dotx;*.dotm;*.dot;*.htm;*.html;*.rtf;*.txt)|*.docx;*.docm;*.dotx;*.dotm;*.doc;*.dot;*.htm;*.html;*.rtf;*.txt|" +
27                    "Word Documents (*.docx)|*.docx|" +
28                    "Word Macro-Enabled Documents (*.docm)|*.docm|" +
29                    "Word 97-2003 Documents (*.doc)|*.doc|" +
30                    "Word Templates (*.dotx)|*.dotx|" +
31                    "Word Macro-Enabled Templates (*.dotm)|*.dotm|" +
32                    "Word 97-2003 Templates (*.dot)|*.dot|" +
33                    "Web Pages (*.htm;*.html)|*.htm;*.html|" +
34                    "Rich Text Format (*.rtf)|*.rtf|" +
35                    "Text Files (*.txt)|*.txt"
36            };
37
38            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
39                using (var stream = new MemoryStream())
40                {
41                    // Convert input file to RTF stream.
42                    DocumentModel.Load(dialog.FileName).Save(stream, SaveOptions.RtfDefault);
43
44                    stream.Position = 0;
45
46                    // Load RTF stream into RichTextBox.
47                    this.richTextBox.LoadFile(stream, RichTextBoxStreamType.RichText);
48                }
49        }
50
51        private void btnSave_Click(object sender, EventArgs e)
52        {
53            var dialog = new SaveFileDialog()
54            {
55                AddExtension = true,
56                Filter =
57                    "Word Document (*.docx)|*.docx|" +
58                    "Word Macro-Enabled Document (*.docm)|*.docm|" +
59                    "Word Template (*.dotx)|*.dotx|" +
60                    "Word Macro-Enabled Template (*.dotm)|*.dotm|" +
61                    "PDF (*.pdf)|*.pdf|" +
62                    "XPS Document (*.xps)|*.xps|" +
63                    "Web Page (*.htm;*.html)|*.htm;*.html|" +
64                    "Single File Web Page (*.mht;*.mhtml)|*.mht;*.mhtml|" +
65                    "Rich Text Format (*.rtf)|*.rtf|" +
66                    "Plain Text (*.txt)|*.txt|" +
67                    "Image (*.png;*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.wdp)|*.png;*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.wdp"
68            };
69
70            if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
71                using (var stream = new MemoryStream())
72                {
73                    // Save RichTextBox content to RTF stream.
74                    this.richTextBox.SaveFile(stream, RichTextBoxStreamType.RichText);
75
76                    stream.Position = 0;
77
78                    // Convert RTF stream to output format.
79                    DocumentModel.Load(stream, LoadOptions.RtfDefault).Save(dialog.FileName);
80                    Process.Start(dialog.FileName);
81                }
82        }
83
84        private void DoGemBoxCopy()
85        {
86            using (var stream = new MemoryStream())
87            {
88                // Save RichTextBox selection to RTF stream.
89                var writer = new StreamWriter(stream);
90                writer.Write(this.richTextBox.SelectedRtf);
91                writer.Flush();
92
93                stream.Position = 0;
94
95                // Save RTF stream to clipboard.
96                DocumentModel.Load(stream, LoadOptions.RtfDefault).Content.SaveToClipboard();
97            }
98        }
99
100        private void DoGemBoxPaste(bool prepend)
101        {
102            using (var stream = new MemoryStream())
103            {
104                // Save RichTextBox content to RTF stream.
105                var writer = new StreamWriter(stream);
106                writer.Write(this.richTextBox.SelectedRtf);
107                writer.Flush();
108
109                stream.Position = 0;
110
111                // Load document from RTF stream and prepend or append clipboard content to it.
112                var document = DocumentModel.Load(stream, LoadOptions.RtfDefault);
113                var content = document.Content;
114                (prepend ? content.Start : content.End).LoadFromClipboard();
115
116                stream.Position = 0;
117
118                // Save document to RTF stream.
119                document.Save(stream, SaveOptions.RtfDefault);
120
121                stream.Position = 0;
122
123                // Load RTF stream into RichTextBox.
124                var reader = new StreamReader(stream);
125                this.richTextBox.SelectedRtf = reader.ReadToEnd();
126            }
127        }
128    }
129}
1Imports System.Collections.Generic
2Imports System.Diagnostics
3Imports System.Drawing
4Imports System.IO
5Imports System.Windows.Forms
6Imports GemBox.Document
7
8Namespace WindowsFormsRichTextEditor
9	Public Partial Class MainForm
10		Inherits Form
11		Public Sub New()
12			InitializeComponent()
13
14			ComponentInfo.SetLicense("FREE-LIMITED-KEY")
15		End Sub
16
17		Private Sub menuItemOpen_Click(sender As Object, e As EventArgs)
18			Dim dialog = New OpenFileDialog() With {
19				.AddExtension = True,
20				.Filter = 
21					"All Documents (*.docx;*.docm;*.doc;*.dotx;*.dotm;*.dot;*.htm;*.html;*.rtf;*.txt)|*.docx;*.docm;*.dotx;*.dotm;*.doc;*.dot;*.htm;*.html;*.rtf;*.txt|" + 
22					"Word Documents (*.docx)|*.docx|" + 
23					"Word Macro-Enabled Documents (*.docm)|*.docm|" + 
24					"Word 97-2003 Documents (*.doc)|*.doc|" + 
25					"Word Templates (*.dotx)|*.dotx|" + 
26					"Word Macro-Enabled Templates (*.dotm)|*.dotm|" + 
27					"Word 97-2003 Templates (*.dot)|*.dot|" + 
28					"Web Pages (*.htm;*.html)|*.htm;*.html|" + 
29					"Rich Text Format (*.rtf)|*.rtf|" + 
30					"Text Files (*.txt)|*.txt"
31			}
32
33			If dialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
34				Using stream = New MemoryStream()
35					' Convert input file to RTF stream.
36					DocumentModel.Load(dialog.FileName).Save(stream, SaveOptions.RtfDefault)
37
38					stream.Position = 0
39
40					' Load RTF stream into RichTextBox.
41					Me.richTextBox.LoadFile(stream, RichTextBoxStreamType.RichText)
42				End Using
43			End If
44		End Sub
45
46		Private Sub btnSave_Click(sender As Object, e As EventArgs)
47			Dim dialog = New SaveFileDialog() With {
48				.AddExtension = True,
49				.Filter = 
50					"Word Document (*.docx)|*.docx|" + 
51					"Word Macro-Enabled Document (*.docm)|*.docm|" + 
52					"Word Template (*.dotx)|*.dotx|" + 
53					"Word Macro-Enabled Template (*.dotm)|*.dotm|" + 
54					"PDF (*.pdf)|*.pdf|" + 
55					"XPS Document (*.xps)|*.xps|" + 
56					"Web Page (*.htm;*.html)|*.htm;*.html|" + 
57					"Single File Web Page (*.mht;*.mhtml)|*.mht;*.mhtml|" + 
58					"Rich Text Format (*.rtf)|*.rtf|" + 
59					"Plain Text (*.txt)|*.txt|" + 
60					"Image (*.png;*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.wdp)|*.png;*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.wdp"
61			}
62
63			If dialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
64				Using stream = New MemoryStream()
65					' Save RichTextBox content to RTF stream.
66					Me.richTextBox.SaveFile(stream, RichTextBoxStreamType.RichText)
67
68					stream.Position = 0
69
70					' Convert RTF stream to output format.
71					DocumentModel.Load(stream, LoadOptions.RtfDefault).Save(dialog.FileName)
72					Process.Start(dialog.FileName)
73				End Using
74			End If
75		End Sub
76
77		Private Sub DoGemBoxCopy()
78			Using stream = New MemoryStream()
79				' Save RichTextBox selection to RTF stream.
80				Dim writer = New StreamWriter(stream)
81				writer.Write(Me.richTextBox.SelectedRtf)
82				writer.Flush()
83
84				stream.Position = 0
85
86				' Save RTF stream to clipboard.
87				DocumentModel.Load(stream, LoadOptions.RtfDefault).Content.SaveToClipboard()
88			End Using
89		End Sub
90
91		Private Sub DoGemBoxPaste(prepend As Boolean)
92			Using stream = New MemoryStream()
93				' Save RichTextBox content to RTF stream.
94				Dim writer = New StreamWriter(stream)
95				writer.Write(Me.richTextBox.SelectedRtf)
96				writer.Flush()
97
98				stream.Position = 0
99
100				' Load document from RTF stream and prepend or append clipboard content to it.
101				Dim document = DocumentModel.Load(stream, LoadOptions.RtfDefault)
102				Dim content = document.Content
103				Dim contentPosition As ContentPosition = If(prepend, content.Start, content.End)
104				contentPosition.LoadFromClipboard()
105
106				stream.Position = 0
107
108				' Save document to RTF stream.
109				document.Save(stream, SaveOptions.RtfDefault)
110
111				stream.Position = 0
112
113				' Load RTF stream into RichTextBox.
114				Dim reader = New StreamReader(stream)
115				Me.richTextBox.SelectedRtf = reader.ReadToEnd()
116			End Using
117		End Sub
118	End Class
119End Namespace

Check next sample or find out more about GemBox.Document and GemBox Software.