Protect Base64 PDF with a password using iText 7

Views: 17231
Comments: 1
Like/Unlike: 2
Posted On: 06-Sep-2019 23:10 

Share:   fb twitter linkedin
Smith
2914 Points
78 Posts

Using iText 7, we can define two passwords: a user password and an owner password. A pdf that is encrypted with an owner password can be opened by every one who receives the document. The owner password is there to define permissions (for instance: the document can be viewed, but not printed). If a pdf is encrypted using a user password, everybody who knows the user password can open the file. There is no username, only a user password.

Nuget Package:

Install-Package itext7 -Version 7.1.7

Import Following packages:

using iText.Kernel.Pdf;
using System;
using System.Text;
using System.IO;

Source Code

public static string EncryptPDFwithPassword(string pdfBase64, string passwordUser, string passwordOwner)
{
    var encryptedBase64 = string.Empty;
    var srcBytes = Convert.FromBase64String(pdfBase64);
    var userPassword = Encoding.ASCII.GetBytes(passwordUser);
    var ownerPassword = Encoding.ASCII.GetBytes(passwordOwner);

    PdfReader reader = new PdfReader(new MemoryStream(srcBytes));
    WriterProperties props = new WriterProperties()
            .SetStandardEncryption(userPassword, ownerPassword, EncryptionConstants.ALLOW_PRINTING,
                    EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA);

    using (var memoryStream = new MemoryStream())
    {
        PdfWriter writer = new PdfWriter(memoryStream, props);
        PdfDocument pdfDoc = new PdfDocument(reader, writer);
        pdfDoc.Close();
        encryptedBase64 = Convert.ToBase64String(memoryStream.ToArray());
    }

    return encryptedBase64;
}

Use it as:

EncryptPDFwithPassword("base64pdf-without-any-prefixes", "niceuser", "niceowner");

References:

https://www.nuget.org/packages/itext7/
https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-protect-pdf-username-and-password
https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-protect-already-existing-pdf-password
1 Comments

Yes, you're right— iText 7  allows you to set two types of passwords for a PDF file: a  user password  and an  owner password . These serve different purposes and are widely used for securing PDF documents programmatically.


 Understanding PDF Passwords with iText 7 


 User Password: 


  * Required to  open and view  the PDF.
  * Only users who know this password can access the content.
  * There is  no concept of a username , just a password.


 Owner Password: 


  * Used to  set permissions  like disallowing printing, editing, or copying.
  * Anyone can open the PDF, but their actions will be restricted according to the permissions set.
  * The owner password is  not prompted when opening the file , but needed if someone tries to change permissions or remove security.



 Example: Setting User and Owner Passwords using iText 7 (C#/.NET) 


Make sure you have the NuGet package installed:


```
Install-Package itext7 -Version 7.1.7
```


Here’s a sample code snippet:


```csharp
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Encryption;
using System.IO;


string src = "input.pdf";
string dest = "output_protected.pdf";


// User and Owner passwords
string userPassword = "user123";
string ownerPassword = "owner456";


PdfReader reader = new PdfReader(src);
PdfWriter writer = new PdfWriter(dest, new WriterProperties()
    .SetStandardEncryption(
        System.Text.Encoding.UTF8.GetBytes(userPassword),
        System.Text.Encoding.UTF8.GetBytes(ownerPassword),
        EncryptionConstants.ALLOW_PRINTING, // Permission example
        EncryptionConstants.ENCRYPTION_AES_128));


PdfDocument pdfDoc = new PdfDocument(reader, writer);
pdfDoc.Close();
```


This code encrypts the file with both passwords and allows only printing (other actions like editing and copying will be restricted).


---


 Alternative for Non-Programmers: SysTools PDF Locker 


If you don’t want to write code and prefer a user-friendly, GUI-based solution,  [SysTools PDF Locker](https://www.systoolsgroup.com/pdf-locker.html)  is a reliable desktop tool.


Key Features:


* Add both  user and owner passwords 
* Restrict printing, copying, editing, and extracting pages
* Bulk lock multiple PDF files
* Fully offline – no data uploaded
* Compatible with Windows OS


SysTools PDF Locker (https://www.systoolsgroup.com/pdf/lock/)  is ideal for users who need to secure PDF files without diving into development environments like iText or scripting.


---


 Summary


* Use  iText 7  if you're comfortable with C# and want full control programmatically.
* Use  SysTools PDF Locker  if you prefer a GUI-based, no-code solution for securing PDFs with password protection and permission settings.


Jan Hardin
26-Jun-2025 at 05:29
 Log In to Chat