Manages the bookmarks and table of contents in this document viewer.
public class DocumentViewerBookmarks : IDisposable
DocumentViewerBookmarks can be accessed by the Bookmarks property.
This class manages the bookmarks of the current LEADDocument set in the document viewer.
Document bookmarks and table of content is parsed from the LEADDocument set the in the viewer using the DocumentStructure class. Each is a DocumentBookmark object that specifies the properties of an entry into the table of content as well as any children items.
When DocumentViewer is created and the value of BookmarksContainer is null, then bookmarks support is not required by the application and Bookmarks will be null.
When the value of BookmarksContainer is not null, then it should be an existing control in the application that will be used as the parent UI element to where DocumentViewerBookmarks creates its UI controls.
In the Windows Forms platform, a TreeView is created during CreateDocumentViewer and added as a child control to BookmarksContainer.
When SetDocument is called to set a new LEADDocument in the viewer, the following occurs:
The tree view control is cleared and all items that belong to the previous document are removed, if any.
If LEADDocument is not null, then IsStructureSupported is used to check whether the document type supports bookmarks, if this is not true then no further action is required.
If IsStructureSupported is true, then the value of IsParsed is used to check whether the bookmarks have already been parsed by the user.
If the value is false, then DocumentStructure is parsed in a background thread to read the document structure and populate the bookmarks. This is performed in a separate thread since reading document bookmarks can be time consuming and to allow the user to start interacting with the document in the viewer without waiting for all the bookmarks to be parsed. When this happens, the Operation event will fire with DocumentViewerOperation.LoadingBookmarks. This allows the application to show a user interface indication that the bookmarks are being loaded. The LEADTOOLS Document Viewer demo uses this event to show a marquee progress bar on top of the bookmarks panel. This progress bar is made visible when loading starts (the value of IsPostOperation property of the event data is false) and made invisible when the loading completes (IsPostOperation is true). IsLoading can be used to check if the background thread is currently busy loading the bookmarks.
When the bookmarks are ready (Either the background thread completes loading or the document structure was already parsed prior to setting it in the document), then DocumentViewerBookmarks will iterate through the items in the Bookmarks list of LEADDocument and creates the parents and children items that correspond to the document table of content.
The tree view control can be accessed through TreeView. DocumentViewerBookmarks handles all the necessary mouse/touch and keyboard events to handle the user interface interaction with the bookmarks. When the user clicks on a bookmarks or presses "Enter" while the control has the focus and a selected item, then the following occurs:
The Operation fires with DocumentViewerOperation.GotoBookmark, the selected DocumentBookmark in Data1 and IsPostOperation set to false. This allows the application to intercept this event and change the default behavior by setting the value of Abort to true.
If the user did not abort the operation, GotoBookmark is called with the selected DocumentBookmark object. The viewer will perform the action of the bookmark Target and go to the specified target page and uses the optional fit and zoom percentage specified.
If the user did not abort the operation, then Operation will fire again with IsPostOperation set to true to indicate that the operation was completed.
using Leadtools;
using Leadtools.Controls;
using Leadtools.Document;
using Leadtools.Document.Viewer;
using Leadtools.Codecs;
using Leadtools.Caching;
using Leadtools.Annotations.Engine;
using Leadtools.Ocr;
public void DocumentViewer_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 OCR. This is optional and not required for the PDF document we are loading.
// But if image documents are loaded and text functionality is used, then we need OCR
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
_ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime");
Init();
}
base.OnLoad(e);
}
// Our document viewer instance
private DocumentViewer _documentViewer;
// Optional OCR engine to use in the example
private IOcrEngine _ocrEngine;
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],
// The right panel is for bookmarks
BookmarksContainer = this.Controls.Find("rightPanel", false)[0],
// Not using annotations for now
UseAnnotations = false
};
// Create the document viewer
_documentViewer = DocumentViewerFactory.CreateDocumentViewer(createOptions);
// We prefer SVG viewing
_documentViewer.View.PreferredItemType = DocumentViewerItemType.Svg;
// Load a document
var fileName = @"C:\LEADTOOLS22\Resources\Images\Leadtools.pdf";
var document = DocumentFactory.LoadFromFile(
fileName,
new LoadDocumentOptions { UseCache = DocumentFactory.Cache != null });
_documentViewer.Operation += (sender, e) =>
{
if (e.Operation == DocumentViewerOperation.LoadingBookmarks)
{
// Disable the bookmarks when we are loading, enable when we are done
var rightPanel = this.Controls.Find("rightPanel", true)[0];
if (rightPanel.InvokeRequired)
{
rightPanel.Invoke((MethodInvoker)delegate { rightPanel.Enabled = e.IsPostOperation; });
}
else
{
rightPanel.Enabled = e.IsPostOperation;
}
}
};
// If we have an OCR engine, use it
document.Text.OcrEngine = _ocrEngine;
// 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 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
Console.WriteLine(this + " Ready");
}
}