CMMaung
CMMaung

Reputation: 165

How to open the password protected pdf using c#

I want to open password protected pdf inside c#.net windows application panel or with adobe reader.

My idea is I want to pass the password via system and open the pdf. Users can see the pdf but cannot save. Even users can do save us in their computer, when they re-open the pdf, the file will ask the password. Meaning that when they want to read the pdf, they must use the system and if they took the pdf to outside, they cannot open cos of the password.

I tried with Adobe Pdf dll, this one cannot pass the password. And I also tried with itextsharp, this one can pass the password but after pass password, need to save the pdf. So when I open the pdf, the file has no password.

I want to directly open the password protected pdf via system. I don't want to save again.

Upvotes: 2

Views: 17700

Answers (2)

mkl
mkl

Reputation: 96064

If I understand you correctly you want to enforce some digital rights management (DRM).

To be able to do that you need to have some control over the PDF viewing component or that component must be limited enough. Otherwise, i.e. if you just somehow forward the password to the Adobe Acrobat, that component assumes the user to be the actual owner of the document who, therefore, is allowed to save an unprotected copy of the file. And, of course, if you temporarily create an unprotected copy, e.g. using iText(Sharp), that temp file can be snatched away easily.

Unfortunately you didn't make clear whether the choice "inside c#.net windows application panel or with adobe reader" is up to you or up to the user.

Thus, if Adobe Acrobat/Reader need to be supported, you have to integrate the DRM enforsing into those products, cf. http://www.adobe.com/devnet/reader/topic_drm.html ("Developing digital rights management (DRM) plug-ins").

If, on the other hand, you have the choice, you merely need some appropriate PDF viewer component. Unfortunately I don't know the viewers available for .NET; if you used Java, JPedal could be customized appropriately.

Upvotes: 0

Ekk
Ekk

Reputation: 5715

There is a similar question how can a password-protected PDF file be opened programmatically? I copied some part of that question and put it here.

public static void unprotectPdf(string input, string output) 
{ 
    bool passwordProtected = PdfDocument.IsPasswordProtected(input); 
    if (passwordProtected) 
    { 
        string password = null; // retrieve the password somehow 

        using (PdfDocument doc = new PdfDocument(input, password)) 
        { 
            // clear both passwords in order 
            // to produce unprotected document 
            doc.OwnerPassword = ""; 
            doc.UserPassword = ""; 

            doc.Save(output); 
        } 
    } 
    else 
    { 
        // no decryption is required 
        File.Copy(input, output, true); 
    } 
} 

Upvotes: 5

Related Questions