1- using DevExpress . Spreadsheet ;
1+ using DevExpress . Docs . Presentation ;
2+ using DevExpress . Drawing ;
3+ using DevExpress . Pdf ;
4+ using DevExpress . Spreadsheet ;
25using DevExpress . XtraRichEdit ;
6+ using System . Drawing ;
7+ using System . Globalization ;
8+ using PresentationDocumentFormat = DevExpress . Docs . Presentation . DocumentFormat ;
39using RichEditDocumentFormat = DevExpress . XtraRichEdit . DocumentFormat ;
410using RichEditEncryptionSettings = DevExpress . XtraRichEdit . API . Native . EncryptionSettings ;
511using SpreadsheetDocumentFormat = DevExpress . Spreadsheet . DocumentFormat ;
612using SpreadsheetEncryptionSettings = DevExpress . Spreadsheet . EncryptionSettings ;
713
814namespace RichEditOpenAIWebApi . BusinessObjects
915{
16+
17+ public static class SharedHelper
18+ {
19+ public static CultureInfo GetTranslationLanguage ( TranslationLang translationLanguage )
20+ {
21+ return translationLanguage switch
22+ {
23+ TranslationLang . English => new CultureInfo ( "en-US" ) ,
24+ TranslationLang . Spanish => new CultureInfo ( "es-ES" ) ,
25+ TranslationLang . French => new CultureInfo ( "fr-FR" ) ,
26+ TranslationLang . German => new CultureInfo ( "de-DE" ) ,
27+ _ => throw new NotSupportedException ( $ "Translation language '{ translationLanguage } ' is not supported.")
28+ } ;
29+ }
30+
31+ public static async Task < string > FileToBase64String ( IFormFile file )
32+ {
33+ using ( var stream = new MemoryStream ( ) )
34+ {
35+ await file . CopyToAsync ( stream ) ;
36+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
37+ byte [ ] imageBytes = stream . ToArray ( ) ;
38+ return Convert . ToBase64String ( imageBytes ) ;
39+ }
40+ }
41+ }
1042 public static class RichEditHelper
1143 {
1244 public static async Task LoadFile ( RichEditDocumentServer server , IFormFile file )
@@ -66,16 +98,7 @@ public static string GetContentType(RichEditFormat documentFormat)
6698 default : return string . Empty ;
6799 }
68100 }
69- public static async Task < string > FileToBase64String ( IFormFile file )
70- {
71- using ( var stream = new MemoryStream ( ) )
72- {
73- await file . CopyToAsync ( stream ) ;
74- stream . Seek ( 0 , SeekOrigin . Begin ) ;
75- byte [ ] imageBytes = stream . ToArray ( ) ;
76- return Convert . ToBase64String ( imageBytes ) ;
77- }
78- }
101+
79102 }
80103 public static class SpreadsheetHelper
81104 {
@@ -130,7 +153,86 @@ public static string GetContentType(SpreadsheetFormat documentFormat)
130153 default : return string . Empty ;
131154 }
132155
156+ }
157+ }
158+ public static class PresentationHelper
159+ {
160+ public static async Task < Presentation > LoadPresentation ( IFormFile file )
161+ {
162+ using ( var stream = new MemoryStream ( ) )
163+ {
164+ await file . CopyToAsync ( stream ) ;
165+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
166+ var presentation = new Presentation ( stream ) ;
167+ return presentation ;
168+ }
169+ }
170+
171+ public static string GetContentType ( PresentationFormat documentFormat )
172+ {
173+ switch ( documentFormat )
174+ {
175+ case PresentationFormat . Ppt :
176+ case PresentationFormat . Pptx :
177+ return "application/vnd.ms-powerpoint" ;
178+ default : return string . Empty ;
179+ }
180+ }
181+
182+ public static Stream SaveDocument ( Presentation presentation , PresentationFormat outputFormat )
183+ {
184+ MemoryStream resultStream = new MemoryStream ( ) ;
185+ if ( outputFormat == PresentationFormat . Pdf )
186+ presentation . ExportToPdf ( resultStream ) ;
187+ else
188+ {
189+ PresentationDocumentFormat documentFormat = new PresentationDocumentFormat ( ( int ) outputFormat ) ;
190+ presentation . SaveDocument ( resultStream , documentFormat ) ;
191+ }
192+ resultStream . Seek ( 0 , SeekOrigin . Begin ) ;
193+ return resultStream ;
194+ }
133195
196+ }
197+ public static class PdfHelper
198+ {
199+
200+ public static async Task LoadPdfDocument ( PdfDocumentProcessor processor , IFormFile file )
201+ {
202+ using ( var stream = new MemoryStream ( ) )
203+ {
204+ await file . CopyToAsync ( stream ) ;
205+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
206+ processor . LoadDocument ( stream , true ) ;
207+ }
208+ }
209+
210+ public static void AddContentToPage ( PdfDocumentProcessor pdfDocumentProcessor , PdfPage page , PdfRectangle pageSize , string text )
211+ {
212+ using ( PdfGraphics graphics = pdfDocumentProcessor . CreateGraphicsWorldSystem ( ) )
213+ {
214+ using ( var textBrush = new DXSolidBrush ( Color . FromArgb ( 255 , Color . DarkOrange ) ) )
215+ {
216+ DXFont font = new DXFont ( "Segoe UI" , 12 , DXFontStyle . Regular ) ;
217+
218+ // Calculate text size
219+ SizeF textSize = graphics . MeasureString (
220+ text ,
221+ font ,
222+ new PdfStringFormat ( ) ) ;
223+
224+ // Calculate an area to draw the text
225+ PointF textPoint = new PointF ( 0 , ( float ) ( pageSize . Height - 10 ) ) ;
226+ RectangleF rectangle = new RectangleF (
227+ 0 , 10 ,
228+ ( float ) pageSize . Width ,
229+ ( float ) ( pageSize . Height / 2 ) ) ;
230+
231+ // Draw text at the calculated area
232+ graphics . DrawString ( text , font , textBrush , rectangle ) ;
233+ graphics . AddToPageForeground ( page ) ;
234+ }
235+ }
134236 }
135237 }
136- }
238+ }
0 commit comments