Home : Atalasoft.PdfDoc.IncorrectPasswordException When Opening PDF in PdfDocument, PdfGeneratedDocument, some Other Classes
Q10490 - ERRMSG: Atalasoft.PdfDoc.IncorrectPasswordException When Opening PDF in PdfDocument, PdfGeneratedDocument, some Other Classes

Summary

The PDF spec actually has two passwords possible for each PDF:
Owner Password
User Password

The Owner password allows full access to the entire PDF as if there were no security set at all

The User Password allows access to the PDF for items that the owner/security has set up to allow to users.

It is common to have a secured PDF where there's no user password (so anyone can open to view under the restrictions of the security settings) but to access the full pdf (including adjusting the security settings or doing things the security settings have disallowed) you need the Owner password

Our PdfDecoder class is only a viewer.. it acts in the realm of the User setting.. if a PDF has no user password set, the PdfDecoder can open it without error even if the full PDF has an owner password.

However, Our classes that need to be able to modify a PDF (PdfDocument, PdfGeneratedDocument, PdfAnnotationDataExporter, PdfAnnotationDataImporter (and classes such as DocumentAnnotationViewer which utilize the annotation importer/exporter) - these classes must be given the owner password and user password if the owner password is set.

We'll say it again as this is critical:

If a PDF is password protected, you must provide both the user and the owner passwords in the constructor in order to open it in Pdf PdfDocument, PdfGeneratedDocument, PdfAnnotationDataExporter, PdfAnnotationDataImporter (and classes such as DocumentAnnotationViewer which utilize the annotation importer/exporter

How To Open Protected PDFs

The constructors for each of the classes in question has properties for the passwords...

NOTE: This is not optional. We simply can not see into the PDF to be able to fully function in those classes without the full owner level permission.

If you do not have the owner password for a PDF you can not use those classes on it.

The best thing to do is to acquire the owner password from the creator of the PDF or ask them to create one without the owner password.

PdfDocument

PdfDocument doc = new PdfDocument("userPassHere", "ownerPassHere", streamOfPDfHere, null, new RepairOptions());

PdfDocument doc = new PdfDocument("userPassHere", "ownerPassHere", "pdfFileNameHere.pdf, new RepairOptions());

PdfGeneratedDocument

PdfGeneratedDocument genDoc = new PdfGeneratedDocument("userPassHere", "ownerPassHere", streamOfPDfHere, BooleanAttemptToImportOrNotHere, null, new RepairOptions());

PdfAnnotationDataImporter

PdfAnnotationDataImporter importer = new PdfAnnotationDataImporter(streamOfPDfHere,"userPassHere", "ownerPassHere");

PdfAnnotationDataExporter

PdfAnnotationDataExporter exporter = new PdfAnnotationDataExporter("userPassHere", "ownerPassHere");

Workaround (destructive) of Last Resort

The question often comes up "but I can read the PDF just fine in Acrobat Reader or the Atalasoft PdfDecoder, why can those see it fine?"
Again, the answer is that to read /view, the PDF may not have a user password set and the viewing is usually unrestricted so that works fine

This does lead to the one passible workaround...

Atalasoft is not a PDF security tool, we do not provide "password breaking" tools, but if the PDF is viewable without a user password and you absolutely must get access to it in PdfDocument for combining or other operations, there's a destructive (see below) but possible workaround

You can use our PdfImageSource class to rasterize the PDF as image base and make a new PDF from what you could view in the original. This new PDF has no security set so you can then open/use it normally in our other classes.

As for the warning about "destructive"... If you do this to a PDF it can/will do the following

  • Lose any Metadata in the original PDF
  • "Flatten" (or remove depending on annotation render settings) any PDF fillable forms
  • "Burn"/"Flatten" (or remove depending on annotation render settings) any visible annotations
  • Destroy any searchable text embedded in the PDF
  • Render any vector/markup based content as images.. (it rasterizes each page as an image of the page converting the PDF to a fully image based PDF possibly increasing file size quite a bit depending on original PDF content)

If you're OK with all these things then you have a workaround:

Here, we will assume you have
Stream yourPdfStream;

which contains a random access stream such as FileStream or MemoryStream with the PDF in question

// always "rewind" before use
yourPdfStream.Seek(0, SeekOrigin.Begin);

// this is where we will store the output/result
MemoryStream unlockedPdfStream = new MemoryStream();
using (PdfImageSource pdfis = new PdfImageSource(yourPdfStream) { Resolution = 200 })
{
    PdfEncoder enc = new PdfEncoder();
    enc.SizeMode = PdfPageSizeMode.FitToPage;
    enc.Save(unlockedPdfStream, pdfis, null);
}

// the out stream wil be at the end so rewind it to be able to use it elsewhere
unlockedPdfStream.Seek(0, SeekOrigin.Begin);

You could easily use a FileStream for your unlockedPdfStream if you prefer...

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2019. InstantASP Ltd. All Rights Reserved