Document writer progress notification event.
Syntax
Example
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.
Visual Basic | Copy Code |
---|
<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()
RasterSupport.Unlock(RasterSupportType.DocumentWriters, "Replace with your own key here")
RasterSupport.Unlock(RasterSupportType.DocumentWritersPdf, "Replace with your own key here")
Dim docWriter As New DocumentWriter()
Dim pdfFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "DocumentWriter.pdf"
Dim emfFileNames() As String = _
{ _
LeadtoolsExamples.Common.ImagesPath.Path + "Ocr1.emf", _
LeadtoolsExamples.Common.ImagesPath.Path + "Ocr2.emf", _
LeadtoolsExamples.Common.ImagesPath.Path + "Ocr3.emf", _
LeadtoolsExamples.Common.ImagesPath.Path + "Ocr4.emf" _
}
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
Private Class MyProgressDialog
Inherits Form
Private _descriptionLabel As Label
Private _progressBar As ProgressBar
Private _cancelButton As Button
Private _operation As String
Private _docWriter As DocumentWriter
Private _emfFileNames() As String
Private _pdfFileName As String
Private _isCanceled As Boolean
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)
BeginInvoke(New MethodInvoker(AddressOf DoWork))
MyBase.OnLoad(e)
End Sub
Private Sub DoWork()
AddHandler _docWriter.Progress, AddressOf _docWriter_Progress
_operation = "Creating the document..."
_docWriter.BeginDocument(_pdfFileName, DocumentFormat.Pdf)
For i As Integer = 0 To _emfFileNames.Length - 1
Dim emfHandle As IntPtr = GetEnhMetaFile(_emfFileNames(i))
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)
DeleteEnhMetaFile(emfHandle)
Next
_operation = "Finishing the document..."
_docWriter.EndDocument()
RemoveHandler _docWriter.Progress, AddressOf _docWriter_Progress
If (_isCanceled) Then
DialogResult = DialogResult.Cancel
Else
DialogResult = DialogResult.OK
End If
_isWorking = False
Close()
End Sub
Private Sub _docWriter_Progress(ByVal sender As Object, ByVal e As DocumentProgressEventArgs)
_descriptionLabel.Text = _operation
_progressBar.Value = e.Percentage
If (_isCanceled) Then
e.Cancel = True
End If
Application.DoEvents()
End Sub
Private Sub _cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs)
_isCanceled = True
End Sub
Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
If (_isWorking) Then
e.Cancel = True
End If
MyBase.OnFormClosing(e)
End Sub
Private Sub InitializeComponent()
SuspendLayout()
_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..."
_progressBar = New ProgressBar()
_progressBar.Location = New Point(27, 47)
_progressBar.Name = "_progressBar"
_progressBar.Size = New Size(364, 23)
_progressBar.TabIndex = 1
_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
Controls.Add(_descriptionLabel)
Controls.Add(_progressBar)
Controls.Add(_cancelButton)
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
|
C# | Copy Code |
---|
// 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() { // Unlock the support needed for LEADTOOLS Document Writers (with PDF output) RasterSupport.Unlock(RasterSupportType.DocumentWriters, "Replace with your own key here"); RasterSupport.Unlock(RasterSupportType.DocumentWritersPdf, "Replace with your own key here"); // Create a new instance of the LEADTOOLS Document Writer DocumentWriter docWriter = new DocumentWriter(); // Get the file names to use string pdfFileName = LeadtoolsExamples.Common.ImagesPath.Path + "DocumentWriter.pdf"; string[] emfFileNames = { LeadtoolsExamples.Common.ImagesPath.Path + "Ocr1.emf", LeadtoolsExamples.Common.ImagesPath.Path + "Ocr2.emf", LeadtoolsExamples.Common.ImagesPath.Path + "Ocr3.emf", LeadtoolsExamples.Common.ImagesPath.Path + "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(); } } |
Remarks
Requirements
Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family
See Also