IAIDocProcessingService.TranslateAsync(PdfDocumentProcessor, PdfDocumentArea, CultureInfo, CancellationToken) Method
Translates text into a specific language with AI-powered translation services.
Namespace: DevExpress.AIIntegration.Docs
Assembly: DevExpress.AIIntegration.Docs.v25.2.dll
NuGet Package: DevExpress.AIIntegration.Docs
Declaration
Task<string> TranslateAsync(
PdfDocumentProcessor processor,
PdfDocumentArea area,
CultureInfo culture,
CancellationToken cancellationToken = default(CancellationToken)
)
Parameters
| Name | Type | Description |
|---|---|---|
| processor | PdfDocumentProcessor | The |
| area | PdfDocumentArea | The document area that contains target content. |
| culture | CultureInfo | An object that specifies culture settings applied during translation. |
Optional Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| cancellationToken | CancellationToken | null | The token that cancels the task. |
Returns
| Type | Description |
|---|---|
| Task<String> | The response that contains AI-generated translation.’ |
Example
How to: Translate the First PDF Page
The following code snippet translates the first page in a PDF document and appends the translation as a new page:
using DevExpress.AIIntegration;
using DevExpress.AIIntegration.Docs;
using DevExpress.Drawing;
using DevExpress.Pdf;
using Microsoft.Extensions.AI;
using System.Drawing;
// Check "Register Service" section for implementation code
var docProcessingService =
defaultAIExtensionsContainer.CreateAIDocProcessingService();
using var pdfDocumentProcessor = new PdfDocumentProcessor();
pdfDocumentProcessor.LoadDocument("Documents/FirstLookExported.pdf");
// Obtain the first page area
var pageBox = pdfDocumentProcessor.Document.Pages[0].CropBox;
PdfDocumentPosition pagePosition1 = new PdfDocumentPosition(1, pageBox.TopLeft);
PdfDocumentPosition pagePosition2 = new PdfDocumentPosition(1, pageBox.BottomRight);
var pageContentArea = PdfDocumentArea.Create(pagePosition1, pagePosition2);
// Translate the page content to Spanish
string translation = await docProcessingService.TranslateAsync(
pdfDocumentProcessor,
pageContentArea,
new System.Globalization.CultureInfo("es-ES"));
// Insert a new page and add the translated text
PdfPage page = pdfDocumentProcessor.InsertNewPage(1, PdfPaperSize.Letter);
PdfRectangle pageSize = page.CropBox;
AddContentToPage(pdfDocumentProcessor, page, pageSize, translation);
// Save the modified document
pdfDocumentProcessor.SaveDocument("result.pdf");
// This method draws text on the inserted page
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);
}
}
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the TranslateAsync(PdfDocumentProcessor, PdfDocumentArea, CultureInfo, CancellationToken) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.