protected void btnConvert_Click(object sender, EventArgs e)
{
//set the license key
LicensingManager.LicenseKey =
"mj0WW6eCQTFvpDizfaa0RQvYSnDGNoBRH7hcZonemkHHfiruweooXpWgorvDv1h/";
//create a PDF document
Document document = new Document();
//optional settings for the PDF document like margins, compression
level,
//security options, viewer preferences, document information,
etc
document.CompressionLevel = CompressionLevel.NormalCompression;
document.Margins = new Margins(10, 10, 0, 0);
document.Security.CanPrint = true;
document.Security.UserPassword = "";
document.DocumentInformation.Author = "HTML
to PDF Converter";
document.ViewerPreferences.HideToolbar = false;
//Add a first page to the document. The next pages will inherit the
settings //from this page
PdfPage page = document.Pages.AddNewPage(PageSize.A4, new Margins(10, 10, 0, 0),
PageOrientation.Portrait);
// add a font to the document that can be used for the texts elements
PdfFont font = document.Fonts.Add(new Font(new FontFamily("Times New Roman"), 10,
GraphicsUnit.Point));
// add header and footer before renderng the content
AddHtmlHeader(document);
AddHtmlFooter(document, font);
// convert HTML to PDF
HtmlToPdfElement htmlToPdfElement;
if (convertURL)
{
// convert a URL to PDF
string urlToConvert = textBoxWebPageURL.Text.Trim();
htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation,
width, height, urlToConvert);
}
else
{
// convert a HTML string to PDF
string htmlStringToConvert = textBoxHTMLCode.Text;
string baseURL = textBoxBaseURL.Text.Trim();
htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, width, height,
htmlStringToConvert, baseURL);
}
//optional settings for the HTML to PDF converter
htmlToPdfElement.FitWidth = cbFitWidth.Checked;
htmlToPdfElement.EmbedFonts = cbEmbedFonts.Checked;
htmlToPdfElement.LiveUrlsEnabled = cbLiveLinks.Checked;
htmlToPdfElement.RightToLeftEnabled = cbRTL.Checked;
htmlToPdfElement.ScriptsEnabled = cbClientScripts.Checked;
htmlToPdfElement.ActiveXEnabled = cbActiveXEnabled.Checked;
// add theHTML to PDF converter element to
page
AddElementResult addResult = page.AddElement(htmlToPdfElement);
// The code below can be used add some other elements right under
the conversion result
// like texts or another HTML to PDF conversion
// add a text element right under the HTML
to PDF document
PdfPage endPage = document.Pages[addResult.EndPageIndex];
TextElement nextTextElement = new TextElement(0, addResult.EndPageBounds.Bottom + 10,
"Below there is another HTML to PDF Element", font);
nextTextElement.ForeColor = Color.Green;
addResult = endPage.AddElement(nextTextElement);
// add another HTML to PDF converter element right under the text
element
endPage = document.Pages[addResult.EndPageIndex];
HtmlToPdfElement nextHtmlToPdfElement =
new HtmlToPdfElement(0, addResult.EndPageBounds.Bottom + 10,
"http://www.google.com");
addResult = endPage.AddElement(nextHtmlToPdfElement);
// send the generated PDF document to client browser
document.Save(Response, false, "HtmlConvert.pdf");
}
private void AddHtmlHeader(Document document)
{
string thisPageURL = HttpContext.Current.Request.Url.AbsoluteUri;
string headerAndFooterHtmlUrl = thisPageURL.Substring(0, thisPageURL.LastIndexOf('/'))
+
"/HeaderAndFooterHtml.htm";
//create a template to be added in the header
and footer
document.HeaderTemplate = document.AddTemplate(document.Pages[0].ClientRectangle.Width, 60);
// create a HTML to PDF converter element to be added to the header
template
HtmlToPdfElement headerHtmlToPdf = new HtmlToPdfElement(headerAndFooterHtmlUrl);
document.HeaderTemplate.AddElement(headerHtmlToPdf);
}
private void AddHtmlFooter(Document document, PdfFont footerPageNumberFont)
{
string thisPageURL = HttpContext.Current.Request.Url.AbsoluteUri;
string headerAndFooterHtmlUrl = thisPageURL.Substring(0, thisPageURL.LastIndexOf('/'))
+
"/HeaderAndFooterHtml.htm";
//create a template to be added in the header
and footer
document.FooterTemplate = document.AddTemplate(document.Pages[0].ClientRectangle.Width, 60);
// create a HTML to PDF converter element to be added to the header
template
HtmlToPdfElement footerHtmlToPdf = new HtmlToPdfElement(headerAndFooterHtmlUrl);
document.FooterTemplate.AddElement(footerHtmlToPdf);
// add page number to the footer
TextElement pageNumberText =
new TextElement(document.FooterTemplate.ClientRectangle.Width - 100, 30,
"This is page &p; of &P; pages", footerPageNumberFont);
document.FooterTemplate.AddElement(pageNumberText);
}