Document writer progress notification event.
public event EventHandler<DocumentProgressEventArgs> Progress Public Event Progress As EventHandler(Of DocumentProgressEventArgs) public event EventHandler<DocumentProgressEventArgs> Progress typedef void (^LTDocumentWriterProgressHandler)(NSInteger percentage, BOOL *stop) public void addProgressListener(ProgressListener listener)public void removeProgressListener(ProgressListener listener)
add_Progress(function(sender, e))remove_Progress(function(sender, e))
public:event EventHandler<DocumentProgressEventArgs^>^ Progress
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.
| Property | Description |
|---|---|
| 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. |
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:
This example will show how to use the Progress event to create a Windows Forms dialog box with a progress bar and a cancel button.
using Leadtools;using Leadtools.Codecs;using Leadtools.Forms.DocumentWriters;public void DocumentWriterProgressExample(){// Get the input and output file names to usevar inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.docx");var outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf");// Show the progress dialogusing (var dlg = new MyProgressDialog(inputFileName, outputFileName)){if (dlg.ShowDialog() == DialogResult.OK)MessageBox.Show("Done!");elseMessageBox.Show("User has canceled the operation or an error occured.");}}// Dialog box to show theprivate class MyProgressDialog : Form{// Filesprivate string _inputFileName;private string _outputFileName;// The controls on the dialogprivate Label _descriptionLabel;private ProgressBar _progressBar;private Button _cancelButton;// Has the operation been canceled?private bool _isCancelPending;// Is the dialog working?private bool _isWorking;public MyProgressDialog(string inputFileName, string outputFileName){InitializeComponent();_inputFileName = inputFileName;_outputFileName = outputFileName;_isCancelPending = false;_isWorking = true;}protected override void OnLoad(EventArgs e){// To keep the UI functioning, do the conversion in a separate threadBeginInvoke(new MethodInvoker(DoWork));base.OnLoad(e);}private void DoWork(){DocumentWriter docWriter;string operation = string.Empty;EventHandler<DocumentProgressEventArgs> progressHandler = (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 operationif (_isCancelPending)e.Cancel = true;Application.DoEvents();};// Get the number of pagesvar codecs = new RasterCodecs();codecs.Options.RasterizeDocument.Load.Resolution = 300;var pageCount = codecs.GetTotalPages(_inputFileName);// Create the document writerdocWriter = new DocumentWriter();// Create the documentoperation = "Creating the document...";// Subscribe to the progress eventdocWriter.Progress += new EventHandler<DocumentProgressEventArgs>(progressHandler);docWriter.BeginDocument(_outputFileName, DocumentFormat.Pdf);// Add the pagesfor (var pageNumber = 1; pageNumber <= pageCount && !_isCancelPending; pageNumber++){// Load the page as SVGvar page = new DocumentSvgPage();page.SvgDocument = codecs.LoadSvg(_inputFileName, pageNumber, null);// Add the page, notice we will not be using image/text feature (the default)operation = string.Format("Adding page {0} of {1}...", pageNumber, pageCount);docWriter.AddPage(page);// Delete the SVGpage.SvgDocument.Dispose();}// Finally finish writing the PDF file on diskoperation = "Finishing the document...";docWriter.EndDocument();// Remove the progress handlerdocWriter.Progress -= new EventHandler<DocumentProgressEventArgs>(progressHandler);codecs.Dispose();// Set the dialog results based on whether the user has canceled the operationif (_isCancelPending)DialogResult = DialogResult.Cancel;elseDialogResult = DialogResult.OK;// The dialog can be closed now_isWorking = false;// We are doneClose();}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_isCancelPending = true;}protected override void OnFormClosing(FormClosingEventArgs e){// Dont allow the form to close while the callback is still working// Instead, cancel the operationif (_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 buttonSuspendLayout();// 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 controlsControls.Add(_descriptionLabel);Controls.Add(_progressBar);Controls.Add(_cancelButton);// Initialize the dialogAutoScaleDimensions = 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();}}static class LEAD_VARS{public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.Forms.DocumentWritersPublic Sub DocumentWriterProgressExample()' Get the input and output file names to useDim inputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.docx")Dim outputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf")' Show the progress dialogUsing dlg As New MyProgressDialog(inputFileName, outputFileName)If dlg.ShowDialog() = DialogResult.OK ThenMessageBox.Show("Done!")ElseMessageBox.Show("User has canceled the operation or an error occured.")End IfEnd UsingEnd Sub' Dialog box to show thePrivate Class MyProgressDialogInherits Form' FilesPrivate _inputFileName As StringPrivate _outputFileName As String' The controls on the dialogPrivate _descriptionLabel As LabelPrivate _progressBar As ProgressBarPrivate _cancelButton As Button' Has the operation been canceled?Private _isCancelPending As Boolean' Is the dialog working?Private _isWorking As BooleanPublic Sub New(inputFileName As String, outputFileName As String)InitializeComponent()_inputFileName = inputFileName_outputFileName = outputFileName_isCancelPending = False_isWorking = TrueEnd SubProtected Overrides Sub OnLoad(ByVal e As EventArgs)' To keep the UI functioning, do the conversion in a separate threadBeginInvoke(New MethodInvoker(AddressOf DoWork))MyBase.OnLoad(e)End SubPrivate Sub DoWork()Dim docWriter As DocumentWriterDim operation As String = String.EmptyDim progressHandler As EventHandler(Of DocumentProgressEventArgs) =Sub(sender As Object, 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 operationIf (_isCancelPending) Thene.Cancel = TrueEnd IfApplication.DoEvents()End Sub' Get the number of pagesDim codecs As New RasterCodecs()codecs.Options.RasterizeDocument.Load.Resolution = 300Dim pageCount As Integer = codecs.GetTotalPages(_inputFileName)' Create the document writerdocWriter = New DocumentWriter()' Create the documentoperation = "Creating the document..."' Subscribe to the progress eventAddHandler docWriter.Progress, progressHandlerdocWriter.BeginDocument(_outputFileName, DocumentFormat.Pdf)' Add the pagesFor pageNumber As Integer = 1 To pageCountIf _isCancelPending ThenExit ForEnd If' Load the page as SVGDim page As New DocumentSvgPage()page.SvgDocument = codecs.LoadSvg(_inputFileName, pageNumber, Nothing)' Add the page, notice we will not be using image/text feature (the default)operation = String.Format("Adding page {0} of {1}...", pageNumber, pageCount)docWriter.AddPage(page)' Delete the SVGpage.SvgDocument.Dispose()Next' Finally finish writing the PDF file on diskoperation = "Finishing the document..."docWriter.EndDocument()' Remove the progress handlerRemoveHandler docWriter.Progress, progressHandlercodecs.Dispose()' Set the dialog results based on whether the user has canceled the operationIf _isCancelPending ThenDialogResult = DialogResult.CancelElseDialogResult = DialogResult.OKEnd If' The dialog can be closed now_isWorking = False' We are doneClose()End SubPrivate Sub _cancelButton_Click(sender As Object, e As EventArgs)' Set the isCanceled variable to true, this will break from the' progress callback and closes the dialog_isCancelPending = TrueEnd SubProtected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)' Dont allow the form to close while the callback is still working' Instead, cancel the operationIf _isWorking Thene.Cancel = TrueEnd IfMyBase.OnFormClosing(e)End SubPrivate Sub InitializeComponent()' Create the controls in this form, a text label, a progress bar and a cancel buttonSuspendLayout()' 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 = TrueAddHandler _cancelButton.Click, AddressOf _cancelButton_Click' Add the controlsControls.Add(_descriptionLabel)Controls.Add(_progressBar)Controls.Add(_cancelButton)' Initialize the dialogAutoScaleDimensions = New SizeF(6.0F, 13.0F)AutoScaleMode = AutoScaleMode.FontCancelButton = _cancelButtonClientSize = New Size(420, 118)FormBorderStyle = FormBorderStyle.FixedDialogMaximizeBox = FalseMinimizeBox = FalseName = "MyProgressDialog"ShowInTaskbar = FalseStartPosition = FormStartPosition.CenterParentText = "DocumentWriter Progress Example"ResumeLayout()End SubEnd ClassPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
Programming with LEADTOOLS Document Writers
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
