Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction - All Topics | Help Version 19.0.6.22
|
Leadtools.Forms.DocumentWriters Namespace > DocumentWriter Class : Progress Event |
public event EventHandler<DocumentProgressEventArgs> Progress
'Declaration
Public Event Progress As EventHandler(Of DocumentProgressEventArgs)
'Usage
Dim instance As DocumentWriter Dim handler As EventHandler(Of DocumentProgressEventArgs) AddHandler instance.Progress, handler
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.
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Forms.DocumentWriters Public Sub DocumentWriterProgressExample() ' Get the input and output file names to use Dim inputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.docx") Dim outputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf") ' Show the progress dialog Using dlg As New MyProgressDialog(inputFileName, outputFileName) 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 ' Files Private _inputFileName As String Private _outputFileName As String ' The controls on the dialog Private _descriptionLabel As Label Private _progressBar As ProgressBar Private _cancelButton As Button ' Has the operation been canceled? Private _isCancelPending As Boolean ' Is the dialog working? Private _isWorking As Boolean Public Sub New(inputFileName As String, outputFileName As String) InitializeComponent() _inputFileName = inputFileName _outputFileName = outputFileName _isCancelPending = 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() Dim docWriter As DocumentWriter Dim operation As String = String.Empty Dim 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 operation If (_isCancelPending) Then e.Cancel = True End If Application.DoEvents() End Sub ' Get the number of pages Dim codecs As New RasterCodecs() codecs.Options.RasterizeDocument.Load.Resolution = 300 Dim pageCount As Integer = codecs.GetTotalPages(_inputFileName) ' Create the document writer docWriter = New DocumentWriter() ' Create the document operation = "Creating the document..." ' Subscribe to the progress event AddHandler docWriter.Progress, progressHandler docWriter.BeginDocument(_outputFileName, DocumentFormat.Pdf) ' Add the pages For pageNumber As Integer = 1 To pageCount If _isCancelPending Then Exit For End If ' Load the page as SVG Dim 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 SVG page.SvgDocument.Dispose() Next ' Finally finish writing the PDF file on disk operation = "Finishing the document..." docWriter.EndDocument() ' Remove the progress handler RemoveHandler docWriter.Progress, progressHandler codecs.Dispose() ' Set the dialog results based on whether the user has canceled the operation If _isCancelPending 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 _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 = 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; public void DocumentWriterProgressExample() { // Get the input and output file names to use var inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.docx"); var outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf"); // Show the progress dialog using (var dlg = new MyProgressDialog(inputFileName, outputFileName)) { 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 { // Files private string _inputFileName; private string _outputFileName; // The controls on the dialog private 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 thread BeginInvoke(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 operation if (_isCancelPending) e.Cancel = true; Application.DoEvents(); }; // Get the number of pages var codecs = new RasterCodecs(); codecs.Options.RasterizeDocument.Load.Resolution = 300; var pageCount = codecs.GetTotalPages(_inputFileName); // Create the document writer docWriter = new DocumentWriter(); // Create the document operation = "Creating the document..."; // Subscribe to the progress event docWriter.Progress += new EventHandler<DocumentProgressEventArgs>(progressHandler); docWriter.BeginDocument(_outputFileName, DocumentFormat.Pdf); // Add the pages for (var pageNumber = 1; pageNumber <= pageCount && !_isCancelPending; pageNumber++) { // Load the page as SVG var 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 SVG page.SvgDocument.Dispose(); } // Finally finish writing the PDF file on disk operation = "Finishing the document..."; docWriter.EndDocument(); // Remove the progress handler docWriter.Progress -= new EventHandler<DocumentProgressEventArgs>(progressHandler); codecs.Dispose(); // Set the dialog results based on whether the user has canceled the operation if (_isCancelPending) DialogResult = DialogResult.Cancel; else DialogResult = DialogResult.OK; // The dialog can be closed now _isWorking = false; // We are done Close(); } 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 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(); } }