-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelpers.cs
More file actions
238 lines (220 loc) · 9.17 KB
/
Helpers.cs
File metadata and controls
238 lines (220 loc) · 9.17 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using DevExpress.Docs.Presentation;
using DevExpress.Drawing;
using DevExpress.Pdf;
using DevExpress.Spreadsheet;
using DevExpress.XtraRichEdit;
using System.Drawing;
using System.Globalization;
using PresentationDocumentFormat = DevExpress.Docs.Presentation.DocumentFormat;
using RichEditDocumentFormat = DevExpress.XtraRichEdit.DocumentFormat;
using RichEditEncryptionSettings = DevExpress.XtraRichEdit.API.Native.EncryptionSettings;
using SpreadsheetDocumentFormat = DevExpress.Spreadsheet.DocumentFormat;
using SpreadsheetEncryptionSettings = DevExpress.Spreadsheet.EncryptionSettings;
namespace RichEditOpenAIWebApi.BusinessObjects
{
public static class SharedHelper
{
public static CultureInfo GetTranslationLanguage(TranslationLang translationLanguage)
{
return translationLanguage switch
{
TranslationLang.English => new CultureInfo("en-US"),
TranslationLang.Spanish => new CultureInfo("es-ES"),
TranslationLang.French => new CultureInfo("fr-FR"),
TranslationLang.German => new CultureInfo("de-DE"),
_ => throw new NotSupportedException($"Translation language '{translationLanguage}' is not supported.")
};
}
public static async Task<string> FileToBase64String(IFormFile file)
{
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
byte[] imageBytes = stream.ToArray();
return Convert.ToBase64String(imageBytes);
}
}
}
public static class RichEditHelper
{
public static async Task LoadFile(RichEditDocumentServer server, IFormFile file)
{
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
server.LoadDocument(stream);
}
}
public static Stream SaveDocument(RichEditDocumentServer server, RichEditFormat outputFormat, RichEditEncryptionSettings? encryptionSettings = null)
{
MemoryStream resultStream = new MemoryStream();
if (outputFormat == RichEditFormat.Pdf)
server.ExportToPdf(resultStream);
else
{
RichEditDocumentFormat documentFormat = new RichEditDocumentFormat((int)outputFormat);
if (documentFormat == RichEditDocumentFormat.Html)
server.Options.Export.Html.EmbedImages = true;
if (encryptionSettings == null)
server.SaveDocument(resultStream, documentFormat);
else
server.SaveDocument(resultStream, documentFormat, encryptionSettings);
}
resultStream.Seek(0, SeekOrigin.Begin);
return resultStream;
}
public static string GetContentType(RichEditFormat documentFormat)
{
switch (documentFormat)
{
case RichEditFormat.Doc:
case RichEditFormat.Dot:
return "application/msword";
case RichEditFormat.Docm:
case RichEditFormat.Docx:
case RichEditFormat.Dotm:
case RichEditFormat.Dotx:
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
case RichEditFormat.ePub:
return "application/epub+zip";
case RichEditFormat.Mht:
case RichEditFormat.Html:
return "text/html";
case RichEditFormat.Odt:
return "application/vnd.oasis.opendocument.text";
case RichEditFormat.Txt:
return "text/plain";
case RichEditFormat.Rtf:
return "application/rtf";
case RichEditFormat.Xml:
return "application/xml";
case RichEditFormat.Pdf:
return "application/pdf";
default: return string.Empty;
}
}
}
public static class SpreadsheetHelper
{
public static async Task LoadWorkbook(Workbook workbook, IFormFile file)
{
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
workbook.LoadDocument(stream);
}
}
public static Stream SaveDocument(Workbook workbook, SpreadsheetFormat outputFormat, SpreadsheetEncryptionSettings? encryptionSettings = null)
{
MemoryStream resultStream = new MemoryStream();
SpreadsheetDocumentFormat documentFormat = new SpreadsheetDocumentFormat((int)outputFormat);
if (outputFormat == SpreadsheetFormat.Pdf)
workbook.ExportToPdf(resultStream);
else
{
if (encryptionSettings == null)
workbook.SaveDocument(resultStream, documentFormat);
else
workbook.SaveDocument(resultStream, documentFormat, encryptionSettings);
}
resultStream.Seek(0, SeekOrigin.Begin);
return resultStream;
}
public static string GetContentType(SpreadsheetFormat documentFormat)
{
switch (documentFormat)
{
case SpreadsheetFormat.Xls:
case SpreadsheetFormat.Xlt:
return "application/msword";
case SpreadsheetFormat.Xlsx:
case SpreadsheetFormat.Xlsb:
case SpreadsheetFormat.Xlsm:
case SpreadsheetFormat.Xltm:
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
case SpreadsheetFormat.Html:
return "text/html";
case SpreadsheetFormat.XmlSpreadsheet2003:
return "application/xml";
case SpreadsheetFormat.Pdf:
return "application/pdf";
default: return string.Empty;
}
}
}
public static class PresentationHelper
{
public static async Task<Presentation> LoadPresentation(IFormFile file)
{
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
var presentation = new Presentation(stream);
return presentation;
}
}
public static string GetContentType(PresentationFormat documentFormat)
{
switch (documentFormat)
{
case PresentationFormat.Ppt:
case PresentationFormat.Pptx:
return "application/vnd.ms-powerpoint";
default: return string.Empty;
}
}
public static Stream SaveDocument(Presentation presentation, PresentationFormat outputFormat)
{
MemoryStream resultStream = new MemoryStream();
if (outputFormat == PresentationFormat.Pdf)
presentation.ExportToPdf(resultStream);
else
{
PresentationDocumentFormat documentFormat = new PresentationDocumentFormat((int)outputFormat);
presentation.SaveDocument(resultStream, documentFormat);
}
resultStream.Seek(0, SeekOrigin.Begin);
return resultStream;
}
}
public static class PdfHelper
{
public static async Task LoadPdfDocument(PdfDocumentProcessor processor, IFormFile file)
{
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
processor.LoadDocument(stream, true);
}
}
public static void AddContentToPage(PdfDocumentProcessor pdfDocumentProcessor, PdfPage page, PdfRectangle pageSize, string text)
{
using (PdfGraphics graphics = pdfDocumentProcessor.CreateGraphicsWorldSystem())
{
using (var textBrush = new DXSolidBrush(Color.FromArgb(255, Color.DarkOrange)))
{
DXFont font = new DXFont("Segoe UI", 12, DXFontStyle.Regular);
// Calculate text size
SizeF textSize = graphics.MeasureString(
text,
font,
new PdfStringFormat());
// Calculate an area to draw the text
PointF textPoint = new PointF(0, (float)(pageSize.Height - 10));
RectangleF rectangle = new RectangleF(
0, 10,
(float)pageSize.Width,
(float)(pageSize.Height / 2));
// Draw text at the calculated area
graphics.DrawString(text, font, textBrush, rectangle);
graphics.AddToPageForeground(page);
}
}
}
}
}