Forums

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Generator for COM/ActiveX (v8.x)  /  test if pdf is encrypted

Forum Thread - test if pdf is encrypted

 Jan 15 2017 8:10 AM
I have a form that asks the user to upload a PDF, but if it's a password protected PDF the uploader says there is no file in the form.

(The IIS logs say: Document_can_not_be_decrypted.)

I am already using DynamicPDF to check the page number, is there a way that I can also check to see if the PDF is password protected, at the same time?
 Jan 16 2017 11:59 AM
Posted by a ceTe Software moderator
Hello,

Yes, you are getting error because of loading the secured/encrypted PDF without providing the correct owner password.

You can check whether the PDF is secured or not using SecurityInfo property of PdfDocument class. You will need to load the input PDF using PdfDocument class and then use this property to get the security information. You can refer to the documentation on PdfDocument class here and documentation  on SecurityInfo property here.

Thanks,
ceTe Software Support Team.
 Jan 17 2017 3:02 PM
I am confused, how do you check the security if you cannot load the PDF?

The new error when using DynamicPDF.PdfDocument trying to LoadPDF, "Please_pass_a_proper_file_path_or_Byte_Array_or_if_encrypted_pass_proper_Password"

using the documentation example
 Dim Pdf
 Set Pdf = Server.CreateObject( "DynamicPdf.PdfDocument" )
 Pdf.LoadPdf c:\temp\somepdf.pdf"
but that LoadPdf is where it fails, so I can not check the "DPDF_SecurityInfo", unless I am just not understanding something.
 Jan 17 2017 5:01 PM
Posted by a ceTe Software moderator
Hello,

Sorry for the confusion. The solution we provided earlier to check for security is for protected/encrypted PDFs that do not require an owner password (there are PDFs out there that are encrypted without an owner password).

In your case the protected PDFs have an actual password, and you are right in that without the password you won’t be able to call the LoadPdf without getting an exception. The only solution for this case is to use the On Error Resume Next statement in ASP to catch and handle the error appropriately.

        Dim MyDocument
        Set MyDocument = Server.CreateObject( "DynamicPDF.Document" )
        
        On Error Resume Next
        
        Dim MyPdfDocument
        Set MyPdfDocument = Server.CreateObject( "DynamicPDF.PdfDocument" )
        MyPdfDocument.LoadPdf Server.MapPath( "./Encrypt.pdf")
        
        If Err.Number <> 0 Then
             'catch and handle the error appropriately
                Response.Write "Error occurred loading pdf: " & Err.Description
             Response.End
        End If
        
        MyDocument.AppendPdf MyPdfDocument
        MyDocument.DrawToWeb
        Set MyDocument = Nothing


Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 7:45 AM.