xtrareports-405450-ai-powered-functionality-desktop-reporting-document-translate-inline-win-viewer.md
This help topic describes how to integrate the AI-powered Translate Inline command into the WinForms Document Viewer.
Unlike the Translate command, Translate Inline translates text directly on the document preview without additional dialogs. You can export or print the translated document.
Install the following NuGet packages:
Microsoft.Extensions.AI.OpenAI (Version=”9.7.1-preview.1.25365.4”)
DevExpress.AIIntegration.WinForms
Optional:
DevExpress.Win.Design (for .NET projects)The following code snippet registers an Azure OpenAI client at application startup:
using Azure.AI.OpenAI;
using DevExpress.AIIntegration;
using Microsoft.Extensions.AI;
using System.ClientModel;
internal static class Program {
static string AzureOpenAIEndpoint { get { return "AZURE_OPENAI_ENDPOINT"; } }
static string AzureOpenAIKey { get { return "AZURE_OPENAI_APIKEY"; } }
static string DeploymentName { get { return "MODEL_NAME"; } } // For example, gpt-4.1.
[STAThread]
static void Main(){
IChatClient client = new AzureOpenAIClient(
new Uri(AzureOpenAIEndpoint),
new ApiKeyCredential(AzureOpenAIKey))
.GetChatClient(DeploymentName).AsIChatClient();
AIExtensionsContainerDesktop.Default.RegisterChatClient(client);
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
Imports Azure.AI.OpenAI
Imports DevExpress.AIIntegration
Imports Microsoft.Extensions.AI
Imports System.ClientModel
Friend Module Program
Private ReadOnly Property AzureOpenAIEndpoint() As String
Get
Return "AZURE_OPENAI_ENDPOINT"
End Get
End Property
Private ReadOnly Property AzureOpenAIKey() As String
Get
Return "AZURE_OPENAI_APIKEY"
End Get
End Property
Private ReadOnly Property DeploymentName() As String
Get
Return "MODEL_NAME"
End Get
End Property
<STAThread>
Sub Main()
Dim client As IChatClient = (New AzureOpenAIClient(New Uri(AzureOpenAIEndpoint), New ApiKeyCredential(AzureOpenAIKey))).GetChatClient(DeploymentName).AsIChatClient()
AIExtensionsContainerDesktop.Default.RegisterChatClient(client)
ApplicationConfiguration.Initialize()
Application.Run(New Form1())
End Sub
End Module
Tip
How to Register OpenAI, Azure OpenAI, Ollama, and Semantic Kernel Clients.
Drop the DocumentViewer component from the Toolbox onto a Form.
Click the smart tag and select Create Ribbon Toolbar or Create Standard Toolbar. Both these actions generate the context menu where users can access the Translate Inline functionality.
Specify the Document Source option. Use an object that supplies a document to the Document Viewer.
Drop the BehaviorManager component from the Toolbox onto a Form.
(For .NET Framework Projects) Use the Register AI-Powered Behaviors option in the BehaviorManager’s smart tag menu to add the “AI-Powered Behaviors” submenu with report behaviors to BehaviorManager.
Use the Edit Behaviors option in the BehaviorManager’s smart tag menu to access the behavior collection.
Add the Translate Inline Behavior and configure its settings:
The following code registers a DocumentTranslateInlineBehavior and attaches it to the WinForms Document Viewer control:
using DevExpress.AIIntegration;
using DevExpress.AIIntegration.WinForms;
// ...
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
behaviorManager1.Attach<DocumentTranslateInlineBehavior>(documentViewer1, behavior => {
behavior.Properties.Languages = new LanguageInfo[] {
new LanguageInfo("de-DE")
};
});
}
}
Imports DevExpress.AIIntegration
Imports DevExpress.AIIntegration.WinForms
' ...
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
behaviorManager1.Attach(Of DocumentTranslateInlineBehavior)(documentViewer1, Sub(behavior)
behavior.Properties.Languages = New LanguageInfo() {
New LanguageInfo("de-DE")
}
End Sub)
End Sub
End Class
Note
Call the BehaviorInitializer.Initialize() method at application startup if your project targets the .NET Framework and you create AI-powered behaviors in code. Otherwise, an exception is thrown.
internal static class Program {
[STAThread]
static void Main() {
//...
// The Initialize() method forcibly initializes the behavior manager in .NET Framework apps.
DevExpress.AIIntegration.WinForms.Reporting.BehaviorInitializer.Initialize();
Application.Run(new Form1());
}
}
You can access the Translate Inline command in the AI context menu of WinForms Document Viewer.
Right-click on an opened report or selected report content. Select Translate Inline from the AI Assistant submenu. Select input text range (selected content, document page, or entire document) and the target language.
The “AI-Generated Translation” label appears at the top-left corner once the translation is completed:
Click Revert to Original in the AI Assistant submenu to cancel translation:
The following message appears when you export a document with an AI-generated translation:
“You are about to export a document that contains AI-generated translations. Do you want to proceed?”