HiQPdf library offers you multiple possibilities to secure and to set the permitted operations on a PDF document. The security settings of the PDF document are controlled by the PdfDocumentSecurity 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 set the security of PDF document containg the conversion to PDF of a HTML content from a given URL. 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 PdfDocumentSecuritySettingsController : Controller
{
IFormCollection m_formCollection;
// GET: PdfDocumentSecuritySettings
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult CreatePdf(IFormCollection collection)
{
m_formCollection = collection;
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string serverPassword = collection["textBoxServerPassword"];
// 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 = collection["textBoxOpenPassword"];
// set permissions password
document.Security.PermissionsPassword = collection["textBoxPermissionsPassword"];
// set PDF document permissions
document.Security.AllowPrinting = collection["checkBoxAllowPrint"].Count > 0;
document.Security.AllowCopyContent = collection["checkBoxAllowCopy"].Count > 0;
document.Security.AllowEditContent = collection["checkBoxAllowEdit"].Count > 0;
document.Security.AllowEditAnnotations = collection["checkBoxAllowEditAnnotations"].Count > 0;
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 (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(collection["textBoxUrl"]);
// 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();
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "PdfDocumentSecuritySettings.pdf";
return fileResult;
}
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()
{
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;
}
}
}
}