public class DocumentProgressEventArgs : EventArgs
The DocumentWriter.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 contains the following:
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Document.Writer;
public void DocumentWriterProgressExample()
{
// Get the input and output file names to use
var inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "test_3.docx");
var outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "test_multipage_DocumentWriter.pdf");
// Show the progress dialog
using (var dlg = new MyProgressDialog(inputFileName, outputFileName))
{
if (dlg.ShowDialog() == DialogResult.OK)
Debug.WriteLine("Done!");
else
Debug.WriteLine("User has canceled the operation or an error occurred.");
}
}
// 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 DocumentWriterSvgPage();
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();
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}