/
ExportActions.cs
163 lines (140 loc) · 6.84 KB
/
ExportActions.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraRichEdit;
using System.Diagnostics;
using DevExpress.XtraPrinting;
using System.IO;
using DevExpress.XtraRichEdit.Export;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit.Export.Html;
namespace RichEditDocumentServerAPIExample.CodeExamples
{
class ExportActions
{
public static Action<RichEditDocumentServer> ExportRangeToHtmlAction = ExportRangeToHtml;
public static Action<RichEditDocumentServer> ExportRangeToPlainTextAction = ExportRangeToPlainText;
public static Action<RichEditDocumentServer> ExportToPDFAction = ExportToPDF;
public static Action<RichEditDocumentServer> ConvertHTMLtoPDFAction = ConvertHTMLtoPDF;
public static Action<RichEditDocumentServer> ConvertHTMLtoDOCXAction = ConvertHTMLtoDOCX;
public static Action<RichEditDocumentServer> ExportToHTMLAction = ExportToHTML;
public static Action<RichEditDocumentServer> BeforeExportAction = BeforeExport;
static void ExportRangeToHtml(RichEditDocumentServer wordProcessor)
{
#region #ExportRangeToHtml
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
DevExpress.XtraRichEdit.API.Native.Document document = wordProcessor.Document;
if (document.Paragraphs.Count > 2)
{
// Access the range of the first three paragraphs.
DocumentRange range = document.CreateRange(document.Paragraphs[0].Range.Start, document.Paragraphs[2].Range.End.ToInt()- document.Paragraphs[0].Range.Start.ToInt());
// Save text contained in the target range in HTML format.
string htmlText = document.GetHtmlText(range, null);
System.IO.File.WriteAllText("test.html", htmlText);
// Show the result in a browser window.
System.Diagnostics.Process.Start("test.html");
}
#endregion #ExportRangeToHtml
}
static void ExportRangeToPlainText(RichEditDocumentServer wordProcessor)
{
#region #ExportRangeToPlainText
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
DevExpress.XtraRichEdit.API.Native.Document document = wordProcessor.Document;
if (document.Paragraphs.Count > 2)
{
// Obtain the plain text contained in the third paragraph.
string plainText = document.GetText(document.Paragraphs[2].Range);
// Show the result in a dialog box.
System.Windows.Forms.MessageBox.Show(plainText);
}
#endregion #ExportRangeToPlainText
}
static void ExportToPDF(RichEditDocumentServer wordProcessor)
{
#region #ExportToPDF
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\MovieRentals.docx", DocumentFormat.OpenXml);
// Specify PDF export options.
PdfExportOptions options = new PdfExportOptions();
options.DocumentOptions.Author = "Mark Jones";
options.Compressed = false;
options.ImageQuality = PdfJpegImageQuality.Highest;
// Export the document to a stream in PDF format.
using (FileStream pdfFileStream = new FileStream("Document_PDF.pdf", FileMode.Create))
{
wordProcessor.ExportToPdf(pdfFileStream, options);
}
// Show the resulting PDF file.
System.Diagnostics.Process.Start("Document_PDF.pdf");
#endregion #ExportToPDF
}
static void ConvertHTMLtoPDF(RichEditDocumentServer wordProcessor)
{
#region #ConvertHTMLtoPDF
// Load a document from an HTML file.
wordProcessor.LoadDocument("Documents\\TextWithImages.htm");
// Save the document as a PDF file.
wordProcessor.ExportToPdf("Document_PDF.pdf");
// Show the resulting PDF file.
System.Diagnostics.Process.Start("Document_PDF.pdf");
#endregion #ConvertHTMLtoPDF
}
static void ConvertHTMLtoDOCX(RichEditDocumentServer wordProcessor)
{
#region #ConvertHTMLtoDOCX
// Load a document from an HTML file.
wordProcessor.LoadDocument("Documents\\TextWithImages.htm");
// Save the document as a DOCX file.
wordProcessor.SaveDocument("Document_DOCX.docx", DocumentFormat.OpenXml);
// Show the resulting DOCX file.
System.Diagnostics.Process.Start("Document_DOCX.docx");
#endregion #ConvertHTMLtoDOCX
}
static void ExportToHTML(RichEditDocumentServer wordProcessor)
{
#region #ExportDocumentToHTML
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\MovieRentals.docx", DocumentFormat.OpenXml);
// Specify the path to the resulting HTML file.
string filePath = "Document_HTML.html";
// Save the document as an HTML file.
using (FileStream htmlFileStream = new FileStream(filePath, FileMode.Create))
{
wordProcessor.SaveDocument(htmlFileStream, DocumentFormat.Html);
}
// Show the resulting HTML file.
System.Diagnostics.Process.Start(filePath);
#endregion #ExportDocumentToHTML
}
static void BeforeExport(RichEditDocumentServer wordProcessor)
{
#region #HandleBeforeExportEvent
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx");
// Handle the Before Export event.
wordProcessor.BeforeExport += (s, e) =>
{
// Specify the export options before a document is exported to HTML.
HtmlDocumentExporterOptions options = e.Options as HtmlDocumentExporterOptions;
if (options != null)
{
options.CssPropertiesExportType = CssPropertiesExportType.Link;
options.HtmlNumberingListExportFormat = HtmlNumberingListExportFormat.HtmlFormat;
options.TargetUri = "Document_HTML.html";
}
};
// Save the document as an HTML file.
wordProcessor.SaveDocument("Document_HTML.html", DocumentFormat.Html);
// Show the resulting HTML file.
System.Diagnostics.Process.Start("Document_HTML.html");
#endregion #HandleBeforeExportEvent
}
}
}