This tutorial shows how to encrypt a PDF, apply permissions and check the result from a C# WinForms or console application. The component is registered as PDFEncryptX.EncryptLib and works in both 32-bit and 64-bit processes.
The example uses the vendor's permission mask: printing, copying and editing are disabled while annotations and form filling remain available. Adjust the mask to match your document policy.
Before you start
- Install the PDF Encrypt ActiveX DLL trial. The installer registers the COM component.
- Add a reference to
PDFEncryptXin your project (or use late binding withCreateObject). - Call
SetLicenseKey()before converting. During evaluation you can leave the placeholder key. - Load the input PDF, build the permission mask, call
Encrypt(), then close the document.
Complete example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PDFEncryptX;
namespace Application1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
EncryptLibClass obj = new PDFEncryptX.EncryptLibClass();
obj.SetLicenseKey("License Code");
int iRtn = obj.LoadFromFile(".\\PDFEncryptX.pdf", "");
if (iRtn == 1)
{
if (obj.IsEncrypted == 1)
{
textBox1.Text = "YES";
}
else
{
textBox1.Text = "NO";
}
textBox2.Text = obj.PageCount.ToString();
textBox3.Text = obj.FileSize.ToString();
}
//Create Permissions: Can not to print, copy and edit.
int iPermissions = obj.CreatePermissions(0, 0, 0, 1, 1, 1, 1, 1);
//Call encrypt method
int iValue = obj.Encrypt(".\\PDFEncryptX_CanNot_Print_Copy_Edit.pdf", 2, iPermissions, "", "");
switch(iValue)
{
case 0: textBox4.Text = "0->Encrypt failed"; break;
case 1: textBox4.Text = "1->Encrypt successful"; break;
case 2: textBox4.Text = "2->Ignore - file already encrypted"; break;
}
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}How it works
LoadFromFile()returns 1 on success, 0 when the file cannot be loaded, and -1 when the file does not exist.Encrypt()returns 0 for failure, 1 for success and 2 when the file is already encrypted.- The output path must be writable by the account running the application.