public bool Cancel { get; set; }
true to cancel the current operation; false to continue the current operation.
Some of the DocumentWriter operations that use the progress monitor can be lengthy. You can allow the user to cancel the operation by using the DocumentWriter.Progress event to show a progress bar and a cancel button in your application. If the user presses the cancel button, you can set the value of the Cancel property to true to abort the current operation.
using Leadtools;using Leadtools.Codecs;using Leadtools.Document.Writer;public void DocumentWriterProgressExample(){// Get the input and output file names to usevar inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "test_3.docx");var outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "test_multipage_DocumentWriter.pdf");// Show the progress dialogusing (var dlg = new MyProgressDialog(inputFileName, outputFileName)){if (dlg.ShowDialog() == DialogResult.OK)Debug.WriteLine("Done!");elseDebug.WriteLine("User has canceled the operation or an error occurred.");}}// 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 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 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:\LEADTOOLS23\Resources\Images";}