using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HiQPdfClient;
namespace HiQPdf_Demo
{
public partial class PdfHtmlObjectsDemo : System.Web.UI.Page
{
protected void buttonConvertToPdf_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(GetSelectedPageSize(), new PdfMargins(0), GetSelectedPageOrientation());
try
{
// create the HTML object from URL or HTML code
PdfHtml htmlObject = null;
if (radioButtonConvertUrl.Checked)
{
// create from URL
htmlObject = new PdfHtml(textBoxUrl.Text);
}
else
{
// create from HTML code
string htmlCode = textBoxHtmlCode.Text;
string baseUrl = textBoxBaseUrl.Text;
htmlObject = new PdfHtml(htmlCode, baseUrl);
}
// set the HTML object start location in PDF page
htmlObject.DestX = float.Parse(textBoxDestX.Text);
htmlObject.DestY = float.Parse(textBoxDestY.Text);
// set the HTML object width in PDF
if (textBoxDestWidth.Text.Length > 0)
htmlObject.DestWidth = float.Parse(textBoxDestWidth.Text);
// set the HTML object height in PDF
if (textBoxDestHeight.Text.Length > 0)
htmlObject.DestHeight = float.Parse(textBoxDestHeight.Text);
// optionally wait an additional time before starting the conversion
htmlObject.WaitBeforeConvert = 2;
// set browser width
htmlObject.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);
// set browser height if specified, otherwise use the default
if (textBoxBrowserHeight.Text.Length > 0)
htmlObject.BrowserHeight = int.Parse(textBoxBrowserHeight.Text);
// set HTML load timeout
htmlObject.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);
// layout the HTML object in PDF
page1.Layout(htmlObject);
// 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=PdfHtmlObjects.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 PdfPageSize GetSelectedPageSize()
{
switch (dropDownListPageSizes.SelectedValue)
{
case "A0":
return PdfPageSize.A0;
case "A1":
return PdfPageSize.A1;
case "A10":
return PdfPageSize.A10;
case "A2":
return PdfPageSize.A2;
case "A3":
return PdfPageSize.A3;
case "A4":
return PdfPageSize.A4;
case "A5":
return PdfPageSize.A5;
case "A6":
return PdfPageSize.A6;
case "A7":
return PdfPageSize.A7;
case "A8":
return PdfPageSize.A8;
case "A9":
return PdfPageSize.A9;
case "ArchA":
return PdfPageSize.ArchA;
case "ArchB":
return PdfPageSize.ArchB;
case "ArchC":
return PdfPageSize.ArchC;
case "ArchD":
return PdfPageSize.ArchD;
case "ArchE":
return PdfPageSize.ArchE;
case "B0":
return PdfPageSize.B0;
case "B1":
return PdfPageSize.B1;
case "B2":
return PdfPageSize.B2;
case "B3":
return PdfPageSize.B3;
case "B4":
return PdfPageSize.B4;
case "B5":
return PdfPageSize.B5;
case "Flsa":
return PdfPageSize.Flsa;
case "HalfLetter":
return PdfPageSize.HalfLetter;
case "Ledger":
return PdfPageSize.Ledger;
case "Legal":
return PdfPageSize.Legal;
case "Letter":
return PdfPageSize.Letter;
case "Letter11x17":
return PdfPageSize.Letter11x17;
case "Note":
return PdfPageSize.Note;
default:
return PdfPageSize.A4;
}
}
private PdfPageOrientation GetSelectedPageOrientation()
{
return (dropDownListPageOrientations.SelectedValue == "Portrait") ?
PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
}
protected void radioButtonConvertUrl_CheckedChanged(object sender, EventArgs e)
{
panelUrl.Visible = radioButtonConvertUrl.Checked;
panelHtmlCode.Visible = !radioButtonConvertUrl.Checked;
}
protected void radioButtonConvertHtmlCode_CheckedChanged(object sender, EventArgs e)
{
panelUrl.Visible = !radioButtonConvertHtmlCode.Checked;
panelHtmlCode.Visible = radioButtonConvertHtmlCode.Checked;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dropDownListPageSizes.SelectedValue = "A4";
dropDownListPageOrientations.SelectedValue = "Portrait";
Master.SelectNode("pdfHtmlObjects");
Master.LoadCodeSample("PdfHtmlObjects");
}
}
}
}