LEADTOOLS Forms (Leadtools.Forms.DocumentWriters assembly)

Progress Event

Show in webframe
Example 







Document writer progress notification event.
Syntax
'Declaration
 
Public Event Progress As EventHandler(Of DocumentProgressEventArgs)
'Usage
 
Dim instance As DocumentWriter
Dim handler As EventHandler(Of DocumentProgressEventArgs)
 
AddHandler instance.Progress, handler

            

            
add_Progress(function(sender, e))
remove_Progress(function(sender, e))

Event Data

The event handler receives an argument of type DocumentProgressEventArgs containing data related to this event. The following DocumentProgressEventArgs properties provide information specific to this event.

PropertyDescription
Cancel Gets or sets a value to allow the user to cancel the current operation.
Percentage Gets the value, in percent, of the completion for the current operation.
Remarks

The Progress event allows you to monitor the progress of a document writer operation as well as cancel the operation if required. This event occur when calling the following methods of the DocumentWriter class:

The DocumentProgressEventArgs class defines the data for the Progress event. It contains the following:

Example
Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Forms.DocumentWriters

' Windows API functions needed to load/delete an EMF
<DllImport("gdi32.dll")> _
Private Shared Function GetEnhMetaFile(ByVal lpszMetaFile As String) As IntPtr
End Function
<DllImport("gdi32.dll")> _
Private Shared Function DeleteEnhMetaFile(ByVal hemf As IntPtr) As Boolean
End Function

Private Sub DocumentWriterProgressExample()
   ' Create a new instance of the LEADTOOLS Document Writer
   Dim docWriter As New DocumentWriter()

   ' Get the file names to use
   Dim pdfFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "DocumentWriter.pdf")

   Dim emfFileNames() As String = _
   { _
      Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.emf"), _
      Path.Combine(LEAD_VARS.ImagesDir, "Ocr2.emf"), _
      Path.Combine(LEAD_VARS.ImagesDir, "Ocr3.emf"), _
      Path.Combine(LEAD_VARS.ImagesDir, "Ocr4.emf") _
   }

   ' Show the progress dialog
   Using dlg As New MyProgressDialog(docWriter, emfFileNames, pdfFileName)
      If (dlg.ShowDialog() = DialogResult.OK) Then
         MessageBox.Show("Done!")
      Else
         MessageBox.Show("User has canceled the operation or an error occured.")
      End If
   End Using
End Sub

' Dialog box to show the 
Private Class MyProgressDialog
   Inherits Form
   ' The controls on the dialog
   Private _descriptionLabel As Label
   Private _progressBar As ProgressBar
   Private _cancelButton As Button
   Private _operation As String

   ' Conversion properties
   Private _docWriter As DocumentWriter
   Private _emfFileNames() As String
   Private _pdfFileName As String

   ' Has the operation been canceled?
   Private _isCanceled As Boolean
   ' Is the dialog working?
   Private _isWorking As Boolean

   Public Sub New(ByVal docWriter As DocumentWriter, ByVal emfFileNames() As String, ByVal pdfFileName As String)
      InitializeComponent()

      _docWriter = docWriter
      _emfFileNames = emfFileNames
      _pdfFileName = pdfFileName

      _isCanceled = False
      _isWorking = True
   End Sub

   Protected Overrides Sub OnLoad(ByVal e As EventArgs)
      ' To keep the UI functioning, do the conversion in a separate thread
      BeginInvoke(New MethodInvoker(AddressOf DoWork))

      MyBase.OnLoad(e)
   End Sub

   Private Sub DoWork()
      ' Subscribe to the progress event
      AddHandler _docWriter.Progress, AddressOf _docWriter_Progress

      ' Create the document
      _operation = "Creating the document..."
      _docWriter.BeginDocument(_pdfFileName, DocumentFormat.Pdf)

      ' Add the pages
      For i As Integer = 0 To _emfFileNames.Length - 1
         ' Use the Windows API to load the EMF
         Dim emfHandle As IntPtr = GetEnhMetaFile(_emfFileNames(i))

         ' Add the page, notice we will not be using image/text feature (the default)
         Dim page As DocumentPage = DocumentPage.Empty
         page.EmfHandle = emfHandle
         page.Image = Nothing

         _operation = String.Format("Adding page {0} of {1}...", i + 1, _emfFileNames.Length)
         _docWriter.AddPage(page)

         ' Use the Windows API to delete the EMF
         DeleteEnhMetaFile(emfHandle)
      Next

      ' Finally finish writing the PDF file on disk
      _operation = "Finishing the document..."
      _docWriter.EndDocument()

      RemoveHandler _docWriter.Progress, AddressOf _docWriter_Progress

      ' Set the dialog results based on whether the user has canceled the operation
      If (_isCanceled) Then
         DialogResult = DialogResult.Cancel
      Else
         DialogResult = DialogResult.OK
      End If

      ' The dialog can be closed now
      _isWorking = False

      ' We are done
      Close()
   End Sub

   Private Sub _docWriter_Progress(ByVal sender As Object, ByVal e As DocumentProgressEventArgs)
      ' Update the description label progress bar
      _descriptionLabel.Text = _operation
      _progressBar.Value = e.Percentage

      ' Check if the user clicked the Cancel button, if so, abort the operation
      If (_isCanceled) Then
         e.Cancel = True
      End If

      Application.DoEvents()
   End Sub

   Private Sub _cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs)
      ' Set the isCanceled variable to true, this will break from the
      ' progress callback and closes the dialog

      _isCanceled = True
   End Sub

   Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
      ' Dont allow the form to close while the callback is still working
      ' Instead, cancel the operation
      If (_isWorking) Then
         e.Cancel = True
      End If

      MyBase.OnFormClosing(e)
   End Sub

   Private Sub InitializeComponent()
      ' Create the controls in this form, a text label, a progress bar and a cancel button
      SuspendLayout()

      ' Text label
      _descriptionLabel = New Label()
      _descriptionLabel.Location = New Point(26, 21)
      _descriptionLabel.Name = "_descriptionLabel"
      _descriptionLabel.Size = New Size(367, 23)
      _descriptionLabel.TabIndex = 0
      _descriptionLabel.TextAlign = ContentAlignment.MiddleLeft
      _descriptionLabel.Text = "Creating final document..."

      ' Progress bar
      _progressBar = New ProgressBar()
      _progressBar.Location = New Point(27, 47)
      _progressBar.Name = "_progressBar"
      _progressBar.Size = New Size(364, 23)
      _progressBar.TabIndex = 1

      ' Cancel button
      _cancelButton = New Button()
      _cancelButton.DialogResult = DialogResult.Cancel
      _cancelButton.Location = New Point(172, 76)
      _cancelButton.Name = "_cancelButton"
      _cancelButton.Size = New Size(75, 23)
      _cancelButton.TabIndex = 2
      _cancelButton.Text = "Cancel"
      _cancelButton.UseVisualStyleBackColor = True
      AddHandler _cancelButton.Click, AddressOf _cancelButton_Click

      ' Add the controls
      Controls.Add(_descriptionLabel)
      Controls.Add(_progressBar)
      Controls.Add(_cancelButton)

      ' Initialize the dialog
      AutoScaleDimensions = New SizeF(6.0F, 13.0F)
      AutoScaleMode = AutoScaleMode.Font
      CancelButton = _cancelButton
      ClientSize = New Size(420, 118)
      FormBorderStyle = FormBorderStyle.FixedDialog
      MaximizeBox = False
      MinimizeBox = False
      Name = "MyProgressDialog"
      ShowInTaskbar = False
      StartPosition = FormStartPosition.CenterParent
      Text = "DocumentWriter Progress Example"

      ResumeLayout()
   End Sub
End Class
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms.DocumentWriters;

// Windows API functions needed to load/delete an EMF
[DllImport("gdi32.dll")]
private static extern IntPtr GetEnhMetaFile(string lpszMetaFile);
[DllImport("gdi32.dll")]
private static extern bool DeleteEnhMetaFile(IntPtr hemf);
private void DocumentWriterProgressExample()
{
   // Create a new instance of the LEADTOOLS Document Writer
   DocumentWriter docWriter = new DocumentWriter();

   // Get the file names to use
   string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "DocumentWriter.pdf");

   string[] emfFileNames = 
   {
      Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.emf"),
      Path.Combine(LEAD_VARS.ImagesDir, "Ocr2.emf"),
      Path.Combine(LEAD_VARS.ImagesDir, "Ocr3.emf"),
      Path.Combine(LEAD_VARS.ImagesDir, "Ocr4.emf")
   };

   // Show the progress dialog
   using(MyProgressDialog dlg = new MyProgressDialog(docWriter, emfFileNames, pdfFileName))
   {
      if(dlg.ShowDialog() == DialogResult.OK)
         MessageBox.Show("Done!");
      else
         MessageBox.Show("User has canceled the operation or an error occured.");
   }
}

// Dialog box to show the 
private class MyProgressDialog : Form
{
   // The controls on the dialog
   private Label _descriptionLabel;
   private ProgressBar _progressBar;
   private Button _cancelButton;
   private string _operation;

   // Conversion properties
   private DocumentWriter _docWriter;
   private string[] _emfFileNames;
   private string _pdfFileName;

   // Has the operation been canceled?
   private bool _isCanceled;
   // Is the dialog working?
   private bool _isWorking;

   public MyProgressDialog(DocumentWriter docWriter, string[] emfFileNames, string pdfFileName)
   {
      InitializeComponent();

      _docWriter = docWriter;
      _emfFileNames = emfFileNames;
      _pdfFileName = pdfFileName;

      _isCanceled = false;
      _isWorking = true;
   }

   protected override void OnLoad(EventArgs e)
   {
      // To keep the UI functioning, do the conversion in a separate thread
      BeginInvoke(new MethodInvoker(DoWork));

      base.OnLoad(e);
   }

   private void DoWork()
   {
      // Subscribe to the progress event
      _docWriter.Progress += new EventHandler<DocumentProgressEventArgs>(_docWriter_Progress);

      // Create the document
      _operation = "Creating the document...";
      _docWriter.BeginDocument(_pdfFileName, DocumentFormat.Pdf);

      // Add the pages
      for(int i = 0; i < _emfFileNames.Length; i++)
      {
         // Use the Windows API to load the EMF
         IntPtr emfHandle = GetEnhMetaFile(_emfFileNames[i]);

         // Add the page, notice we will not be using image/text feature (the default)
         DocumentPage page = DocumentPage.Empty;
         page.EmfHandle = emfHandle;
         page.Image = null;

         _operation = string.Format("Adding page {0} of {1}...", i + 1, _emfFileNames.Length);
         _docWriter.AddPage(page);

         // Use the Windows API to delete the EMF
         DeleteEnhMetaFile(emfHandle);
      }

      // Finally finish writing the PDF file on disk
      _operation = "Finishing the document...";
      _docWriter.EndDocument();

      _docWriter.Progress -= new EventHandler<DocumentProgressEventArgs>(_docWriter_Progress);

      // Set the dialog results based on whether the user has canceled the operation
      if(_isCanceled)
         DialogResult = DialogResult.Cancel;
      else
         DialogResult = DialogResult.OK;

      // The dialog can be closed now
      _isWorking = false;

      // We are done
      Close();
   }

   private void _docWriter_Progress(object sender, DocumentProgressEventArgs e)
   {
      // Update the description label progress bar
      _descriptionLabel.Text = _operation;
      _progressBar.Value = e.Percentage;

      // Check if the user clicked the Cancel button, if so, abort the operation
      if(_isCanceled)
         e.Cancel = true;

      Application.DoEvents();
   }

   private void _cancelButton_Click(object sender, EventArgs e)
   {
      // Set the isCanceled variable to true, this will break from the
      // progress callback and closes the dialog

      _isCanceled = true;
   }

   protected override void OnFormClosing(FormClosingEventArgs e)
   {
      // Dont allow the form to close while the callback is still working
      // Instead, cancel the operation
      if(_isWorking)
         e.Cancel = true;

      base.OnFormClosing(e);
   }

   private void InitializeComponent()
   {
      // Create the controls in this form, a text label, a progress bar and a cancel button
      SuspendLayout();

      // Text label
      _descriptionLabel = new Label();
      _descriptionLabel.Location = new Point(26, 21);
      _descriptionLabel.Name = "_descriptionLabel";
      _descriptionLabel.Size = new Size(367, 23);
      _descriptionLabel.TabIndex = 0;
      _descriptionLabel.TextAlign = ContentAlignment.MiddleLeft;
      _descriptionLabel.Text = "Creating final document...";

      // Progress bar
      _progressBar = new ProgressBar();
      _progressBar.Location = new Point(27, 47);
      _progressBar.Name = "_progressBar";
      _progressBar.Size = new Size(364, 23);
      _progressBar.TabIndex = 1;

      // Cancel button
      _cancelButton = new Button();
      _cancelButton.DialogResult = DialogResult.Cancel;
      _cancelButton.Location = new Point(172, 76);
      _cancelButton.Name = "_cancelButton";
      _cancelButton.Size = new Size(75, 23);
      _cancelButton.TabIndex = 2;
      _cancelButton.Text = "Cancel";
      _cancelButton.UseVisualStyleBackColor = true;
      _cancelButton.Click += new System.EventHandler(_cancelButton_Click);

      // Add the controls
      Controls.Add(_descriptionLabel);
      Controls.Add(_progressBar);
      Controls.Add(_cancelButton);

      // Initialize the dialog
      AutoScaleDimensions = new SizeF(6F, 13F);
      AutoScaleMode = AutoScaleMode.Font;
      CancelButton = _cancelButton;
      ClientSize = new Size(420, 118);
      FormBorderStyle = FormBorderStyle.FixedDialog;
      MaximizeBox = false;
      MinimizeBox = false;
      Name = "MyProgressDialog";
      ShowInTaskbar = false;
      StartPosition = FormStartPosition.CenterParent;
      Text = "DocumentWriter Progress Example";

      ResumeLayout();
   }
}
Requirements

Target Platforms

See Also

Reference

DocumentWriter Class
DocumentWriter Members
DocumentPage Structure
DocumentFormat Enumeration
Programming with LEADTOOLS Document Writers
Files to be Included with Your Application
Unlocking Special LEAD Features

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.

Leadtools.Forms.DocumentWriters requires a Document or Medical toolkit license and unlock key. For more information, refer to: Imaging Pro/Document/Medical Features