INFO: Password Protected PDFs in PdfDocument and PdfGeneratedDocument


Background

We've had a case where password protected PDFs are being used and there's a bit of a confusion as to how exactly it's done.

API Looks Like

It would appear that the API would say that an owner password can be set but no user password

However, when you do that the issue is the PDF is not actually locked out at all - you're never prompted for the full thing.

How it works

In order to set a password you set a user password. When you do then the security protections apply. If you want to allow for opening up more you can then also set a different owner password which overrides the perms

User PW Only

If you set only a user password, then user password becomes full access and pretty much ignores any security

If you go to the properties of the document while open in acro reader and look up security you will see password security set

For the document security you will basically see all rights

Effective state: PW required for any access, when given is full (sort of acts as if "owner only" in terms of how that feels)

User and Owner PW

If you set a user and owner password, then the user gets the restricted security and owner gets full access.

When you open it as user you will get the restricted permission set which shows in acro readers properties -> Security under document security

If you open it as Owner then you will still se the security set for the USER under the security details but document security shows default

Owner PW Only

If you set an owner password with no user password, ALL SECURITY IS IGNORED

In summary

If the document has user only password, then essentially its a full unlock

If the document has user and owner passwords then the user one gives restricted perms but owner unlocks fully

If the document has only owner password, all security is ignored

Advanced Topic - Blank Password

All of what we said above is when you provide null / Nothing (cs/vb respectively) as one of the user or owner passwords (or both or don't set them). When you set an empty string as user password, things get interesting...

Empty String as User Password

When you have an empty string for user password and an owner password is set, you get a PDF that opens fine in Acrobat Reader without ever prompting for a password, but has restrictions based on whatever security was set.

Such a PDF may open OK in PdfDocument but will error on saving if used with the user password.

To override that in PdfDocument, you need to supply the owner password.

Empty String as Owner AND User Password

If you set empty strings for owner and user passwords, then try and open in PdfDocument:

If you provide only one password of empty string on open, it will treat it as if you gave only the user password and thus restrict you from saving.

If you provide both passwords (even if both are blank empty strings) then it will treat it as if you've provided the owner password.

Advanced topic - Stripping security

Any time you open a PDF successfully in PdfDocument with owner permissions (either by providing the owner password or if it only has user password and you supply that), then the opened document has effectively stripped security / passwords.

If you then save it out, the resulting save will not have any password or security unless you explicitly set them via PdfSaveOptions.SetOneTimePasswords(...)

Opening a PDF that has User only PW

If you successfully open a PDF with user only protection (so functionally you're owner) then when saving out, you need to apply any security you want to "stick" otherwise it effectively strips PdfDocument doc = PdfDocument(stream, secureUserPw);

Opening a PDF that has User and owner PW

You are free to use PdfDocument doc = new PdfDocument(stream, secureUserPw, secureOwnerPw);

however, the truth is you can actually just provide the owner password You are free to use PdfDocument doc = PdfDocument(stream, secureOwnerPw);

This is because if you successfully provide the owner password, then the PDF is unlocked as described above

This is true even though our API Reference calls that first slot "userPassword", this is treated as the master unlock

When saving, if you do not set passwords, then effectively, it strips all security

NOTE
If you open such a pdf that has user AND owner, then try and do something that the user doesn't have permissions for, it will not let you save. You won't get a 'wrong password', you'll get an error clearly stating saving isn't allowed'

When you have both a user and owner password set, it will open in PdfDocument but will error on saving if you did not provide the owner password either along with user password, or as the only password provided

TL;DR: To override that in PdfDocument, you need to supply the owner password.

FINAL THOUGHTS

  • There is no scenario where you can have a null user password and an owner password set.
  • You can have an empty password for a user password set, but it will then not afford acrobat reader users a way to unlock the advanced perms
  • however, the owner password will then be required in order to open the PDF in PdfDocument
  • everything above applies equally to PdfGeneratedDocument