using Leadtools;
using Leadtools.Controls;
using Leadtools.Document;
using Leadtools.Document.Viewer;
using Leadtools.Document.Converter;
using Leadtools.Codecs;
using Leadtools.Caching;
using Leadtools.Annotations.Engine;
using Leadtools.Annotations.Automation;
using Leadtools.Annotations.WinForms;
using Leadtools.Annotations.Designers;
using Leadtools.Document.Writer;
using Leadtools.Ocr;
public void DocumentViewerWithAnnotations_Example()
{
// New Form to be used as our application
var form = new MyForm();
form.ShowDialog();
}
class MyForm : Form
{
public MyForm()
{
this.Size = new Size(800, 800);
this.Text = "LEADTOOLS Document Viewer Example";
}
protected override void OnLoad(EventArgs e)
{
if (!DesignMode)
Init();
base.OnLoad(e);
}
// Our document viewer instance
private DocumentViewer _documentViewer;
private void Init()
{
// Initialize the user interface
InitUI();
// Init the cache. This is optional, but it can speed up viewing of large documents
// if not needed, remove this section
DocumentFactory.Cache = new FileCache();
// Init the document viewer, pass along the panels
var createOptions = new DocumentViewerCreateOptions
{
// The middle panel for the view
ViewContainer = this.Controls.Find("middlePanel", false)[0],
// The left panel for the thumbnails
ThumbnailsContainer = this.Controls.Find("leftPanel", false)[0],
// Using annotations in this example
UseAnnotations = true
};
// Create the document viewer
_documentViewer = DocumentViewerFactory.CreateDocumentViewer(createOptions);
// We prefer SVG viewing
_documentViewer.View.PreferredItemType = DocumentViewerItemType.Svg;
// Initalize the annotations
InitAnnotations();
// Load a document
var fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Leadtools.pdf";
var document = DocumentFactory.LoadFromFile(
fileName,
new LoadDocumentOptions { UseCache = DocumentFactory.Cache != null });
// Set it in the viewer
_documentViewer.SetDocument(document);
// Run pan/zoom
var interactiveComboBox = this.Controls.Find("interactiveComboBox", true)[0] as ComboBox;
interactiveComboBox.SelectedItem = DocumentViewerCommands.InteractivePanZoom;
}
private void InitAnnotations()
{
// The annotations toolbar will be added here
var rightPanel = this.Controls.Find("rightPanel", 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();
// Craete the toolbar
automationManagerHelper.ModifyToolBarParentVisiblity = true;
automationManagerHelper.CreateToolBar();
var toolBar = automationManagerHelper.ToolBar;
toolBar.Dock = DockStyle.Fill;
toolBar.AutoSize = true;
toolBar.BorderStyle = BorderStyle.None;
toolBar.Appearance = ToolBarAppearance.Flat;
rightPanel.Controls.Add(toolBar);
toolBar.BringToFront();
// Handler for showing the context menu when the user right clicks on an annotation object
EventHandler<AnnAutomationEventArgs> onShowContextMenu = (sender, e) =>
{
// 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 show 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 where we put the toolbar when we are loading, enable when we are done
rightPanel.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
// We will 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;
}
};
}
private void InitUI()
{
// Add a panel on the left for the document viewer thumbnails part
var leftPanel = new Panel();
leftPanel.Name = "leftPanel";
leftPanel.Width = 200;
leftPanel.Dock = DockStyle.Left;
leftPanel.BackColor = Color.Gray;
leftPanel.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(leftPanel);
// Add a panel to the right, this will work for the bookmarks or annotations part
var rightPanel = new Panel();
rightPanel.Name = "rightPanel";
rightPanel.Width = 200;
rightPanel.Dock = DockStyle.Right;
rightPanel.BackColor = Color.LightBlue;
rightPanel.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(rightPanel);
// Add a panel to fill the rest, for the document viewer
var middlePanel = new Panel();
middlePanel.Name = "middlePanel";
middlePanel.BackColor = Color.DarkGray;
middlePanel.BorderStyle = BorderStyle.None;
middlePanel.Dock = DockStyle.Fill;
this.Controls.Add(middlePanel);
// Add a top panel to host our application controls
var topPanel = new Panel();
topPanel.Name = "topPanel";
topPanel.Height = 100;
topPanel.Dock = DockStyle.Top;
topPanel.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(topPanel);
middlePanel.BringToFront();
// Add buttons for the UI
// Combo box for interactive modes
var interactiveComboBox = new ComboBox();
interactiveComboBox.Name = "interactiveComboBox";
interactiveComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
// The command names for the items so we can just run them
interactiveComboBox.Items.Add(DocumentViewerCommands.InteractivePanZoom);
interactiveComboBox.Items.Add(DocumentViewerCommands.InteractiveSelectText);
interactiveComboBox.SelectedIndexChanged += (sender, e) =>
{
var commandName = interactiveComboBox.SelectedItem as string;
_documentViewer.Commands.Run(commandName);
};
topPanel.Controls.Add(interactiveComboBox);
// Generic label for information used by the examples
var infoLabel = new Label();
infoLabel.Name = "infoLabel";
infoLabel.Text = "Info...";
infoLabel.AutoSize = false;
infoLabel.Width = 400;
infoLabel.Left = interactiveComboBox.Right + 20;
topPanel.Controls.Add(infoLabel);
var exampleButton = new Button();
exampleButton.Top = interactiveComboBox.Bottom + 2;
exampleButton.Name = "exampleButton";
exampleButton.Text = "&Example";
exampleButton.Click += (sender, e) => Example(exampleButton);
topPanel.Controls.Add(exampleButton);
}
private void Example(Button exampleButton)
{
// Add the example code here
MessageBox.Show(this, "Ready");
}
Imports Leadtools
Imports Leadtools.Controls
Imports Leadtools.Document
Imports Leadtools.Document.Viewer
Imports Leadtools.Document.Converter
Imports Leadtools.Codecs
Imports Leadtools.Caching
Imports Leadtools.Annotations.Engine
Imports Leadtools.Annotations.Automation
Imports Leadtools.Annotations.WinForms
Imports Leadtools.Annotations.Designers
Imports Leadtools.Document.Writer
Imports Leadtools.Ocr
Public Sub DocumentViewerWithAnnotations_Example()
' New Form to be used as our application
Dim form As New MyForm()
form.ShowDialog()
End Sub
Class MyForm
Inherits Form
Public Sub New()
Me.Size = New Size(800, 800)
Me.Text = "LEADTOOLS Document Viewer Example"
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
If Not DesignMode Then
Init()
End If
MyBase.OnLoad(e)
End Sub
' Our document viewer instance
Private _documentViewer As DocumentViewer
Private Sub Init()
' Initialize the user interface
InitUI()
' Init the cache. This is optional, but it can speed up viewing of large documents
' if not needed, remove this section
DocumentFactory.Cache = New FileCache()
' Init the document viewer, pass along the panels
Dim createOptions As New DocumentViewerCreateOptions()
' The middle panel for the view
createOptions.ViewContainer = Me.Controls.Find("middlePanel", False)(0)
' The left panel for the thumbnails
createOptions.ThumbnailsContainer = Me.Controls.Find("leftPanel", False)(0)
' Using annotations in this example
createOptions.UseAnnotations = True
' Create the document viewer
_documentViewer = DocumentViewerFactory.CreateDocumentViewer(createOptions)
' We prefer SVG viewing
_documentViewer.View.PreferredItemType = DocumentViewerItemType.Svg
' Initialize the annotations
InitAnnotations()
' Load a document
Dim fileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Leadtools.pdf"
Dim loadOptoins As New LoadDocumentOptions()
loadOptoins.UseCache = Not IsNothing(DocumentFactory.Cache)
Dim Document As LEADDocument = DocumentFactory.LoadFromFile(fileName, loadOptoins)
' Set it in the viewer
_documentViewer.SetDocument(Document)
' Run pan/zoom
Dim interactiveComboBox As ComboBox = CType(Me.Controls.Find("interactiveComboBox", True)(0), ComboBox)
interactiveComboBox.SelectedItem = DocumentViewerCommands.InteractivePanZoom
End Sub
Private Sub InitAnnotations()
' The annotations toolbar will be added here
Dim rightPanel As Control = Me.Controls.Find("rightPanel", True)(0)
' Get the automation manager from the document viewer
Dim automationManager As AnnAutomationManager = _documentViewer.Annotations.AutomationManager
' Create the manager helper. This sets the rendering engine
Dim automationManagerHelper As New AutomationManagerHelper(automationManager)
' Tell the document viewer that automation manager helper is created
_documentViewer.Annotations.Initialize()
' Create the toolbar
automationManagerHelper.ModifyToolBarParentVisiblity = True
automationManagerHelper.CreateToolBar()
Dim toolBar As ToolBar = automationManagerHelper.ToolBar
toolBar.Dock = DockStyle.Fill
toolBar.AutoSize = True
toolBar.BorderStyle = BorderStyle.None
toolBar.Appearance = ToolBarAppearance.Flat
rightPanel.Controls.Add(toolBar)
toolBar.BringToFront()
' Handler for showing the context menu when the user right clicks on an annotation object
Dim onShowContextMenu As EventHandler(Of AnnAutomationEventArgs) =
Sub(sender, e)
' Get the object type
Dim automationObject As AnnAutomationObject = e.Object
If automationObject Is Nothing Then Return
' Convert the point to client coordinates
Dim imageViewer As ImageViewer = _documentViewer.View.ImageViewer
Dim position As Point = imageViewer.PointToClient(Cursor.Position)
Dim automation As AnnAutomation = _documentViewer.Annotations.Automation
' Show its context menu
Dim contextMenu As ObjectContextMenu = CType(automationObject.ContextMenu, ObjectContextMenu)
If Not IsNothing(contextMenu) Then
contextMenu.Automation = automation
contextMenu.Show(imageViewer, position)
End If
End Sub
' Handler for show the object properties dialog
Dim onShowObjectProperties As EventHandler(Of AnnAutomationEventArgs) =
Sub(sender, e)
' Get the automation object from the document viewer
Using dlg As New AutomationUpdateObjectDialog()
dlg.UserName = _documentViewer.UserName
dlg.Automation = CType(sender, AnnAutomation)
dlg.ShowDialog(Me)
e.Cancel = Not dlg.IsModified
End Using
End Sub
' Handle extra annotations using the Operation event
AddHandler _documentViewer.Operation,
Sub(sender, e)
Select Case e.Operation
Case DocumentViewerOperation.LoadingAnnotations
' Disable the panel where we put the toolbar when we are loading, enable when we are done
rightPanel.Enabled = e.IsPostOperation
Case DocumentViewerOperation.CreateAutomation
If e.IsPostOperation Then
' Automation object has been created, use it to perform any extra task not handled by the
' document viewer by default
' We will handle showing the context menu when the user right clicks on an object and when they
' select properties
AddHandler _documentViewer.Annotations.Automation.OnShowContextMenu, onShowContextMenu
AddHandler _documentViewer.Annotations.Automation.OnShowObjectProperties, onShowObjectProperties
End If
Case DocumentViewerOperation.DestroyAutomation
If Not e.IsPostOperation Then
' Automation is about to be destroyed, remove any events we used
RemoveHandler _documentViewer.Annotations.Automation.OnShowContextMenu, onShowContextMenu
RemoveHandler _documentViewer.Annotations.Automation.OnShowObjectProperties, onShowObjectProperties
End If
End Select
End Sub
End Sub
Private Sub InitUI()
' Add a panel on the left for the document viewer thumbnails part
Dim leftPanel As New Panel()
leftPanel.Name = "leftPanel"
leftPanel.Width = 200
leftPanel.Dock = DockStyle.Left
leftPanel.BackColor = Color.Gray
leftPanel.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(leftPanel)
' Add a panel to the right, this will work for the bookmarks or annotations part
Dim rightPanel As New Panel()
rightPanel.Name = "rightPanel"
rightPanel.Width = 200
rightPanel.Dock = DockStyle.Right
rightPanel.BackColor = Color.LightBlue
rightPanel.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(rightPanel)
' Add a panel to fill the rest, for the document viewer
Dim middlePanel As New Panel()
middlePanel.Name = "middlePanel"
middlePanel.BackColor = Color.DarkGray
middlePanel.BorderStyle = BorderStyle.None
middlePanel.Dock = DockStyle.Fill
Me.Controls.Add(middlePanel)
' Add a top panel to host our application controls
Dim topPanel As New Panel()
topPanel.Name = "topPanel"
topPanel.Height = 100
topPanel.Dock = DockStyle.Top
topPanel.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(topPanel)
middlePanel.BringToFront()
' Add buttons for the UI
' Combo box for interactive modes
Dim interactiveComboBox As New ComboBox()
interactiveComboBox.Name = "interactiveComboBox"
interactiveComboBox.DropDownStyle = ComboBoxStyle.DropDownList
' The command names for the items so we can just run them
interactiveComboBox.Items.Add(DocumentViewerCommands.InteractivePanZoom)
interactiveComboBox.Items.Add(DocumentViewerCommands.InteractiveSelectText)
AddHandler interactiveComboBox.SelectedIndexChanged,
Sub(sender, e)
Dim commandName As String = CType(interactiveComboBox.SelectedItem, String)
_documentViewer.Commands.Run(commandName)
End Sub
topPanel.Controls.Add(interactiveComboBox)
' Generic label for information used by the examples
Dim infoLabel As New Label()
infoLabel.Name = "infoLabel"
infoLabel.Text = "Info..."
infoLabel.AutoSize = False
infoLabel.Width = 400
infoLabel.Left = interactiveComboBox.Right + 20
topPanel.Controls.Add(infoLabel)
Dim exampleButton As New Button()
exampleButton.Top = interactiveComboBox.Bottom + 2
exampleButton.Name = "exampleButton"
exampleButton.Text = "&Example"
AddHandler exampleButton.Click, Sub(sender, e) Example(exampleButton)
topPanel.Controls.Add(exampleButton)
End Sub
Private Sub Example(exampleButton As Button)
' Add the example code here
MessageBox.Show(Me, "Ready")
End Sub