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 HtmlFittingAndScalingOptions : 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 the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
htmlToPdfConverter.ServerPassword = serverPassword;
// set a demo serial number
htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// set browser width
htmlToPdfConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);
// set browser height if specified, otherwise use the default
if (textBoxBrowserHeight.Text.Length > 0)
htmlToPdfConverter.BrowserHeight = int.Parse(textBoxBrowserHeight.Text);
// set HTML Load timeout
htmlToPdfConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);
// optionally wait an additional time before starting the conversion
// it is not necessary to set this property if the HTML page is not updated after initial load
htmlToPdfConverter.WaitBeforeConvert = 2;
// set PDF page size and orientation
htmlToPdfConverter.Document.PageSize = GetSelectedPageSize();
htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation();
// set PDF page size and orientation
htmlToPdfConverter.Document.PageSize = GetSelectedPageSize();
htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation();
// set PDF page margins
htmlToPdfConverter.Document.Margins = new PdfMargins(
int.Parse(textBoxLeftMargin.Text), int.Parse(textBoxRightMargin.Text),
int.Parse(textBoxTopMargin.Text), int.Parse(textBoxBottomMargin.Text));
// set HTML location and size in PDF page
if (textBoxHtmlLeftLocation.Text.Length > 0)
htmlToPdfConverter.Document.DestX = float.Parse(textBoxHtmlLeftLocation.Text);
if (textBoxHtmlTopLocation.Text.Length > 0)
htmlToPdfConverter.Document.DestY = float.Parse(textBoxHtmlTopLocation.Text);
if (textBoxHtmlWidth.Text.Length > 0)
htmlToPdfConverter.Document.DestWidth = float.Parse(textBoxHtmlWidth.Text);
if (textBoxHtmlHeight.Text.Length > 0)
htmlToPdfConverter.Document.DestHeight = float.Parse(textBoxHtmlHeight.Text);
// control if the HTML can be scaled to fit the PDF page width
htmlToPdfConverter.Document.FitPageWidth = checkBoxFitPageWidth.Checked;
// control if the HTML can be enlarged to fit the PDF page width when FitPageWidth is true
htmlToPdfConverter.Document.ForceFitPageWidth = checkBoxForceFitPageWidth.Checked;
// control if the PDF page can be resized to display the whole HTML content when FitPageWidth is false
htmlToPdfConverter.Document.ResizePageWidth = checkBoxResizePdfPage.Checked;
// control if the HTML content can be scaled to fit the PDF page height
htmlToPdfConverter.Document.FitPageHeight = checkBoxFitPageHeight.Checked;
// control if the whole HTML content will be rendered in one PDF page without scaling
htmlToPdfConverter.Document.PostCardMode = checkBoxPostCardMode.Checked;
// convert HTML to PDF
byte[] pdfBuffer = null;
if (radioButtonConvertUrl.Checked)
{
// convert URL to a PDF memory buffer
string url = textBoxUrl.Text;
pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
}
else
{
// convert HTML code
string htmlCode = textBoxHtmlCode.Text;
string baseUrl = textBoxBaseUrl.Text;
// convert HTML code to a PDF memory buffer
pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl);
}
// 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, attachment or inline, and the file name
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=HtmlFittingAndScaling.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();
}
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 Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dropDownListPageSizes.SelectedValue = "A4";
dropDownListPageOrientations.SelectedValue = "Portrait";
Master.SelectNode("htmlScaling");
Master.LoadCodeSample("HtmlFittingAndScaling");
}
}
protected void checkBoxFitPageWidth_CheckedChanged(object sender, EventArgs e)
{
checkBoxResizePdfPage.Visible = !checkBoxFitPageWidth.Checked;
checkBoxForceFitPageWidth.Visible = checkBoxFitPageWidth.Checked;
}
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;
}
}
}