using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HiQPdfClient;
namespace HiQPdf_Demo
{
public partial class ConvertManyHtmlToPdf : System.Web.UI.Page
{
protected void buttonCreatePdf_Click(object sender, EventArgs e)
{
string serverIP = textBoxServerIP.Text;
uint serverPort = uint.Parse(textBoxServerPort.Text);
string serverPassword = textBoxServerPassword.Text;
// create an empty PDF document
PdfDocument document = new PdfDocument(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
document.ServerPassword = serverPassword;
// set a demo serial number
document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// add a page to document
PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfMargins(5), PdfPageOrientation.Portrait);
try
{
// set the document header and footer before adding any objects to document
SetHeader(document);
SetFooter(document);
// layout the HTML from URL 1
PdfHtml html1 = new PdfHtml(textBoxUrl1.Text);
html1.WaitBeforeConvert = 2;
page1.Layout(html1);
// determine the PDF page where to add URL 2
if (checkBoxNewPage.Checked)
{
// URL 2 is laid out on a new page with the selected orientation
PdfPage page2 = document.AddPage(PdfPageSize.A4, new PdfMargins(5), GetSelectedPageOrientation());
// layout the HTML from URL 2
PdfHtml html2 = new PdfHtml(textBoxUrl2.Text);
html2.WaitBeforeConvert = 2;
page2.Layout(html2);
}
else
{
// URL 2 is laid out immediately after URL 1
// using the document realtive flow layout
PdfHtml html2 = new PdfHtml(textBoxUrl2.Text);
html2.WaitBeforeConvert = 2;
document.Layout(html2);
}
// write the PDF document to a memory buffer
byte[] pdfBuffer = document.WriteToMemory();
// inform the browser about the binary data format
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
// let the browser know how to open the PDF document and the file name
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=ConvertManyHtmlToPdf.pdf; size={0}",
pdfBuffer.Length.ToString()));
// write the PDF buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(pdfBuffer);
// call End() method of HTTP response to stop ASP.NET page processing
HttpContext.Current.Response.End();
}
finally
{
document.Close();
}
}
private void SetHeader(PdfDocument document)
{
if (!checkBoxAddHeader.Checked)
return;
// create the document header
document.CreateHeaderCanvas(50);
// add PDF objects to the header canvas
string headerImageFile = Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo.png";
PdfImage logoHeaderImage = new PdfImage(5, 5, 40, headerImageFile);
document.Header.Layout(logoHeaderImage);
// layout HTML in header
PdfHtml headerHtml = new PdfHtml(50, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
headerHtml.FitDestHeight = true;
headerHtml.FontEmbedding = true;
document.Header.Layout(headerHtml);
PdfPage startPage = document.Pages[0];
float headerWidth = startPage.Size.Width - startPage.Margins.Left - startPage.Margins.Right;
float headerHeight = document.Header.Height;
// create a border for header
PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2);
borderRectangle.LineStyle.LineWidth = 0.5f;
borderRectangle.ForeColor = PdfColor.Navy;
document.Header.Layout(borderRectangle);
}
private void SetFooter(PdfDocument document)
{
if (!checkBoxAddFooter.Checked)
return;
//create the document footer
document.CreateFooterCanvas(50);
// layout HTML in footer
PdfHtml footerHtml = new PdfHtml(5, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
footerHtml.FitDestHeight = true;
footerHtml.FontEmbedding = true;
document.Footer.Layout(footerHtml);
PdfPage startPage = document.Pages[0];
float footerWidth = startPage.Size.Width - startPage.Margins.Left - startPage.Margins.Right;
float footerHeight = document.Footer.Height;
// add page numbering
PdfFont pageNumberingFont = new PdfFont("Times New Roman", 8, true);
PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont);
pageNumberingText.HorizontalAlign = PdfTextHAlign.Center;
pageNumberingText.EmbedSystemFont = true;
pageNumberingText.ForeColor = PdfColor.DarkGreen;
document.Footer.Layout(pageNumberingText);
string footerImageFile = Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo.png";
PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, footerImageFile);
document.Footer.Layout(logoFooterImage);
// create a border for footer
PdfRectangle borderRectangle = new PdfRectangle(1, 1, footerWidth - 2, footerHeight - 2);
borderRectangle.LineStyle.LineWidth = 0.5f;
borderRectangle.ForeColor = PdfColor.DarkGreen;
document.Footer.Layout(borderRectangle);
}
private PdfPageOrientation GetSelectedPageOrientation()
{
return (dropDownListPageOrientations.SelectedValue == "Portrait") ?
PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
panelNewPageOrientation.Visible = checkBoxNewPage.Checked;
Master.SelectNode("convertManyHtmlToPdf");
Master.LoadCodeSample("ConvertManyHtmlToPdf");
}
}
protected void checkBoxNewPage_CheckedChanged(object sender, EventArgs e)
{
panelNewPageOrientation.Visible = checkBoxNewPage.Checked;
}
}
}