1
1
using DevExpress . Office . Utils ;
2
2
using DevExpress . Spreadsheet ;
3
+ using DevExpress . XtraPrinting . Native ;
3
4
using DevExpress . XtraRichEdit ;
5
+ using DevExpress . XtraRichEdit . API . Native ;
4
6
using Microsoft . AspNetCore . Mvc ;
5
7
using RichEditOpenAIWebApi . BusinessObjects ;
6
8
using Swashbuckle . AspNetCore . Annotations ;
9
+ using System . Globalization ;
7
10
using System . Net ;
8
11
9
12
namespace RichEditOpenAIWebApi . Controllers
@@ -12,6 +15,7 @@ namespace RichEditOpenAIWebApi.Controllers
12
15
[ Route ( "[controller]/[action]" ) ]
13
16
public class OpenAIController : ControllerBase
14
17
{
18
+ // Insert your OpenAI key
15
19
string openAIApiKey = "" ;
16
20
[ HttpPost ]
17
21
[ SwaggerResponse ( ( int ) HttpStatusCode . OK , "Download a file" , typeof ( FileContentResult ) ) ]
@@ -20,11 +24,11 @@ public async Task<IActionResult> GenerateImageAltText(IFormFile documentWithImag
20
24
try
21
25
{
22
26
var imageHelper = new OpenAIClientImageHelper ( openAIApiKey ) ;
23
- using ( var server = new RichEditDocumentServer ( ) )
27
+ using ( var wordProcessor = new RichEditDocumentServer ( ) )
24
28
{
25
- await RichEditHelper . LoadFile ( server , documentWithImage ) ;
29
+ await RichEditHelper . LoadFile ( wordProcessor , documentWithImage ) ;
26
30
27
- server . GenerateAltTextForImages ( ( document ) =>
31
+ wordProcessor . IterateSubDocuments ( ( document ) =>
28
32
{
29
33
foreach ( var shape in document . Shapes )
30
34
{
@@ -33,7 +37,7 @@ public async Task<IActionResult> GenerateImageAltText(IFormFile documentWithImag
33
37
}
34
38
} ) ;
35
39
36
- Stream result = RichEditHelper . SaveDocument ( server , outputFormat ) ;
40
+ Stream result = RichEditHelper . SaveDocument ( wordProcessor , outputFormat ) ;
37
41
string contentType = RichEditHelper . GetContentType ( outputFormat ) ;
38
42
string outputStringFormat = outputFormat . ToString ( ) . ToLower ( ) ;
39
43
return File ( result , contentType , $ "result.{ outputStringFormat } ") ;
@@ -61,7 +65,7 @@ public async Task<IActionResult> GenerateChartAltText(IFormFile documentWithImag
61
65
foreach ( var chart in worksheet . Charts )
62
66
{
63
67
OfficeImage image = chart . ExportToImage ( ) ;
64
- chart . AlternativeText = await imageHelper . DescribeImageAsync ( image ) ;
68
+ chart . AlternativeText = imageHelper . DescribeImageAsync ( image ) . Result ;
65
69
}
66
70
}
67
71
@@ -75,7 +79,69 @@ public async Task<IActionResult> GenerateChartAltText(IFormFile documentWithImag
75
79
{
76
80
return StatusCode ( 500 , e . Message + Environment . NewLine + e . StackTrace ) ;
77
81
}
82
+ }
83
+ [ HttpPost ]
84
+ [ SwaggerResponse ( ( int ) HttpStatusCode . OK , "Download a file" , typeof ( FileContentResult ) ) ]
85
+ public async Task < IActionResult > GenerateHyperlinkDescriptionForWord ( IFormFile documentWithHyperlinks , [ FromQuery ] RichEditFormat outputFormat )
86
+ {
87
+ try
88
+ {
89
+ var hyperlinkHelper = new OpenAIClientHyperlinkHelper ( openAIApiKey ) ;
90
+ using ( var wordProcessor = new RichEditDocumentServer ( ) )
91
+ {
92
+ await RichEditHelper . LoadFile ( wordProcessor , documentWithHyperlinks ) ;
78
93
94
+ wordProcessor . IterateSubDocuments ( async ( document ) =>
95
+ {
96
+ foreach ( var hyperlink in document . Hyperlinks )
97
+ {
98
+ if ( string . IsNullOrEmpty ( hyperlink . ToolTip ) || hyperlink . ToolTip == hyperlink . NavigateUri )
99
+ {
100
+ hyperlink . ToolTip = hyperlinkHelper . DescribeHyperlinkAsync ( hyperlink . NavigateUri ) . Result ;
101
+ }
102
+ }
103
+ } ) ;
104
+ Stream result = RichEditHelper . SaveDocument ( wordProcessor , outputFormat ) ;
105
+ string contentType = RichEditHelper . GetContentType ( outputFormat ) ;
106
+ string outputStringFormat = outputFormat . ToString ( ) . ToLower ( ) ;
107
+ return File ( result , contentType , $ "result.{ outputStringFormat } ") ;
108
+ }
109
+ }
110
+ catch ( Exception e )
111
+ {
112
+ return StatusCode ( 500 , e . Message + Environment . NewLine + e . StackTrace ) ;
113
+ }
79
114
}
115
+ [ HttpPost ]
116
+ [ SwaggerResponse ( ( int ) HttpStatusCode . OK , "Download a file" , typeof ( FileContentResult ) ) ]
117
+ public async Task < IActionResult > GenerateHyperlinkDescriptionForSpreadsheet ( IFormFile documentWithHyperlinks , [ FromQuery ] SpreadsheetFormat outputFormat )
118
+ {
119
+ try
120
+ {
121
+ var hyperlinkHelper = new OpenAIClientHyperlinkHelper ( openAIApiKey ) ;
122
+ using ( var workbook = new Workbook ( ) )
123
+ {
124
+ await SpreadsheetHelper . LoadWorkbook ( workbook , documentWithHyperlinks ) ;
125
+
126
+ foreach ( var worksheet in workbook . Worksheets )
127
+ {
128
+ foreach ( var hyperlink in worksheet . Hyperlinks )
129
+ {
130
+ if ( hyperlink . IsExternal && ( string . IsNullOrEmpty ( hyperlink . TooltipText ) || hyperlink . TooltipText == hyperlink . Uri ) )
131
+ hyperlink . TooltipText = hyperlinkHelper . DescribeHyperlinkAsync ( hyperlink . Uri ) . Result ;
132
+ }
133
+ }
134
+
135
+ Stream result = SpreadsheetHelper . SaveDocument ( workbook , outputFormat ) ;
136
+ string contentType = SpreadsheetHelper . GetContentType ( outputFormat ) ;
137
+ string outputStringFormat = outputFormat . ToString ( ) . ToLower ( ) ;
138
+ return File ( result , contentType , $ "result.{ outputStringFormat } ") ;
139
+ }
140
+ }
141
+ catch ( Exception e )
142
+ {
143
+ return StatusCode ( 500 , e . Message + Environment . NewLine + e . StackTrace ) ;
144
+ }
145
+ }
80
146
}
81
147
}
0 commit comments