ZingPDF logo

Guide

How to remove a password from a PDF in C#

Decrypting the current revision is enough when you only need a plain copy. If the file has incremental history and you need a rewritten output, call RemoveHistoryAsync() before saving.

If you have the password and want a plain copy, call DecryptAsync(...) and save. Add RemoveHistoryAsync() when you want the output rewritten without older encrypted revisions.

The shortest working example

If you just need to open a password-protected PDF and save an unprotected copy, this is the core path.

using ZingPDF;

using var input = File.OpenRead("protected.pdf");
using var pdf = Pdf.Load(input);

await pdf.DecryptAsync("password");
await pdf.SaveAsync(File.Create("unprotected.pdf"));

This removes encryption from the latest saved revision and writes a plain output file. For many internal exports and one-off conversions, that is all you need.

When you should also remove history

PDF files often contain incremental revision history. In practice, that means a document can have older byte ranges representing previous states of the file, with newer revisions appended on top.

That matters because removing encryption from the latest revision does not automatically remove every older encrypted revision from the saved file. If you need a file that contains only the latest live objects, rewrite it.

using ZingPDF;

using var input = File.OpenRead("protected.pdf");
using var output = File.Create("unprotected-clean.pdf");
using var pdf = Pdf.Load(input);

await pdf.DecryptAsync("password");
await pdf.RemoveHistoryAsync();
await pdf.SaveAsync(output);

Use that version when the output is going to another system, going into storage, or being handed off as the final file.

Why a saved file can still carry older encrypted bytes

PDF files can be saved incrementally. That means the file may contain older byte ranges for earlier states of the document, with newer revisions appended afterward.

That is why "decrypt and save" is only half the answer. You also need to decide whether you are removing protection from the latest revision only, or writing a file that no longer contains older revisions at all.

In ZingPDF, that decision maps cleanly to the API. Use DecryptAsync(...) to remove encryption from the latest revision. Add RemoveHistoryAsync() when you need the output rewritten without earlier revision history.

Authenticate vs decrypt

If you only need to read or edit the document while keeping it protected, use AuthenticateAsync(...). That tells ZingPDF to open the encrypted file with the supplied password, but it does not turn the saved output into a plain PDF.

using var pdf = Pdf.Load(File.OpenRead("protected.pdf"));

await pdf.AuthenticateAsync("password");
var pageCount = await pdf.GetPageCountAsync();

Use DecryptAsync(...) only when your intended output is an unprotected document.

Stream requirements to remember

The save model has a few stream requirements.

  • The input stream passed to Pdf.Load(...) must be seekable.
  • The output stream passed to SaveAsync(...) must be writable and seekable.
  • If you save to a different stream, that stream must be empty.
  • If you save back to the original stream, ZingPDF appends a new incremental revision unless you remove history first.

Test it against one of your protected files

Test it against one of your own password-protected PDFs and decide whether you want a latest-revision decrypt or a full rewrite without older history.

If the next step after opening a protected file is updating fields, read How to fill PDF form fields in C#.

Open encryption docs View pricing