T Tim Chapman Active Member Licensed User Longtime User Jul 20, 2024 #1 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.
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 Daestrum Jul 21, 2024 Assuming the document is loaded with something like Code: Copy pdf.Initialize("c:/temp/pdftest1.pdf") You could try (uses JavaObject library) Code: Copy 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.
Assuming the document is loaded with something like Code: Copy pdf.Initialize("c:/temp/pdftest1.pdf") You could try (uses JavaObject library) Code: Copy 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 Jul 20, 2024 #2 You can use the property NumberOfPages. 0 the document is encrypted Last edited: Jul 20, 2024 Upvote 0
T Tim Chapman Active Member Licensed User Longtime User Jul 20, 2024 #3 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: Jul 20, 2024 Upvote 0
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.
drgottjr Expert Licensed User Longtime User Jul 20, 2024 #4 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
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.
teddybear Well-Known Member Licensed User Jul 21, 2024 #5 Tim Chapman said: 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. Click to expand... Could you provide an encrypted sample for test? here it returns 0 using the encrypted sample. Attachments sample.pdf sample.pdf 43.1 KB · Views: 307 Upvote 0
Tim Chapman said: 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. Click to expand... Could you provide an encrypted sample for test? here it returns 0 using the encrypted sample.
T Tim Chapman Active Member Licensed User Longtime User Jul 21, 2024 #6 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
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.
T Tim Chapman Active Member Licensed User Longtime User Jul 21, 2024 #7 How do I parse the last exception message? Upvote 0
teddybear Well-Known Member Licensed User Jul 21, 2024 #8 Tim Chapman said: 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. Click to expand... Try...catch can't catch the log INFO: Document is encrypted, because it does not throw an exception at all. Tim Chapman said: How do I parse the last exception message? Click to expand... 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
Tim Chapman said: 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. Click to expand... Try...catch can't catch the log INFO: Document is encrypted, because it does not throw an exception at all. Tim Chapman said: How do I parse the last exception message? Click to expand... 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?
Daestrum Expert Licensed User Longtime User Jul 21, 2024 #9 Assuming the document is loaded with something like Code: Copy pdf.Initialize("c:/temp/pdftest1.pdf") You could try (uses JavaObject library) Code: Copy 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: Jul 21, 2024 Upvote 0 Solution
Assuming the document is loaded with something like Code: Copy pdf.Initialize("c:/temp/pdftest1.pdf") You could try (uses JavaObject library) Code: Copy 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.
T Tim Chapman Active Member Licensed User Longtime User Jul 21, 2024 #10 That works great. Thank you all for the help! Upvote 0