Back to Devexpress

IAIDocProcessingService.TranslateAsync(PdfDocumentProcessor, PdfDocumentArea, CultureInfo, CancellationToken) Method

officefileapi-devexpress-dot-aiintegration-dot-docs-dot-iaidocprocessingservice-dot-translateasync-x28-pdfdocumentprocessor-pdfdocumentarea-cultureinfo-cancellationtoken-x29.md

latest8.7 KB
Original Source

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

csharp
Task<string> TranslateAsync(
    PdfDocumentProcessor processor,
    PdfDocumentArea area,
    CultureInfo culture,
    CancellationToken cancellationToken = default(CancellationToken)
)
vb
Function TranslateAsync(
    processor As PdfDocumentProcessor,
    area As PdfDocumentArea,
    culture As CultureInfo,
    cancellationToken As CancellationToken = Nothing
) As Task(Of String)

Parameters

NameTypeDescription
processorPdfDocumentProcessor

The PdfDocumentProcessor instance which content should be translated.

| | area | PdfDocumentArea |

The document area that contains target content.

| | culture | CultureInfo |

An object that specifies culture settings applied during translation.

|

Optional Parameters

NameTypeDefaultDescription
cancellationTokenCancellationTokennull

The token that cancels the task.

|

Returns

TypeDescription
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:

csharp
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);
        }
    }
}
vb
Imports DevExpress.AIIntegration
Imports DevExpress.AIIntegration.Docs
Imports DevExpress.Drawing
Imports DevExpress.Pdf
Imports Microsoft.Extensions.AI
Imports System.Drawing
Imports System.Globalization

' Check "Register Service" section for implementation code
var docProcessingService = 
    defaultAIExtensionsContainer.CreateAIDocProcessingService()

Using pdfDocumentProcessor As New PdfDocumentProcessor()
    pdfDocumentProcessor.LoadDocument("Documents/FirstLookExported.pdf")

    ' Obtain the first page area
    Dim pageBox = pdfDocumentProcessor.Document.Pages(0).CropBox
    Dim pagePosition1 As New PdfDocumentPosition(1, pageBox.TopLeft)
    Dim pagePosition2 As New PdfDocumentPosition(1, pageBox.BottomRight)
    Dim pageContentArea = PdfDocumentArea.Create(pagePosition1, pagePosition2)

    ' Translate the page content to Spanish
    Dim translation As String = Await docProcessingService.TranslateAsync(
        pdfDocumentProcessor, 
        pageContentArea, 
        New CultureInfo("es-ES"))

    ' Insert a new page and add the translated text
    Dim page As PdfPage = pdfDocumentProcessor.InsertNewPage(1, PdfPaperSize.Letter)
    Dim pageSize As PdfRectangle = page.CropBox
    AddContentToPage(pdfDocumentProcessor, page, pageSize, translation)

    ' Save the modified document
    pdfDocumentProcessor.SaveDocument("result.pdf")
End Using

' This method draws text on the inserted page
Private Sub AddContentToPage(
    pdfDocumentProcessor As PdfDocumentProcessor, 
    page As PdfPage,
    pageSize As PdfRectangle, 
    text As String)

    Using graphics As PdfGraphics = pdfDocumentProcessor.CreateGraphicsWorldSystem()
        Using textBrush As New DXSolidBrush(Color.FromArgb(255, Color.DarkOrange))
            Dim font As New DXFont("Segoe UI", 12, DXFontStyle.Regular)

            ' Calculate text size
            Dim textSize As SizeF = graphics.MeasureString(
                text, 
                font, 
                New PdfStringFormat())

            ' Calculate an area to draw the text
            Dim textPoint As New PointF(0, CSng(pageSize.Height - 10))
            Dim rectangle As New RectangleF(
                0, 
                10, 
                CSng(pageSize.Width), 
                CSng(pageSize.Height / 2))

            ' Draw text at the calculated area
            graphics.DrawString(text, font, textBrush, rectangle)
            graphics.AddToPageForeground(page)
        End Using
    End Using
End Sub

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.

office-file-api-ai-implementation/CS/Controllers/TranslateController.cs#L123

csharp
var pageArea = PdfDocumentArea.Create(pagePosition1, pagePosition2);
translation = await docProcessingService.TranslateAsync(pdfDocumentProcessor, pageArea, language);
break;

See Also

IAIDocProcessingService Interface

IAIDocProcessingService Members

DevExpress.AIIntegration.Docs Namespace