This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
124 lines (113 loc) · 4.93 KB
/
MainWindow.xaml.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
using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using DevExpress.Xpf.Editors;
using DevExpress.XtraRichEdit.Export;
using DevExpress.XtraRichEdit.Export.Html;
using DevExpress.Office.Services;
namespace ExportOnlyBodyContent {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
CssPropertiesExportType cssExportType;
ExportRootTag htmlExportType;
public MainWindow() {
InitializeComponent();
InitComboHtmlExportType();
InitComboCssExportType();
}
#region Initializing
private void InitComboHtmlExportType() {
ListItemCollection collExportHtml = edtExportHtmlType.Items;
collExportHtml.BeginUpdate();
collExportHtml.Clear();
collExportHtml.Add(ExportRootTag.Body);
collExportHtml.Add(ExportRootTag.Html);
collExportHtml.EndUpdate();
edtExportHtmlType.SelectedIndex = 0;
htmlExportType = ExportRootTag.Body;
}
private void InitComboCssExportType() {
ListItemCollection collCssStyle = edtCssStyleType.Items;
collCssStyle.BeginUpdate();
collCssStyle.Clear();
collCssStyle.Add(CssPropertiesExportType.Style);
collCssStyle.Add(CssPropertiesExportType.Link);
collCssStyle.Add(CssPropertiesExportType.Inline);
collCssStyle.EndUpdate();
edtCssStyleType.SelectedIndex = 0;
cssExportType = CssPropertiesExportType.Style;
}
#endregion
private void btnLoadDocument_Click(object sender, RoutedEventArgs e) {
this.richEditControl1.LoadDocument();
}
private void richEditControl1_DocumentLoaded(object sender, EventArgs e) {
try {
string fileName = richEditControl1.Options.DocumentSaveOptions.CurrentFileName;
if (!String.IsNullOrEmpty(fileName)) {
using (StreamReader reader = new StreamReader(fileName)) {
this.memoEdit1.Text = reader.ReadToEnd();
}
}
}
catch {
}
}
private void richEditControl1_EmptyDocumentCreated(object sender, EventArgs e) {
this.memoEdit1.Text = String.Empty;
}
#region Adjusting
private string GetFileName(string filter) {
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = filter;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CheckFileExists = false;
saveFileDialog.CheckPathExists = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.DereferenceLinks = true;
saveFileDialog.ValidateNames = true;
if (saveFileDialog.ShowDialog(this) == true)
return saveFileDialog.FileName;
return String.Empty;
}
private void SaveFile(string fileName, string value) {
using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) {
using (StreamWriter writer = new StreamWriter(stream)) {
writer.Write(value);
}
}
}
private void edtCssStyleType_SelectedIndexChanged(object sender, RoutedEventArgs e) {
this.cssExportType = (CssPropertiesExportType)edtCssStyleType.EditValue;
}
private void edtExportHtmlType_SelectedIndexChanged(object sender, RoutedEventArgs e) {
this.htmlExportType = (ExportRootTag)edtExportHtmlType.EditValue;
}
#endregion
private void btnExportHtml_Click(object sender, RoutedEventArgs e) {
string fileName = GetFileName("HyperText Markup Language Format|*.html");
if (String.IsNullOrEmpty(fileName))
return;
IUriProviderService svc = (IUriProviderService)richEditControl1.GetService(typeof(IUriProviderService));
svc.RegisterProvider(new MyUriProvider(Path.GetDirectoryName(fileName)));
string stringHtml = String.Empty;
ExportHtml(out stringHtml, null, fileName);
this.memoEdit2.Text = stringHtml;
SaveFile(fileName, stringHtml);
}
#region #exporting
private void ExportHtml(out string stringHtml, HtmlExporter exporter, string fileName) {
stringHtml = String.Empty;
HtmlDocumentExporterOptions options = new HtmlDocumentExporterOptions();
options.ExportRootTag = htmlExportType;
options.CssPropertiesExportType = cssExportType;
options.TargetUri = Path.GetFileNameWithoutExtension(fileName);
exporter = new HtmlExporter(this.richEditControl1.Model, options);
stringHtml = exporter.Export();
}
#endregion #exporting
}
}