简体   繁体   中英

Check for pdf contents to file to find the pdf is password protected

I am trying to find that the PDF is encrypted or not. I am facing issue while uploading my documents generated by our application. It works fine for the other PDF.

I figured out that this happens because my PDF file contents the Encrypt Metadata true, even my file is not password protected.

trailer
<</Size 49
/Root 46 0 R
/Encrypt 47 0 R
/ID [<544779292784d1082d90221fd2118106><544779292784d1082d90221fd2118106>]
/Info 48 0 R
>>
startxref
218840
%%EOF


<<
/Filter/Standard
/R 3 /V 2 /Length 128
/O<0a9c59beafa2ba093c4bace402aae8e14eacb78a9ab178187f5922be0f044f63>
/U<a1b38ac6f6fe4d59b099045b71b52d7328bf4e5e4e758a4164004e56fffa0108>
/P -1852/EncryptMetadata true
>>

Can someone help in finding how should i check that my PDF file is just Encrypt Metadata as true while it is not password protected.

You may differentiate between encrypted and password protected PDF files with Aspose.PDF for .NET API. PdfFileInfo class exposes some bool properties including IsEncrypted HasEditPassword and HasOpenPassword which can be checked to achieve your requirements. Below is a code snippet for your kind reference:

        // Load source PDF file
        PdfFileInfo info = new PdfFileInfo();
        info.BindPdf(dataDir + "Test.pdf");
        if (info.IsEncrypted)
        {
            // Determine if the source PDF is encrypted
            Console.WriteLine("File is encrypted");
        }
        if (info.HasEditPassword)
        {
            // Determine if the source PDF has edit password
            Console.WriteLine("File has edit password");
        }
        if (info.HasOpenPassword)
        {
            // Determine if the source PDF has open password
            Console.WriteLine("File has open password");
        }

We hope this will be helpful. Feel free to let us know if you need any further assistance.

PS : I work with Aspose as Developer Evangelist.

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