HiQPdf HTML to PDF Converter offers you multiple possibilities to secure and to set the permitted operations on the generated PDF document. The security settings of the PDF document are controlled by the PdfDocumentControlSecurity property. You can password protect the PDF document with separate passwords to open the PDF document and to edit the permissions. You can also enable or disable the PDF document printing, editing and content copying. When you set a password or change the permissions the PDF document content is encrypted using an encryption algorithm. The currently supported encryption algorithms are RC4 and AES. For each type of encryption algorithm you can also set the encryption key size which gives the encryption level. AES algorithm supports high 128-bit and very high 256-bit encryption key sizes and the RC4 algorithm supports low 40-bit and high 128-bit encryption key sizes.
In this demo you can see how to convert an URL or HTML file to a PDF and how to set the security of generated PDF document. You can set a password required to open the PDF document in a PDF viewer, a password to edit the PDF document permissions in a PDF Editor, you can enable or disable the PDF document printing, content copying, editing or filling PDF forms in a PDF viewer or editor, set the encryption mode and the encryption level of the PDF document.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using HiQPdfClient;
namespace HiQPdf_Demo.Controllers
{
public class PdfSecuritySettingsController : Controller
{
IFormCollection m_formCollection;
// GET: PdfSecuritySettings
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToPdf(IFormCollection collection)
{
m_formCollection = collection;
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string serverPassword = collection["textBoxServerPassword"];
// 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 encryption mode
htmlToPdfConverter.Document.Security.EncryptionMode = GetSelectedEncryptionMode();
// set encryption level
htmlToPdfConverter.Document.Security.EncryptionLevel = GetSelectedEncryptionLevel();
// set open password
htmlToPdfConverter.Document.Security.OpenPassword = collection["textBoxOpenPassword"];
// set permissions password
htmlToPdfConverter.Document.Security.PermissionsPassword = collection["textBoxPermissionsPassword"];
// set PDF document permissions
htmlToPdfConverter.Document.Security.AllowPrinting = collection["checkBoxAllowPrint"].Count > 0;
htmlToPdfConverter.Document.Security.AllowCopyContent = collection["checkBoxAllowCopy"].Count > 0;
htmlToPdfConverter.Document.Security.AllowEditContent = collection["checkBoxAllowEdit"].Count > 0;
htmlToPdfConverter.Document.Security.AllowEditAnnotations = collection["checkBoxAllowEditAnnotations"].Count > 0;
htmlToPdfConverter.Document.Security.AllowFormFilling = collection["checkBoxAllowFillForms"].Count > 0;
// 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 (htmlToPdfConverter.Document.Security.PermissionsPassword == String.Empty &&
(htmlToPdfConverter.Document.Security.OpenPassword != String.Empty || !IsDefaultPermission(htmlToPdfConverter.Document.Security)))
{
htmlToPdfConverter.Document.Security.PermissionsPassword = "admin";
}
// convert URL to a PDF memory buffer
string url = collection["textBoxUrl"];
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "PdfSecuritySettings.pdf";
return fileResult;
}
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()
{
switch (m_formCollection["dropDownListEncryptionMode"])
{
case "RC4":
return PdfEncryptionMode.RC4;
case "AES":
return PdfEncryptionMode.AES;
default:
return PdfEncryptionMode.RC4;
}
}
private PdfEncryptionLevel GetSelectedEncryptionLevel()
{
switch (m_formCollection["dropDownListEncryptionLevel"])
{
case "Low":
return PdfEncryptionLevel.Low;
case "High":
return PdfEncryptionLevel.High;
case "Very High":
return PdfEncryptionLevel.VeryHigh;
default:
return PdfEncryptionLevel.High;
}
}
}
}