This tutorial shows how to use the automated annotation features of the LEADTOOLS SDK in a WinForms C# .NET 6 application using the DocumentViewer
control.
Overview | |
---|---|
Summary | This tutorial covers automated annotation features in a C# WinForms Application using the Document Viewer. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (55 KB) |
Platform | WinForms C# Application |
IDE | Visual Studio 2022 |
Development License | Download LEADTOOLS |
Before working on the Draw and Edit Annotations on Documents - WinForms C# tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and the Display Files in the Document Viewer tutorials.
Start with a copy of the project created in the Display Files in the Document Viewer tutorial. If the project is not available, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:
If using NuGet references, this tutorial requires the following NuGet packages and their dependencies:
Leadtools.Annotations.WinForms
Leadtools.Document.Sdk
Leadtools.Document.Viewer.WinForms
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:
Leadtools.Annotations.Automation.dll
Leadtools.Annotations.Engine.dll
Leadtools.Annotations.WinForms.dll
Leadtools.Caching.dll
Leadtools.Controls.WinForms.dll
Leadtools.Core.dll
Leadtools.Document.Pdf.dll
Leadtools.Document.Viewer.WinForms.dll
Leadtools.Document.dll
Leadtools.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to the Setting a Runtime License tutorial.
There are two types of runtime licenses:
Note: Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, the license set, and the Document Viewer code added, coding can begin.
Go to Form1.cs
in the Solution Explorer. Right-click on the Design Window and select View Code or press F7 to bring up the code behind the Form.
Add the following statements to the using
block at the top:
using Leadtools;
using Leadtools.Document;
using Leadtools.Caching;
using Leadtools.Document.Viewer;
using Leadtools.Controls;
using Leadtools.Annotations.Automation;
using Leadtools.Annotations.WinForms;
Add the code below to the InitUI()
method to create the panel that will hold the annotations toolbar:
var annToolbarPanel = new Panel();
annToolbarPanel.Name = "annToolBarPanel";
annToolbarPanel.Width = 200;
annToolbarPanel.Dock = DockStyle.Right;
annToolbarPanel.BackColor = Color.LightBlue;
annToolbarPanel.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(annToolbarPanel);
In the InitDocumentViewer()
method, change createOptions.UseAnnotations
value to true
and add a call to InitAnnotations()
method.
var createOptions = new DocumentViewerCreateOptions();
// Set the UI part where the Document Viewer is displayed
createOptions.ViewContainer = this.Controls.Find("docViewerPanel", false)[0];
// Set the UI part where the Thumbnails are displayed
createOptions.ThumbnailsContainer = this.Controls.Find("thumbPanel", false)[0];
// Enable using annotations
createOptions.UseAnnotations = true;
// Now create the viewer
_documentViewer = DocumentViewerFactory.CreateDocumentViewer(createOptions);
// Initialize Annotations
InitAnnotations();
Use the code below for the InitAnnotations()
method, to initialize the automation manager, create and populate the toolbar, and enable a context menu to show when right-clicking an annotation in the Document Viewer.
private void InitAnnotations()
{
// The annotations toolbar will be added here
var annToolbarPanel = this.Controls.Find("annToolBarPanel", true)[0];
// Get the automation manager from the document viewer
var automationManager = _documentViewer.Annotations.AutomationManager;
// Create the manager helper. This sets the rendering engine
var automationManagerHelper = new AutomationManagerHelper(automationManager);
// Tell the document viewer that automation manager helper is created
_documentViewer.Annotations.Initialize();
// Create the toolbar
automationManagerHelper.ModifyToolBarParentVisiblity = true;
automationManagerHelper.CreateToolBar();
var annToolBar = automationManagerHelper.ToolBar;
annToolBar.Dock = DockStyle.Fill;
annToolBar.AutoSize = true;
annToolBar.LayoutStyle = ToolStripLayoutStyle.Flow;
annToolbarPanel.Controls.Add(annToolBar);
annToolBar.BringToFront();
// Handler for showing the context menu when the user right-clicks on an annotation object
EventHandler<AnnAutomationEventArgs> onShowContextMenu = (sender, e) =>
{
// If not clicked on an annotation object
if (e == null)
return;
// Get the object type
var automationObject = e.Object as AnnAutomationObject;
if (automationObject == null)
return;
// Convert the point to client coordinates
var imageViewer = _documentViewer.View.ImageViewer;
var position = imageViewer.PointToClient(Cursor.Position);
var automation = _documentViewer.Annotations.Automation;
// Show its context menu
var contextMenu = automationObject.ContextMenu as ObjectContextMenu;
if (contextMenu != null)
{
contextMenu.Automation = automation;
contextMenu.Show(imageViewer, position);
}
};
// Handler for showing the object properties dialog
EventHandler<AnnAutomationEventArgs> onShowObjectProperties = (sender, e) =>
{
// Get the automation object from the document viewer
using (var dlg = new AutomationUpdateObjectDialog())
{
dlg.UserName = _documentViewer.UserName;
dlg.Automation = sender as AnnAutomation;
dlg.ShowDialog(this);
e.Cancel = !dlg.IsModified;
}
};
// Handle extra annotations using the Operation event
_documentViewer.Operation += (sender, e) =>
{
switch (e.Operation)
{
case DocumentViewerOperation.LoadingAnnotations:
// Disable the panel containing the toolbar when loading it, then enable it when done
annToolbarPanel.Enabled = e.IsPostOperation;
break;
case DocumentViewerOperation.CreateAutomation:
if (e.IsPostOperation)
{
// Automation object has been created, use it to perform any extra task not handled by the
// document viewer by default
// Handle showing the context menu when the user right-clicks on an object and when they
// select properties
_documentViewer.Annotations.Automation.OnShowContextMenu += onShowContextMenu;
_documentViewer.Annotations.Automation.OnShowObjectProperties += onShowObjectProperties;
}
break;
case DocumentViewerOperation.DestroyAutomation:
if (!e.IsPostOperation)
{
// Automation is about to be destroyed, remove any events we used
_documentViewer.Annotations.Automation.OnShowContextMenu -= onShowContextMenu;
_documentViewer.Annotations.Automation.OnShowObjectProperties -= onShowObjectProperties;
}
break;
default:
break;
}
};
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and any of the annotations on the toolbar can be selected to draw on the loaded document. The following image shows the Document Viewer, with the annotation toolbar on the right of the viewer.
This tutorial showed how to use the AutomationManager
, AutomationManagerHelper
and AnnAutomation
classes with the DocumentViewer
control to draw and edit automated annotations.