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 PdFDocumentSecuritySettings : 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==";
// set encryption mode
document.Security.EncryptionMode = GetSelectedEncryptionMode();
// set encryption level
document.Security.EncryptionLevel = GetSelectedEncryptionLevel();
// set open password
document.Security.OpenPassword = textBoxOpenPassword.Text;
// set permissions password
document.Security.PermissionsPassword = textBoxPermissionsPassword.Text;
// set PDF document permissions
document.Security.AllowPrinting = checkBoxAllowPrint.Checked;
document.Security.AllowCopyContent = checkBoxAllowCopy.Checked;
document.Security.AllowEditContent = checkBoxAllowEdit.Checked;
document.Security.AllowEditAnnotations = checkBoxAllowEditAnnotations.Checked;
document.Security.AllowFormFilling = checkBoxAllowFillForms.Checked;
// set a default permissions password if an open password was set without settings a permissions password
// or if any of the permissions does not have the default value
if (document.Security.PermissionsPassword == String.Empty &&
(document.Security.OpenPassword != String.Empty || !IsDefaultPermission(document.Security)))
{
document.Security.PermissionsPassword = "admin";
}
// add a page to document with the default size and orientation
PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfMargins(0), PdfPageOrientation.Portrait);
try
{
// create the HTML object from URL
PdfHtml htmlObject = new PdfHtml(textBoxUrl.Text);
// optionally wait an additional time before starting the conversion
htmlObject.WaitBeforeConvert = 2;
// 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=PdfDocumentSecuritySettings.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 bool IsDefaultPermission(PdfSecurity pdfSecurity)
{
if (!pdfSecurity.AllowAssembling)
return false;
if (!pdfSecurity.AllowCopyAccessibilityContent)
return false;
if (!pdfSecurity.AllowCopyContent)
return false;
if (!pdfSecurity.AllowEditAnnotations)
return false;
if (!pdfSecurity.AllowEditContent)
return false;
if (!pdfSecurity.AllowFormFilling)
return false;
if (!pdfSecurity.AllowHighResolutionPrinting)
return false;
if (!pdfSecurity.AllowPrinting)
return false;
return true;
}
private PdfEncryptionMode GetSelectedEncryptionMode()
{
if (dropDownListEncryptionMode.SelectedValue == null)
return PdfEncryptionMode.RC4;
switch (dropDownListEncryptionMode.SelectedValue)
{
case "RC4":
return PdfEncryptionMode.RC4;
case "AES":
return PdfEncryptionMode.AES;
default:
return PdfEncryptionMode.RC4;
}
}
private PdfEncryptionLevel GetSelectedEncryptionLevel()
{
if (dropDownListEncryptionLevel.SelectedValue == null)
return PdfEncryptionLevel.High;
switch (dropDownListEncryptionLevel.SelectedValue)
{
case "Low":
return PdfEncryptionLevel.Low;
case "High":
return PdfEncryptionLevel.High;
case "Very High":
return PdfEncryptionLevel.VeryHigh;
default:
return PdfEncryptionLevel.High;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dropDownListEncryptionMode.SelectedValue = "RC4";
dropDownListEncryptionLevel.SelectedValue = "High";
Master.SelectNode("pdfDocumentSecurity");
Master.LoadCodeSample("PdfDocumentSecurity");
}
}
protected void dropDownListEncryptionMode_SelectedIndexChanged(object sender, EventArgs e)
{
PdfEncryptionMode encryptionMode = GetSelectedEncryptionMode();
PdfEncryptionLevel encryptionLevel = GetSelectedEncryptionLevel();
if (encryptionMode == PdfEncryptionMode.RC4 && encryptionLevel == PdfEncryptionLevel.VeryHigh)
dropDownListEncryptionLevel.SelectedValue = "High"; // very high mode not supported for RC4
if (encryptionMode == PdfEncryptionMode.AES && encryptionLevel == PdfEncryptionLevel.Low)
dropDownListEncryptionLevel.SelectedValue = "High"; // low mode not supported for AES
}
protected void dropDownListEncryptionLevel_SelectedIndexChanged(object sender, EventArgs e)
{
PdfEncryptionMode encryptionMode = GetSelectedEncryptionMode();
PdfEncryptionLevel encryptionLevel = GetSelectedEncryptionLevel();
if (encryptionMode == PdfEncryptionMode.RC4 && encryptionLevel == PdfEncryptionLevel.VeryHigh)
dropDownListEncryptionLevel.SelectedValue = "High"; // very high mode not supported for RC4
if (encryptionMode == PdfEncryptionMode.AES && encryptionLevel == PdfEncryptionLevel.Low)
dropDownListEncryptionLevel.SelectedValue = "High"; // low mode not supported for AES
}
protected void checkBoxAllowFillForms_CheckedChanged(object sender, EventArgs e)
{
if (!checkBoxAllowFillForms.Checked)
{
checkBoxAllowEdit.Checked = false;
checkBoxAllowEditAnnotations.Checked = false;
}
}
protected void checkBoxAllowEdit_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxAllowEdit.Checked)
checkBoxAllowFillForms.Checked = true;
}
protected void checkBoxAllowEditAnnotations_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxAllowEditAnnotations.Checked)
checkBoxAllowFillForms.Checked = true;
}
}
}