Translate Reports Inline in the WinForms Document Viewer
- 4 minutes to read
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.
Activate AI Assistant
Install NuGet Packages
Install the following NuGet packages:
- Microsoft.Extensions.AI (Version=”9.5.0”)
Azure.AI.OpenAI (Version=”2.2.0-beta.4”)
This tutorial uses Azure OpenAI. Refer to the AI-powered Extensions help topic for information about NuGet packages required for other supported AI services.
DevExpress.AIIntegration.WinForms
Optional:
DevExpress.Win.Design
(for .NET projects)
Register AI Client
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());
}
}
Note
Review the following help topic for information on how to register other supported AI services: How to Register an AI Client.
Add a Document Viewer
- 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.
Create and Configure AI Assistant Behaviors
- 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 settings are available:
Languages
Specifies target languages/cultures for text translation.
Temperature
Controls the randomness of the output. Lower temperatures (values between 0 and 0.5) yield more predictable and focused outputs, while higher temperatures (values between 0.5 and 1) produce more diverse and creative responses.
Target
Attaches behaviors to DevExpress controls.
Create and Configure AI Assistant Behavior at Runtime
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")
};
});
}
}
Translate a Report Document
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?”