B4J Question Encrypted PDF file

Tim Chapman

Active Member
Licensed User
Longtime User
I used the code here to convert a pdf file to text: https://www.b4x.com/android/forum/threads/read-a-text-in-file-pdf.61945/

It works beautifully for a normal non-encrypted pdf file.

For an encrypted file, it puts the following in the log portion of the IDE:
INFO: Document is encrypted

I want my code to capture that line so that my code can be aware that the file is encrypted.
Is there any way to capture that log entry?

Thank you in advance for the assistance.
 
Solution
Assuming the document is loaded with something like
B4X:
pdf.Initialize("c:/temp/pdftest1.pdf")

You could try (uses JavaObject library)
B4X:
Log(pdf.As(JavaObject).RunMethod("isEncrypted",Null))
'or
If pdf.As(JavaObject).RunMethod("isEncrypted",Null) Then
    Log("This file is Encrypted")
End If

I tested it with a clean and an encrypted pdf.
Encrypted shows True, whilst clean shows False.

teddybear

Well-Known Member
Licensed User
You can use the property NumberOfPages. 0 the document is encrypted
 
Last edited:
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
It shows 412 pages even though it is encrypted. It gives the correct page count.
The code gives a java.io.IOException error on encrypted files. I will research trapping errors. I think this will solve the problem.
Thank you for the reply.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you would use a try/catch block. exactly where to insert it would depend on
what you've coded already.

an i/o exception doesn't necessarily only mean an excrypted file; you would have
to parse the lastexception message captured in the catch part of the try/catch block.
clearly, any i/o exception would cause an abort, but the lastexception message should
confirm the exact nature of the i/o exception. a lot depends on how the underlying code
in the conversion routine was written.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
It shows 412 pages even though it is encrypted. It gives the correct page count.
The code gives a java.io.IOException error on encrypted files. I will research trapping errors. I think this will solve the problem.
Thank you for the reply.
Could you provide an encrypted sample for test? here it returns 0 using the encrypted sample.
 

Attachments

  • sample.pdf
    43.1 KB · Views: 69
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
When I purchase a pdf file online from drivethrurpg.com they come with restrictions.
They do not require a password to open them.
But you can't save them as text files.
Those files are the ones being flagged as encrypted. I don't have any that require a password.
My try...catch code is catching the pdf files that have this protection.
This is sufficient for my purposes at the moment.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
When I purchase a pdf file online from drivethrurpg.com they come with restrictions.
They do not require a password to open them.
But you can't save them as text files.
Those files are the ones being flagged as encrypted. I don't have any that require a password.
My try...catch code is catching the pdf files that have this protection.
This is sufficient for my purposes at the moment.
Try...catch can't catch the log INFO: Document is encrypted, because it does not throw an exception at all.
How do I parse the last exception message?
It is not useful to parse the last exception message. the exception was caused by forcibly unzip encrypted pdf

I'm not quite sure what your intention is?
decrypt the pdf and extract text, or only test if it is encrypted?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Assuming the document is loaded with something like
B4X:
pdf.Initialize("c:/temp/pdftest1.pdf")

You could try (uses JavaObject library)
B4X:
Log(pdf.As(JavaObject).RunMethod("isEncrypted",Null))
'or
If pdf.As(JavaObject).RunMethod("isEncrypted",Null) Then
    Log("This file is Encrypted")
End If

I tested it with a clean and an encrypted pdf.
Encrypted shows True, whilst clean shows False.
 
Last edited:
Upvote 0
Solution
Top