-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTranslateController.cs
More file actions
152 lines (131 loc) · 6.86 KB
/
TranslateController.cs
File metadata and controls
152 lines (131 loc) · 6.86 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
using DevExpress.AIIntegration.Docs;
using DevExpress.Pdf;
using DevExpress.XtraRichEdit;
using Microsoft.AspNetCore.Mvc;
using RichEditOpenAIWebApi.BusinessObjects;
using Swashbuckle.AspNetCore.Annotations;
using System.Net;
namespace RichEditOpenAIWebApi.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class TranslateController : ControllerBase
{
private readonly IAIDocProcessingService docProcessingService;
public TranslateController(IAIDocProcessingService docService)
{
docProcessingService = docService;
}
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
public async Task<IActionResult> TranslatePresentation(IFormFile file, [FromQuery] TranslationLang translationLanguage, [FromQuery] PresentationFormat outputFormat, PresentationPart part)
{
try
{
var presentation = await PresentationHelper.LoadPresentation(file);
var language = SharedHelper.GetTranslationLanguage(translationLanguage);
switch (part)
{
case PresentationPart.FirstSlide:
var slide = presentation.Slides[0];
await docProcessingService.TranslateAsync(slide, language);
break;
case PresentationPart.WholePresentation:
await docProcessingService.TranslateAsync(presentation, language);
break;
default:
break;
}
Stream result = PresentationHelper.SaveDocument(presentation, outputFormat);
string contentType = PresentationHelper.GetContentType(outputFormat);
string outputStringFormat = outputFormat.ToString().ToLower();
return File(result, contentType, $"result.{outputStringFormat}");
}
catch (Exception e)
{
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
}
}
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
public async Task<IActionResult> TranslateWordDocument(IFormFile document, [FromQuery] TranslationLang translationLanguage, [FromQuery] RichEditFormat outputFormat, RichEditDocumentPart part)
{
try
{
using (var wordProcessor = new RichEditDocumentServer())
{
await RichEditHelper.LoadFile(wordProcessor, document);
var language = SharedHelper.GetTranslationLanguage(translationLanguage);
switch (part)
{
case RichEditDocumentPart.FirstPage:
var fixedRange = wordProcessor.DocumentLayout.GetPage(0).MainContentRange;
var pageRange = wordProcessor.Document.CreateRange(fixedRange.Start, fixedRange.Length); // Translate the first page
await docProcessingService.TranslateAsync(pageRange, language);
break;
case RichEditDocumentPart.FirstSection:
var sectionRange = wordProcessor.Document.Sections[0].Range;
await docProcessingService.TranslateAsync(sectionRange, language);
break;
case RichEditDocumentPart.WholeDocument:
await docProcessingService.TranslateAsync(wordProcessor, language);
break;
default:
break;
}
await docProcessingService.TranslateAsync(wordProcessor, language);
Stream result = RichEditHelper.SaveDocument(wordProcessor, outputFormat);
string contentType = RichEditHelper.GetContentType(outputFormat);
string outputStringFormat = outputFormat.ToString().ToLower();
return File(result, contentType, $"result.{outputStringFormat}");
}
}
catch (Exception e)
{
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
}
}
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
public async Task<IActionResult> TranslatePdfDocument(IFormFile document, [FromQuery] TranslationLang translationLanguage, [FromQuery] PdfPart part)
{
try
{
using (var pdfDocumentProcessor = new PdfDocumentProcessor())
{
await PdfHelper.LoadPdfDocument(pdfDocumentProcessor, document);
var language = SharedHelper.GetTranslationLanguage(translationLanguage);
string translation = string.Empty; // Initialize to avoid CS0165
switch (part)
{
case PdfPart.FirstPage:
var firstPageBox = pdfDocumentProcessor.Document.Pages[0].CropBox;
PdfDocumentPosition pagePosition1 = new PdfDocumentPosition(1, firstPageBox.TopLeft);
PdfDocumentPosition pagePosition2 = new PdfDocumentPosition(1, firstPageBox.BottomRight);
var pageArea = PdfDocumentArea.Create(pagePosition1, pagePosition2);
translation = await docProcessingService.TranslateAsync(pdfDocumentProcessor, pageArea, language);
break;
case PdfPart.WholeDocument:
translation = await docProcessingService.TranslateAsync(pdfDocumentProcessor, language);
break;
default:
translation = string.Empty; // Ensure translation is assigned for all cases
break;
}
// Insert a new page and add the translated text
PdfPage page = pdfDocumentProcessor.InsertNewPage(1, PdfPaperSize.Letter);
PdfRectangle pageSize = page.CropBox;
PdfHelper.AddContentToPage(pdfDocumentProcessor, page, pageSize, translation);
MemoryStream result = new MemoryStream();
pdfDocumentProcessor.SaveDocument(result);
result.Seek(0, SeekOrigin.Begin);
return File(result, "application/pdf", "result.pdf");
}
}
catch (Exception e)
{
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
}
}
}
}