-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
132 lines (119 loc) · 4.8 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace RichEditControl_Encryption
{
public partial class Form1 : XtraForm
{
int tryCount = 4;
public Form1()
{
InitializeComponent();
InitializeEncryptionComboBoxEdit1();
InitializePasswordEdit();
richEditControl1.LoadDocument("testEncrypted.docx");
}
private void RichEditControl1_BeforeImport(object sender, BeforeImportEventArgs e)
{
//Specify the password to load an encrypted file
richEditControl1.Options.Import.EncryptionPassword = "test";
}
private void RichEditControl1_EncryptedFilePasswordRequested(object sender, EncryptedFilePasswordRequestedEventArgs e)
{
//Count the number of attempts to enter the password
if (tryCount > 0)
{
tryCount--;
}
}
private void RichEditControl1_EncryptedFilePasswordCheckFailed(object sender, EncryptedFilePasswordCheckFailedEventArgs e)
{
//Analyze the error that led to this event
//Depending on the user input and number of attempts,
//Prompt user to enter a password again
//or create an empty file
switch (e.Error)
{
case RichEditDecryptionError.PasswordRequired:
if (tryCount > 0)
{
e.TryAgain = true;
e.Handled = true;
MessageBox.Show("You did not enter the password!", String.Format(" {0} attempts left", tryCount));
}
else
e.TryAgain = false;
break;
case RichEditDecryptionError.WrongPassword:
if (tryCount > 0)
{
if ((MessageBox.Show("The password is incorrect. Try Again?", String.Format(" {0} attempts left", tryCount),
MessageBoxButtons.YesNo)) == DialogResult.Yes)
{
e.TryAgain = true;
e.Handled = true;
}
}
break;
}
e.Handled = true;
}
private void InitializePasswordEdit()
{
passwordEdit1.EditValue = "123";
}
void InitializeEncryptionComboBoxEdit1()
{
foreach (EncryptionType currentValue in Enum.GetValues(typeof(EncryptionType)))
encryptionComboBox1.Properties.Items.Add(currentValue.ToString());
encryptionComboBox1.EditValue = EncryptionType.Strong.ToString();
}
private void SimpleButton1_Click(object sender, EventArgs e)
{
richEditControl1.Document.Encryption.Password = passwordEdit1.EditValue.ToString();
richEditControl1.Document.Encryption.Type = (EncryptionType)Enum.Parse(typeof(EncryptionType), encryptionComboBox1.EditValue.ToString());
richEditControl1.SaveDocumentAs();
}
private void RichEditControl1_DecryptionFailed(object sender, DecryptionFailedEventArgs e)
{
MessageBox.Show(e.Exception.Message.ToString(), "Exception");
}
}
#region #CustomSaveFileDialog
public class MyRichEditControl : RichEditControl
{
public override void SaveDocumentAs()
{
using (SaveFileDialog myFileDialog = new SaveFileDialog())
{
myFileDialog.Filter = "Word 2007 Document (*.docx)|*.docx|Microsoft Word Document (*.doc*)|*.doc*";
myFileDialog.FilterIndex = 1;
myFileDialog.CheckFileExists = false;
DialogResult result = myFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
string ext = Path.GetExtension(myFileDialog.FileName);
if (ext == ".docx")
this.SaveDocument(myFileDialog.FileName, DocumentFormat.OpenXml);
else
this.SaveDocument(myFileDialog.FileName, DocumentFormat.Doc);
if (MessageBox.Show("The document is saved with new password. Open the file?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.LoadDocument(myFileDialog.FileName);
}
}
}
}
}
#endregion #CustomSaveFileDialog
}