Working with Security

22 Jun 202324 minutes to read

Essential PDF allows you to protect the PDF document using encryption and set permission to the PDF document operations like printing, editing, copy content etc. using user password and owner password. Two types of encryption algorithms are available

  1. Rivest Cipher 4 (RC4)
  2. Advanced Encryption Standard (AES)

Working with RC4 Encryption

You can encrypt PDF document by specifying the Algorithm property as RC4 through PdfEncryptionAlgorithm Enum and KeySize property as 40bit or 128bit through PdfEncryptionKeySize Enum in PdfSecurity class. The following code snippet illustrates how to encrypt the PDF document with the UserPassword.

User password: Prevents people from opening or viewing a PDF document. Once the User Password is set, to open the PDF document, Adobe Acrobat/Reader will prompt a user to enter this password. If it is not correct, the document will not open. By setting a PDF User password, you can secure the PDF document.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key128Bit;
security.Algorithm = PdfEncryptionAlgorithm.RC4;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted with RC4 128bit", font, brush, new PointF(0, 40));

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key128Bit;
security.Algorithm = PdfEncryptionAlgorithm.RC4;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted with RC4 128bit", font, brush, new PointF(0, 40));

//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the font.
Dim font As New PdfStandardFont(PdfFontFamily.TimesRoman, 20.0F, PdfFontStyle.Bold)
'Set the brush.
Dim brush As PdfBrush = PdfBrushes.Black

'Document security.
Dim security As PdfSecurity = document.Security
'Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key128Bit
security.Algorithm = PdfEncryptionAlgorithm.RC4
security.UserPassword = "password"
'Draw the text.
graphics.DrawString("Encrypted with RC4 128bit", font, brush, New PointF(0, 40))

'Save and close the document.
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

NOTE

While using both user and owner passwords, please specify different user and owner password while encrypting the PDF document for better security.

You can protect the PDF document from printing, editing, copying with the OwnerPassword by using the following code snippet.

Owner password: Sets PDF document restrictions, which can include printing, content copying, editing, page extracting, commenting, and more. Once the owner password is set, Acrobat will require this password to make any changes to the PDF document. It further secures the PDF document to set a PDF Owner Password.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key128Bit;
security.Algorithm = PdfEncryptionAlgorithm.RC4;
security.OwnerPassword = "syncfusion";
//It allows printing and accessibility copy content.
security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.AccessibilityCopyContent;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("This document is protected with owner password", font, brush, new PointF(0, 40));

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key128Bit;
security.Algorithm = PdfEncryptionAlgorithm.RC4;
security.OwnerPassword = "syncfusion";
//It allows printing and accessibility copy content.
security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.AccessibilityCopyContent;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("This document is protected with owner password", font, brush, new PointF(0, 40));

//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the font.
Dim font As New PdfStandardFont(PdfFontFamily.TimesRoman, 20.0F, PdfFontStyle.Bold)
'Set the brush.
Dim brush As PdfBrush = PdfBrushes.Black

'Document security.
Dim security As PdfSecurity = document.Security
'Specifies key size and encryption algorithm using 128 bit key in RC4 mode.
security.KeySize = PdfEncryptionKeySize.Key128Bit
security.Algorithm = PdfEncryptionAlgorithm.RC4
security.OwnerPassword = "syncfusion"
'It allows printing and accessibility copy content.
security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags.AccessibilityCopyContent
security.UserPassword = "password"
'Draw the text.
graphics.DrawString("This document is protected with owner password", font, brush, New PointF(0, 40))

'Save and close the document.
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Working with AES Encryption

You can encrypt PDF document by specifying the Algorithm property as AES through PdfEncryptionAlgorithm Enum and KeySize property as 40bit or 128bit or 256bit through PdfEncryptionKeySize Enum in PdfSecurity class. The following code snippet illustrates how to encrypt the PDF document with the UserPassword.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted with AES 256bit", font, brush, new PointF(0, 40));

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted with AES 256bit", font, brush, new PointF(0, 40));

//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the font.
Dim font As New PdfStandardFont(PdfFontFamily.TimesRoman, 20.0F, PdfFontStyle.Bold)
'Set the brush.
Dim brush As PdfBrush = PdfBrushes.Black

'Document security.
Dim security As PdfSecurity = document.Security
'Specifies key size and encryption algorithm using 256 bit key in AES mode.
security.KeySize = PdfEncryptionKeySize.Key256Bit
security.Algorithm = PdfEncryptionAlgorithm.AES
security.UserPassword = "password"
'Draw the text.
graphics.DrawString("Encrypted with AES 256bit", font, brush, New PointF(0, 40))

'Save and close the document.
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

You can protect the PDF document from printing, editing, copying with the OwnerPassword by using the following code snippet.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm using 256 bit key in AES mode.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
security.OwnerPassword = "syncfusion";
//It allows printing and accessibility copy content.
security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.AccessibilityCopyContent;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("This document is protected with owner password", font, brush, new PointF(0, 40));

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm using 256 bit key in AES mode.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
security.OwnerPassword = "syncfusion";
//It allows printing and accessibility copy content.
security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.AccessibilityCopyContent;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("This document is protected with owner password", font, brush, new PointF(0, 40));

//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the font.
Dim font As New PdfStandardFont(PdfFontFamily.TimesRoman, 20.0F, PdfFontStyle.Bold)
'Set the brush.
Dim brush As PdfBrush = PdfBrushes.Black

'Document security.
Dim security As PdfSecurity = document.Security
'Specifies key size and encryption algorithm using 256 bit key in RC4 mode.
security.KeySize = PdfEncryptionKeySize.Key256Bit
security.Algorithm = PdfEncryptionAlgorithm.AES
security.OwnerPassword = "syncfusion"
'It allows printing and accessibility copy content.
security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags.AccessibilityCopyContent
security.UserPassword = "password"
'Draw the text.
graphics.DrawString("This document is protected with owner password", font, brush, New PointF(0, 40))

'Save and close the document.
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Encryption Options

Now, the Syncfusion PDF library has provided options to encrypt the PDF document as follows:

  • Encrypt all contents - All contents of the document will be encrypted.

  • Encrypt all contents except Metadata - All contents of the document will be encrypted except metadata.

  • Encrypt only attachments - Encrypts only the file attachments, rest of the document will be left unencrypted.

The default value of EncryptionOptions is EncryptAllContents. You can choose any one of these options using the property EncryptionOptions available in the class PdfSecurity.

Encrypt all contents

You can encrypt all the PDF content by specifying the EncryptionOptions property as EncryptAllContents option through PdfEncryptionOptions Enum in PdfSecurity class. The following code snippet explains how to encrypt all contents of the PDF document.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContents;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted with AES 256bit", font, brush, new PointF(0, 40));                        

//Save the document into stream.
MemoryStream stream = new MemoryStream(); 
document.Save(stream); 
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContents;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted with AES 256bit", font, brush, new PointF(0, 40));

//Save and close the document
document.Save("Output.pdf");
document.Close();
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the font.
Dim font As New PdfStandardFont(PdfFontFamily.TimesRoman, 20.0F, PdfFontStyle.Bold)
'Set the brush.
Dim brush As PdfBrush = PdfBrushes.Black

'Document security.
Dim security As PdfSecurity = document.Security
'Specifies key size and encryption algorithm using 256 bit key in AES mode.
security.KeySize = PdfEncryptionKeySize.Key256Bit
security.Algorithm = PdfEncryptionAlgorithm.AES
'Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContents;
security.UserPassword = "password"
'Draw the text.
graphics.DrawString("Encrypted with AES 256bit", font, brush, New PointF(0, 40))        

'Save and close the document
document.Save("Output.pdf")
document.Close()

You can download a complete working sample from GitHub.

Encrypt all contents except metadata

The Syncfusion Essential PDF library now supports encrypting the PDF document except the document information (metadata) by using the EncryptAllContentsExceptMetadata option. The document information will not be encrypted when using this EncryptionOption.

You can encrypt all contents except metadata by specifying the EncryptionOptions property as EncryptAllContentsExceptMetadata option through PdfEncryptionOptions Enum in PdfSecurity class. The following code snippet explains how to encrypt all contents except metadata of the PDF document.

NOTE

Encrypt all contents except metadata is only supported in AES algorithms with 128bit, 256bit, and 256bit revision6 key size.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContentsExceptMetadata;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted all contents except metadata with AES 256bit", font, brush, new PointF(0, 40));                        

//Save the document into stream.
MemoryStream stream = new MemoryStream(); 
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContentsExceptMetadata;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted all contents except metadata with AES 256bit", font, brush, new PointF(0, 40));

//Save and close the document.
document.Save("Output.pdf");
document.Close();
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the font.
Dim font As New PdfStandardFont(PdfFontFamily.TimesRoman, 20.0F, PdfFontStyle.Bold)
'Set the brush.
Dim brush As PdfBrush = PdfBrushes.Black

'Document security.
Dim security As PdfSecurity = document.Security
'Specifies key size and encryption algorithm using 256 bit key in AES mode.
security.KeySize = PdfEncryptionKeySize.Key256Bit
security.Algorithm = PdfEncryptionAlgorithm.AES
'Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContentsExceptMetadata
security.UserPassword = "password"
'Draw the text.
graphics.DrawString("Encrypted all contents except metadata with AES 256bit", font, brush, New PointF(0, 40))        

'Save and close the document.
document.Save("Output.pdf"
document.Close()

You can download a complete working sample from GitHub.

Encrypt only attachments

You can encrypt only attachments present in the PDF document by specifying the EncryptionOptions property as EncryptOnlyAttachments option through PdfEncryptionOptions Enum in PdfSecurity class.

The following code example explains how to create an encrypt only attachment document using the Syncfusion PDF Library.

NOTE

UserPassword is mandatory for encrypt only attachments and it is only supported in AES algorithms with 128bit, 256bit, and 256bit revision6 key size.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted only attachments with AES 256bit", font, brush, new PointF(0, 40));

//Creates an attachment.
PdfAttachment attachment = new PdfAttachment("Input.txt");
attachment.ModificationDate = DateTime.Now;
attachment.Description = "Input.txt";
attachment.MimeType = "application/txt";
//Add the attachment to the document.
document.Attachments.Add(attachment);

//Save the document into stream.
MemoryStream stream = new MemoryStream(); 
document.Save(stream); 
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
//Set the brush.
PdfBrush brush = PdfBrushes.Black;

//Document security.
PdfSecurity security = document.Security;
//Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;
security.UserPassword = "password";
//Draw the text.
graphics.DrawString("Encrypted only attachments with AES 256bit", font, brush, new PointF(0, 40));

//Creates an attachment.
PdfAttachment attachment = new PdfAttachment("Input.txt");
attachment.ModificationDate = DateTime.Now;
attachment.Description = "Input.txt";
attachment.MimeType = "application/txt";
//Add the attachment to the document.
document.Attachments.Add(attachment);

//Save and close the document.
document.Save("Output.pdf");
document.Close();
'Create a new PDF document.      
Dim document As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the font.
Dim font As New PdfStandardFont(PdfFontFamily.TimesRoman, 20.0F, PdfFontStyle.Bold)
'Set the brush.
Dim brush As PdfBrush = PdfBrushes.Black

'Document security.
Dim security As PdfSecurity = document.Security
'Specifies key size and encryption algorithm using 256 bit key in AES mode.
security.KeySize = PdfEncryptionKeySize.Key256Bit
security.Algorithm = PdfEncryptionAlgorithm.AES
'Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;
security.UserPassword = "password"
'Draw the text.
graphics.DrawString("Encrypted only attachments with AES 256bit", font, brush, New PointF(0, 40))

'Creates an attachment.
Dim attachment As New PdfAttachment("Input.txt")
attachment.ModificationDate = DateTime.Now
attachment.Description = "Input.txt" 
attachment.MimeType = "application/txt" 
'Add the attachment to the document.
document.Attachments.Add(attachment)

'Save and close the document.
document.Save("Output.pdf")
document.Close()

You can download a complete working sample from GitHub.

Opening an encrypt-only-attachment document

The Syncfusion Essential PDF library now provides support for loading the encrypt-only-attachment PDF documents. To access the attachments in the existing PDF document, the UserPassword is mandatory.

You can provide the UserPassword in following ways:

  • Load the PDF document with password.
  • Provide password using the OnPdfPassword Event when accessing the attachments.

It is possible to access all the contents except attachment when loading the PDF document without UserPassword.

The following code example explains how to load an encrypt-only-attachment document with password in PdfAttachment class using Syncfusion PDF Library.

//Load the PDF document.            
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read); 
PdfLoadedDocument document = new PdfLoadedDocument(docStream,"password");

//Accessing the attachments.
foreach(PdfAttachment attachment in document.Attachments)
{
  FileStream  stream = new FileStream(attachment.FileName, FileMode.Create);
  stream.Write(attachment.Data, 0, attachment.Data.Length);
  stream.Dispose();
}                      

//Close the document.
document.Close(true);
//Load the PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf","password");
            
//Accessing the attachments.
foreach(PdfAttachment attachment in document.Attachments)
{
  FileStream stream = new FileStream(attachment.FileName, FileMode.Create);
  stream.Write(attachment.Data, 0, attachment.Data.Length);
  stream.Dispose();
}         

//Close the document.
document.Close(true);
'Load the PDF document.
Dim document As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf", "password")
             
'Accessing the attachments.
For Each attachment As PdfAttachment In document.Attachments
  Dim stream = New FileStream(attachment.FileName, FileMode.Create)
  stream.Write(attachment.Data, 0, attachment.Data.Length)
  stream.Dispose
Next

'Close the document.
document.Close(true)

You can download a complete working sample from GitHub.

Set user password using event when accessing the attachment

The following code example illustrates how to provide the password when accessing attachments from encrypt-only-attachment document using OnPdfPasswordEventArgs class.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read); 
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
document.OnPdfPassword += LDoc_OnPdfPassword;

//Accessing the attachments.
foreach(PdfAttachment  attachment in document.Attachments)
{
  FileStream  stream= new FileStream(attachment.FileName, FileMode.Create);
  stream.Write(attachment.Data, 0, attachment.Data.Length);
  stream.Dispose();
}                      

//Close the document.
document.Close(true);
	
//Provide the user password in event.
private static void LDoc_OnPdfPassword(object sender, OnPdfPasswordEventArgs args)
{
  args.UserPassword = "syncfusion";
}
//Load the PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
document.OnPdfPassword += LDoc_OnPdfPassword;

//Accessing the attachments.
foreach(PdfAttachment attachment in document.Attachments)
{
  FileStream stream= new FileStream(attachment.FileName, FileMode.Create);
  stream.Write(attachment.Data, 0, attachment.Data.Length);
  stream.Dispose();
}                      

//Close the document.
document.Close(true);
	
//Provide the user password in event.
private static void LDoc_OnPdfPassword(object sender, OnPdfPasswordEventArgs args)
{
  args.UserPassword = "syncfusion";
}
'Load the PDF document.         
Dim document As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
AddHandler document.OnPdfPassword, AddressOf LDoc_OnPdfPassword

'Accessing the attachments.
For Each attachment As PdfAttachment In document.Attachments
  Dim stream As FileStream = New FileStream(attachment.FileName, FileMode.Create)
  stream.Write(attachment.Data, 0, attachment.Data.Length)
  stream.Dispose()
Next

'Close the document.
document.Close(True)

'Provide the user password in event.
Private Sub LDoc_OnPdfPassword(ByVal sender As Object, ByVal args As OnPdfPasswordEventArgs)
  args.UserPassword = "password"
End Sub

You can download a complete working sample from GitHub.

Protect attachments in existing PDF document

The Syncfusion PDF Library supports encrypting only the attachment files in an existing PDF document by specifying the EncryptionOptions property as EncryptOnlyAttachments option through PdfEncryptionOptions Enum in PdfSecurity class. Refer to the following code snippet.

NOTE

UserPassword is mandatory for this encryption option.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read); 
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

//PDF document security.
PdfSecurity security = document.Security; 
//Specifies encryption key size, algorithm and permission.
security.KeySize = PdfEncryptionKeySize.Key256Bit; 
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Provide user password.
security.UserPassword = "password";
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;

//Save the document into stream.
MemoryStream stream = new MemoryStream(); 
document.Save(stream); 
//Close the document.
document.Close(true);
//Load the PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");

//PDF document security.
PdfSecurity security = document.Security; 
//Specifies encryption key size, algorithm and permission.
security.KeySize = PdfEncryptionKeySize.Key256Bit; 
security.Algorithm = PdfEncryptionAlgorithm.AES; 
//Provide user password.
security.UserPassword = "password";
//Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;

//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Load the PDF document.        
Dim document As New PdfLoadedDocument("Input.pdf")

'PDF document security.
Dim security As PdfSecurity = document.Security
'Specifies encryption key size, algorithm and permission.
security.KeySize = PdfEncryptionKeySize.Key256Bit
security.Algorithm = PdfEncryptionAlgorithm.AES
'Provide user password.
security.UserPassword = "password"
'Specifies encryption option.
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments

'Save the document.
document.Save("Output.pdf")
'Close the document
document.Close(true)

You can download a complete working sample from GitHub.

Protect an existing document

You can protect an existing PDF document with both UserPassword and OwnerPassword by using the following code snippet.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

//PDF document security.
PdfSecurity security = document.Security;
//Specifies encryption key size, algorithm and permission. 
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Provide owner and user password.
security.OwnerPassword = "ownerPassword256";
security.UserPassword = "userPassword256";

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
//Load the PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");

//PDF document security.
PdfSecurity security = document.Security;
//Specifies encryption key size, algorithm and permission.
security.KeySize = PdfEncryptionKeySize.Key256Bit;
security.Algorithm = PdfEncryptionAlgorithm.AES;
//Provide owner and user password.
security.OwnerPassword = "ownerPassword256";
security.UserPassword = "userPassword256";

//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Load an existing document.
Dim document As New PdfLoadedDocument("Input.pdf")

'PDF Document Security.
Dim security As PdfSecurity = document.Security
'Specifies key size and encryption algorithm.
security.KeySize = PdfEncryptionKeySize.Key128Bit
security.Algorithm = PdfEncryptionAlgorithm.RC4
'Provide owner and user password.
security.OwnerPassword = "ownerPassword256"
security.UserPassword = "userPassword256"

'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)

You can download a complete working sample from GitHub.

Changing the password of the PDF document

You can change the UserPassword of the existing PDF document by using following code snippet.

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream, "password");
//Change the user password 
loadedDocument.Security.UserPassword = "NewPassword";

//Save the document into stream
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the document
loadedDocument.Close(true);
//Load the password protected PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf","password");
//Change the user password.
loadedDocument.Security.UserPassword = "NewPassword";

//Save the password changed PDF document.
loadedDocument.Save("Output.pdf");
//Close the document.
loadedDocument.Close(true);
'Load the password protected PDF document
Dim loadedDocument As New PdfLoadedDocument("Input.pdf", "password")
'Change the user password 
loadedDocument.Security.UserPassword = "NewPassword"

'Save the password changed PDF document
loadedDocument.Save("Output.pdf")
'Close the document
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Change the permission of the PDF document

You can change the permission of the PDF document using the Permissions. The following code snippet illustrates the same.

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream, "syncfusion");
//Change the permission
loadedDocument.Security.Permissions = PdfPermissionsFlags.CopyContent | PdfPermissionsFlags.AssembleDocument;

//Save the document into stream
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the PDF document
loadedDocument.Close(true);
//Load the password protected PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf", "syncfusion");
//Change the permission
loadedDocument.Security.Permissions = PdfPermissionsFlags.CopyContent | PdfPermissionsFlags.AssembleDocument;

//Save and Close the PDF document
loadedDocument.Save("Output.pdf");
loadedDocument.Close(true);
'Load the password protected PDF document
Dim loadedDocument As New PdfLoadedDocument("Input.pdf", "syncfusion")
'Change the permission
loadedDocument.Security.Permissions = PdfPermissionsFlags.CopyContent Or PdfPermissionsFlags.AssembleDocument

'Save and Close the PDF document
loadedDocument.Save("Output.pdf")
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Remove password from the user password PDF document

You can remove the UserPassword from the encrypted PDF document by using the following code snippet.

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream, "password");
//Change the user password
loadedDocument.Security.UserPassword = string.Empty;

//Save the document into stream
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the document
loadedDocument.Close(true);
//Load the password protected PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf","password");
//Change the user password 
loadedDocument.Security.UserPassword = string.Empty;

//Save the password removed PDF document
loadedDocument.Save("Output.pdf");
//Close the document
loadedDocument.Close(true);
'Load the password protected PDF document
Dim loadedDocument As New PdfLoadedDocument("Input.pdf", "password")
'Change the user password 
loadedDocument.Security.UserPassword = String.Empty

'Save the password removed PDF document
loadedDocument.Save("Output.pdf")
'Close the document
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

How to determine whether the PDF document is password protected or not?

You can determine whether the existing PDF document is password protected or not by catching the PdfDocumentException as shown below.

try
{
  //Load the PDF document
  FileStream docStream = new FileStream("Output.pdf", FileMode.Open, FileAccess.Read);
  PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
}
catch (PdfDocumentException exception)
{
  if (exception.Message == "Can't open an encrypted document. The password is invalid.")
	{
		MessageBox.Show("Cannot open an encrypted document without password");
	}
}
try
{
	//Load the password protected PDF document without user password
	PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Output.pdf");
}
catch (PdfDocumentException exception)
{
	if (exception.Message == "Can't open an encrypted document. The password is invalid.")
	{
		MessageBox.Show("Cannot open an encrypted document without password");
	}
}
Try
	'Load the password protected PDF document without user password
	Dim loadedDocument As New PdfLoadedDocument("Output.pdf")
Catch exception As PdfDocumentException
	If exception.Message = "Can't open an encrypted document. The password is invalid." Then
		MessageBox.Show("Cannot open an encrypted document without password")
	End If
End Try

You can download a complete working sample from GitHub.

How to determine whether the PDF document is protected by user or owner password

Essential PDF supports identifying the document whether it is protected by user or owner.

The following table shows the various combination for loading the secured document with user or owner password:

Document type Open with User password Owner password
PDF document secured with both the owner and user passwords. User password Returns user password Returns null
PDF document secured with both the owner and user passwords. Owner password Returns user password

Note: Returns null for AES 256 and AES 256 Revision 6 encryptions.
Returns owner password
PDF document secured with owner password alone. Owner password Returns null Returns owner password
PDF document secured with user password alone. User Password Returns user password Returns owner Password (owner password is same as the user password; it allows full permission to users).