Converter Event Handling
The DynamicPDF Converter for .NET supports event handling when converting different inputs to a PDF document.
Converter Events
The Converter
class has the following events which can be handled by one of the Converter
class's child classes.
Handling converter events using the AsyncConverter class is obsolete and one of the Converter
class's child classes should be used instead.
The following events can be handled when converting to a PDF is using one of the specific file-type converters.
ExcelConverter
The ExcelConverter
class can handle the following three events.
Event | Event Handler |
---|---|
Completed |
ConversionCompleted |
DocumentParsing |
ExcelDocumentParsing |
ProgressChanged |
ProgressChanged |
HtmlConverter
, ImageConverter
, PowerPointConverter
, RtfConverter
, TextConverter
, VisioConverter
, WordConverter
These converters can handle the following two events.
EVENT | EVENT Handler |
---|---|
Completed |
ConversionCompleted |
ProgressChanged |
ProgressChanged |
Event Example (Excel)
The following illustrates handling events when converting and Excel spreadsheet.
public static void EventExcelExample(string outputPdf)
{
ExcelConverter excel = new ExcelConverter(Program.GetResourcePath(@"DocumentA.xlsx"));
excel.DocumentParsing += new ExcelDocumentParsing(converter_documentparsing);
excel.ProgressChanged += new ProgressChanged(converter_progresschanged);
excel.Completed += new ConversionCompleted(converter_completed);
excel.Convert(Program.GetOutputDocPath(outputPdf));
}
public static void converter_progresschanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine(e.Status);
}
static void converter_documentparsing(object sender, ExcelDocumentParsingEventArgs e)
{
Console.WriteLine("parsing spreadsheet");
foreach (ExcelWorksheet excelWorksheet in e.Worksheets)
{
Console.WriteLine("Parsing a worksheet");
}
}
public static void converter_completed(object sender, ConversionCompletedEventArgs e)
{
if (e.FailException != null)
Console.WriteLine("Conversion failed with:" + e.FailException.Message);
else Console.WriteLine("Conversion completed");
}
Public Shared Sub EventExcelExample(outputPdf As String)
Dim excel As New ExcelConverter(Program.GetResourcePath("DocumentA.xlsx"))
AddHandler excel.DocumentParsing, AddressOf converter_documentparsing
AddHandler excel.ProgressChanged, AddressOf converter_progresschanged
AddHandler excel.Completed, AddressOf converter_completed
excel.Convert(Program.GetOutputDocPath(outputPdf))
End Sub
Public Shared Sub converter_progresschanged(sender As Object, e As ProgressChangedEventArgs)
Console.WriteLine(e.Status)
End Sub
Private Shared Sub converter_documentparsing(sender As Object, e As ExcelDocumentParsingEventArgs)
Console.WriteLine("parsing spreadsheet")
For Each excelWorksheet As ExcelWorksheet In e.Worksheets
Console.WriteLine("Parsing a worksheet")
Next
End Sub
Public Shared Sub converter_completed(sender As Object, e As ConversionCompletedEventArgs)
If e.FailException IsNot Nothing Then
Console.WriteLine("Conversion failed with:" & e.FailException.Message)
Else
Console.WriteLine("Conversion completed")
End If
End Sub
A common error when using Excel is that not all pages are printed. This is because the page setup options specified within a particular Excel file can be set to print only a particular area of the file. Since these settings are defined within the Excel file itself, there is no way to override them, and they need to be changed within the file itself if any portion of the file outside the defined page setup option is to be printed.
Event Example (Word)
The following example illustrates handling events when converting a Word document.
public static void EventWordExample(string outputPdf)
{
WordConverter word = new WordConverter(Program.GetResourcePath(@"DocumentA.doc"));
word.ProgressChanged += new ProgressChanged(converter_progresschanged);
word.Completed += new ConversionCompleted(converter_completed);
word.Convert(Program.GetOutputDocPath(outputPdf));
}
static void converter_documentparsing(object sender, ExcelDocumentParsingEventArgs e)
{
Console.WriteLine("parsing spreadsheet");
foreach (ExcelWorksheet excelWorksheet in e.Worksheets)
{
Console.WriteLine("Parsing a worksheet");
}
}
public static void converter_completed(object sender, ConversionCompletedEventArgs e)
{
if (e.FailException != null)
Console.WriteLine("Conversion failed with:" + e.FailException.Message);
else Console.WriteLine("Conversion completed");
}
Public Shared Sub EventWordExample(outputPdf As String)
Dim word As New WordConverter(Program.GetResourcePath("DocumentA.doc"))
AddHandler word.ProgressChanged, AddressOf converter_progresschanged
AddHandler word.Completed, AddressOf converter_completed
word.Convert(Program.GetOutputDocPath(outputPdf))
End Sub
Private Shared Sub converter_documentparsing(sender As Object, e As ExcelDocumentParsingEventArgs)
Console.WriteLine("parsing spreadsheet")
For Each excelWorksheet As ExcelWorksheet In e.Worksheets
Console.WriteLine("Parsing a worksheet")
Next
End Sub
Public Shared Sub converter_completed(sender As Object, e As ConversionCompletedEventArgs)
If e.FailException IsNot Nothing Then
Console.WriteLine("Conversion failed with:" & e.FailException.Message)
Else
Console.WriteLine("Conversion completed")
End If
End Sub