This repository has been archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeController.cs
98 lines (86 loc) · 4.47 KB
/
HomeController.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
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using DevExpress.XtraRichEdit;
using System.IO;
using DevExpress.XtraPrinting;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraPrintingLinks;
namespace T135508.Controllers {
public class HomeController : Controller {
public ActionResult Index() {
return View();
}
public ActionResult HtmlEditorPartial() {
return PartialView("_HtmlEditorPartial");
}
public ActionResult HtmlEditorPartialImageSelectorUpload() {
HtmlEditorExtension.SaveUploadedImage("HtmlEditor", HomeControllerControllerHtmlEditorSettings.ImageSelectorSettings);
return null;
}
public ActionResult HtmlEditorPartialImageUpload() {
HtmlEditorExtension.SaveUploadedImage("HtmlEditor", HomeControllerControllerHtmlEditorSettings.ImageUploadValidationSettings, HomeControllerControllerHtmlEditorSettings.ImageUploadDirectory);
return null;
}
public ActionResult Export(string HtmlEditor) {
using (MemoryStream stream = new MemoryStream()) {
ExporToStream(HtmlEditor, stream);
Response.Clear();
Response.Buffer = false;
Response.AppendHeader("Content-Type", "application/pdf");
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.AppendHeader("Content-Disposition", "attachment; filename=document.pdf");
Response.BinaryWrite(stream.ToArray());
Response.End();
}
return View("Index");
}
void ExporToStream(string html, Stream stream) {
using (RichEditDocumentServer server = new RichEditDocumentServer()) {
server.HtmlText = html;
string text = "<b>SOME SAMPLE TEXT</b>";
AddHeaderToDocument(server.Document, text);
AddFooterToDocument(server.Document, text);
using (PrintingSystemBase ps = new PrintingSystemBase()) {
using (PrintableComponentLinkBase pcl = new PrintableComponentLinkBase(ps)) {
pcl.Component = server;
pcl.CreateDocument();
ps.ExportToPdf(stream);
}
}
}
}
void AddHeaderToDocument(DevExpress.XtraRichEdit.API.Native.Document document, string htmlText) {
SubDocument doc = document.Sections[0].BeginUpdateHeader();
doc.AppendHtmlText(htmlText);
document.Sections[0].EndUpdateHeader(doc);
}
void AddFooterToDocument(DevExpress.XtraRichEdit.API.Native.Document document, string htmlText) {
SubDocument doc = document.Sections[0].BeginUpdateFooter();
doc.AppendHtmlText(htmlText);
document.Sections[0].EndUpdateFooter(doc);
}
}
public class HomeControllerControllerHtmlEditorSettings {
public const string ImageUploadDirectory = "~/Content/UploadImages/";
public const string ImageSelectorThumbnailDirectory = "~/Content/Thumb/";
public static DevExpress.Web.UploadControlValidationSettings ImageUploadValidationSettings = new DevExpress.Web.UploadControlValidationSettings() {
AllowedFileExtensions = new string[] { ".jpg", ".jpeg", ".jpe", ".gif", ".png" },
MaxFileSize = 4000000
};
static DevExpress.Web.Mvc.MVCxHtmlEditorImageSelectorSettings imageSelectorSettings;
public static DevExpress.Web.Mvc.MVCxHtmlEditorImageSelectorSettings ImageSelectorSettings {
get {
if (imageSelectorSettings == null) {
imageSelectorSettings = new DevExpress.Web.Mvc.MVCxHtmlEditorImageSelectorSettings(null);
imageSelectorSettings.Enabled = true;
imageSelectorSettings.UploadCallbackRouteValues = new { Controller = "Home", Action = "HtmlEditorPartialImageSelectorUpload" };
imageSelectorSettings.CommonSettings.RootFolder = ImageUploadDirectory;
imageSelectorSettings.CommonSettings.ThumbnailFolder = ImageSelectorThumbnailDirectory;
imageSelectorSettings.CommonSettings.AllowedFileExtensions = new string[] { ".jpg", ".jpeg", ".jpe", ".gif" };
imageSelectorSettings.UploadSettings.Enabled = true;
}
return imageSelectorSettings;
}
}
}
}