private void btnConvertToHtml_Click(object sender, EventArgs e)
{
if (pdfFileTextBox.Text.Trim().Equals(String.Empty))
{
MessageBox.Show("Please choose a source PDF file", "Choose PDF file", MessageBoxButtons.OK);
return;
}
// the source pdf file
string pdfFileName = pdfFileTextBox.Text.Trim();
// start page number
int startPageNumber = int.Parse(textBoxStartPage.Text.Trim());
// end page number
// when it is 0 the conversion will continue up to the end of document
int endPageNumber = 0;
if (textBoxEndPage.Text.Trim() != String.Empty)
endPageNumber = int.Parse(textBoxEndPage.Text.Trim());
// create the converter object and set the user options
PdfToHtmlConverter pdfToHtmlConverter = new PdfToHtmlConverter();
pdfToHtmlConverter.LicenseKey = "UtzN3cnM3c3dy9PN3c7M08zP08TExMTdzQ==";
// set the resolution of HTML images
pdfToHtmlConverter.Resolution = int.Parse(textBoxResolution.Text);
// set the zoom of HTML content
pdfToHtmlConverter.Zoom = int.Parse(textBoxZoom.Text);
// the demo output directory
string outputDirectory = Path.Combine(Application.StartupPath, @"DemoFiles\Output");
Cursor = Cursors.WaitCursor;
try
{
// convert PDF pages to HTML files in a directory
pdfToHtmlConverter.CreateIndexFile = true;
pdfToHtmlConverter.ConvertPdfPagesToHtmlFile(pdfFileName, startPageNumber, endPageNumber, outputDirectory, "PdfPage");
// uncomment the lines below to raise the PageConvertedEvent event when a PDF page is converted
// the pdfToHtmlConverter_PageConvertedEvent handler below will be executed for each converted PDF page
// Do not forget to uninstall the handler when is not needed anymore
//pdfToHtmlConverter.PageConvertedEvent += pdfToHtmlConverter_PageConvertedEvent;
//pdfToHtmlConverter.ConvertPdfPagesToHtmlInEvent(pdfFileName, startPageNumber, endPageNumber);
// uncomment the line below to convert PDF pages in memory to an array of PdfPageHtml objects
//PdfPageHtml[] pdfPageHtmls = pdfToHtmlConverter.ConvertPdfPagesToHtml(pdfFileName, startPageNumber, endPageNumber);
}
catch (Exception ex)
{
// The conversion failed
MessageBox.Show(String.Format("An error occurred. {0}", ex.Message), "Error");
return;
}
finally
{
Cursor = Cursors.Arrow;
}
try
{
System.Diagnostics.Process.Start(outputDirectory);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Cannot open output folder. {0}", ex.Message));
return;
}
}
/// <summary>
/// The PageConvertedEvent event handler called after when a PDF page was converted to HTML
/// The event is raised when the ConvertPdfPagesToHtmlInEvent() method is used
/// </summary>
/// <param name="args">The handler argument containing the PDF page HTML and page number</param>
void pdfToHtmlConverter_PageConvertedEvent(PageConvertedEventArgs args)
{
// get the HTML document and page number from even handler argument
string pdfPageHtml = args.PdfPageHtml.Html;
int pageNumber = args.PdfPageHtml.PageNumber;
// save the PDF page HTML to a file
string outputHtmlFile = Path.Combine(Application.StartupPath, @"DemoFiles\Output", "PdfPage_" + pageNumber + ".html");
File.WriteAllText(outputHtmlFile, pdfPageHtml, Encoding.UTF8);
args.PdfPageHtml.Dispose();
}