using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using HiQPdfClient;
namespace HiQPdf_Demo
{
public partial class PdfToHtmlConverter : System.Web.UI.Page
{
protected void buttonConvertPdfToHtml_Click(object sender, EventArgs e)
{
string serverIP = textBoxServerIP.Text;
uint serverPort = uint.Parse(textBoxServerPort.Text);
string serverPassword = textBoxServerPassword.Text;
// get the PDF file
string pdfFile = Server.MapPath("~") + @"\DemoFiles\Pdf\Presentation.pdf";
// create the PDF to HTML converter
PdfToHtml pdfToHtmlConverter = new PdfToHtml(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
pdfToHtmlConverter.ServerPassword = serverPassword;
// set a demo serial number
pdfToHtmlConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// set the conversion resolution in DPI
pdfToHtmlConverter.DPI = int.Parse(textBoxDPI.Text);
// set the zoom level
pdfToHtmlConverter.Zoom = int.Parse(textBoxZoom.Text);
int fromPdfPageNumber = int.Parse(textBoxFromPage.Text);
int toPdfPageNumber = textBoxToPage.Text.Length > 0 ? int.Parse(textBoxToPage.Text) : 0;
// convert a range of pages of the PDF document to HTML documents in memory
// the HTML can also be produced to a folder using the ConvertToHtmlFiles method
// or they can be produced one by one using the RaisePageConvertedToHtmlEvent method
PdfPageHtmlCode[] htmlDocuments = pdfToHtmlConverter.ConvertToHtml(pdfFile, fromPdfPageNumber, toPdfPageNumber);
// return if no page was converted
if (htmlDocuments.Length == 0)
return;
// get the first page HTML bytes in a buffer
byte[] htmlDocumentBuffer = null;
try
{
// get the HTML document UTF-8 bytes
htmlDocumentBuffer = Encoding.UTF8.GetBytes(htmlDocuments[0].HtmlCode);
}
finally
{
// dispose the HTML documents
for (int i = 0; i < htmlDocuments.Length; i++)
htmlDocuments[i].Dispose();
}
// inform the browser about the binary data format
HttpContext.Current.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
// let the browser know how to open the HTML document and the HTML document name
HttpContext.Current.Response.AddHeader("Content-Disposition",
String.Format("attachment; filename=Page.html; size={0}", htmlDocumentBuffer.Length.ToString()));
// write the HTML document buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(htmlDocumentBuffer);
// call End() method of HTTP response to stop ASP.NET page processing
HttpContext.Current.Response.End();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string pageUri = HttpContext.Current.Request.Url.AbsoluteUri;
hyperLinkOpenPdf.NavigateUrl = pageUri.Substring(0, pageUri.LastIndexOf('/')) + @"/DemoFiles/Pdf/Presentation.pdf";
Master.SelectNode("pdfToHtml");
Master.LoadCodeSample("PdfToHtml");
}
}
}
}