简体   繁体   中英

How to open a password protected PDF using VB6/VB.NET?

I want to open and view a password protected PDF file in VB6/VB.NET program. I have tried using the Acrobat PDF Library but could not do it.

The reason I want to create a password protected PDF file is because I dont want the PDF file to be opened without the password externally ie outside the program.

To open a password protected PDF you will need to develop at least a PDF parser, decryptor and generator. I wouldn't recommend to do that, though. It's nowhere near an easy task to accomplish.

With help of a PDF library everything is much simpler. You might want to try Docotic.Pdf library for the task.

Here is a sample for you task:

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);
    }
}

Docotic.Pdf can also extract text (formatted or not) from PDFs. It might be useful for indexing (I guess it's what you are up to because you mentioned Adobe IFilter)

you can convert code to vb over the internet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM