User password to use when disallowing certain features in a PDF file such as editing, copying, and printing.
public string UserPassword { get; set; }
A System.String
that contains the user password to use when encrypting a PDF file. The default value is null.
There are two different types of passwords to protect different aspects of a PDF file with a password:
Password Type | Permissions |
---|---|
UserPassword | Restricted by PDFSecurityOptions properties |
OwnerPassword | Full Access, PDFSecurityOptions properties do not restrict access |
UserPassword protects with encryption and requires a valid password to open it. Use for restricting PDF file properties. UserPassword requires 128-bit encryption to provide the level of control available with the PDFSecurityOptions properties.
PDF viewers such as Adobe Acrobat will ask for the UserPassword if specified in a PDF file. Viewing the PDF file is only granted with a correct password value. To allow anyone to view the PDF file, do not set a value in UserPassword (leave it as an empty string or null).
Used to restrict certain rights on the PDF file when loading into a viewer such as Adobe Acrobat, these include but not limited to disabling printing the PDF file. When protecting a PDF file with a UserPassword you can control user access to PDF attributes:
OwnerPassword protects with encryption and requires a valid password to open it. OwnerPassword can open PDF files with no restrictions to the PDF attributes, even if restrictions are specified by PDFSecurityOptions. The file is encrypted either with 40- or 128-bit encryption.
In cases where both OwnerPassword and UserPassword are set on the same PDF file, consider the target audience and the kind of PDF file permissions and control that can be granted by each of the password options.
For both password options, opening and viewing the file with either password is possible. However, UserPassword provides another layer of file permissions to restrict certain properties even after opening the PDF file. UserPassword is able to block content editing, printing, copying, document assembly and other restrictions as set with PDFSecurityOptions. In the case of OwnerPassword, with the correct password a file is opened and viewed without restrictions.
In general, the target audience to use the UserPassword option, is an audience of readers and not editors. In the case of editors, the OwnerPassword option is best as it grants full permission rights. For comparison, two tables below show the PDFSecurityOptions properties setting access to a PDF file with both UserPassword and OwnerPassword passwords:
Case 1: Disabled PDFSecurityOptions Permissions
The OwnerPassword option provides full permission but the UserPassword option has restricted access.
PDFSecurityOptions Property | Enabled | UserPassword Permission | OwnerPassword Permission |
---|---|---|---|
Annotations | false | ☒ | ☑ |
Assembling | false | ☒ | ☑ |
Copying text to the clipboard | false | ☒ | ☑ |
Editing | false | ☒ | ☑ |
Printing Quality | false | ☒ | ☑ |
Printing | false | ☒ | ☑ |
Case 2: Enabled PDFSecurityOptions Permissions
The OwnerPassword option provides full permission and the UserPassword option has gained additional permissions.
PDFSecurityOptions Property | Enabled | UserPassword Permission | OwnerPassword Permission |
---|---|---|---|
Annotations | true | ☑ | ☑ |
Assembling | true | ☑ | ☑ |
Copying text to the clipboard | true | ☑ | ☑ |
Editing | true | ☑ | ☑ |
Printing Quality | true | ☑ | ☑ |
Printing | true | ☑ | ☑ |
using Leadtools.WinForms;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Controls;
using Leadtools.Drawing;
using Leadtools.ImageProcessing;
using Leadtools.Pdf;
using Leadtools.Svg;
/// This example will encrypt a PDF file with both a user password and an owner password and restrict printing.
public void PDFFileSecurityOptionsExample()
{
string sourceFileName = Path.Combine(LEAD_VARS.ImagesDir, @"Leadtools.pdf");
string destinationFileName = Path.Combine(LEAD_VARS.ImagesDir, @"LEAD_EncryptedNoPrint.pdf");
PDFFile pdfFile = new PDFFile(sourceFileName);
// Set the security options
pdfFile.SecurityOptions = new PDFSecurityOptions();
pdfFile.SecurityOptions.UserPassword = "LEAD";
pdfFile.SecurityOptions.OwnerPassword = "LEAD_SECRET";
pdfFile.SecurityOptions.PrintEnabled = false;
pdfFile.SecurityOptions.HighQualityPrintEnabled = false;
pdfFile.SecurityOptions.EncryptionMode = PDFEncryptionMode.RC128Bit;
// Encrypt the file by saving it to the destination file
pdfFile.Convert(1, -1, destinationFileName);
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";
}