• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

accessing password attribute of pdf using VB/VBA

New Here ,
Dec 25, 2019 Dec 25, 2019

Copy link to clipboard

Copied

Hello,

I was working on an interesting project for a client of mine and I came across an interesting problem I was hoping someone could help me with? I was scanning through thousands of pdfs for some information and extracting this to Excel for processing. I used the acrobat library to work with the pdf data. The problem I was having was that when the program encountered a password protected pdf, it would bug out until the cancel button was pressed. I couldn't find anything in the library documentation on how to handle password protected pdfs so I used window api to click the cancel button. A direct side effect of this is that I am unable to do anything on the machine whilst the program is running as the windows api would activate the acrobat window.When not opening the pdf to look for the password button (and not using the windows api) I didn't have this issue as it was doing all the reading and copying text in the background.

 

My question is this: is there a way to find out whether a pdf is password protected using the pdf attribute information? If so, how can this be done? This would solve all my issues for next time 🙂

 

Thanks!

TOPICS
Acrobat SDK and JavaScript

Views

8.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 25, 2019 Dec 25, 2019

Copy link to clipboard

Copied

Hi,

 

I was wondering in which version of windows is this occuring and which version of MS Office were the Excel documents created with?

 

Also, would it make any sense to disable Data Execution Prevention (DEP) for all programs in your Windows (just for troubleshooting purposes) .

 

Since you've identified that this is only happening with a few password protected documents you may want to run the accessibility checker to figure out additional issues in the file, like if templates, addins or macros with high security levels were applied in the source document, for example, and if it could be related or not to your issue.

 

OR, remove or lower the password protected restrictions in such documents (if you have the password of course).

 

Otherwise it seems like removing the restriction by Saving the document As or using the browser method to remove password (refrying the document) is the way to go and see if that helps.

 

See here to review steps on password protected PDFs:

 

https://helpx.adobe.com/acrobat/using/securing-pdfs-passwords.html#id_71066 

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 26, 2019 Dec 26, 2019

Copy link to clipboard

Copied

This is possible when using other types of PDF libraries, as they can open a PDF file without actually displaying it on the screen (and then check if it's secured or not before moving on). I'm not sure Acrobat is capable of doing that, so when it loads a secured file the password dialog appears automatically.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 26, 2019 Dec 26, 2019

Copy link to clipboard

Copied

What library would give access to the password attribute in a pdf? I have no objection to using multiple libraries as if I can get the process of checking for password protection before opening the pdf, it'll save me a lot of time in the future. At the moment my code is opening up every single pdf to check for the password box and then continuing with the code. Not very efficient. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 26, 2019 Dec 26, 2019

Copy link to clipboard

Copied

I'm familiar with Java libraries, mostly. For example, PDFBox can certainly do it.

If you're interested I could create for you (for a small fee) a simple stand-alone command-line application that uses this library that will take the path of a PDF file as input and return true or false, depending on whether or not it's secured.

You can contact me privately (via try6767 at gmail.com) to discuss it further.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 26, 2019 Dec 26, 2019

Copy link to clipboard

Copied

The Acrobat SDK will do this. You could write an Acrobat plug-in that can be accessed from JavaScript or the VB IAC interface to open a PDF. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 03, 2020 Jan 03, 2020

Copy link to clipboard

Copied

If your problem already someone is linked to Excel land, why dont you create a VBA macro to check the Security of the selected PDFs (this may what you are already doing)

 

To do this, you not only have to use the VBA API to access adobe, but you have then refrence the javascript library from there as well.  

 

It would looks something like the following:

 

Dim pdApp As Acrobat.AcroApp
Dim pdDoc As Acrobat.AcroPDDoc
Dim jso As Object
Dim securityHandler As String

Set pdApp = CreateObject("AcroExch.App")
Set pdDoc = CreateObject("AcroExch.PDDoc")

pdDoc.Open "1.pdf"

Set jso = pdDoc.GetJSObject

If not jso Is Nothing Then
     'You want to disable any error handling you have, if a pdf file has no secuity on it, the jso.securityHandler call will throw an error
     On Error Resume Next
     securityHandler = jso.securityHandler
     On Error GoTo ErrorHandle
                
     If securityHandler <> "" Then
          MsgBox "Secured!"
     Endif
Endif

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2020 Jan 03, 2020

Copy link to clipboard

Copied

If the document requires a password to be opened, then this VB code won't work, cause the PDF can't be opened. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

All PDF in Directory were protected same password to disable printing.

How can print PDF use VBA?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

I've had to handle this situation before, i.e. processing password protected PDFs.  Neither VBA or JavaScript have functionality for opening a password protected PDF. As I stated in a previous post. The only way to do this in Acrobat is to write a plug-in. This plug-in can then be accessed from JavaScript to open the file. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2020 Mar 30, 2020

Copy link to clipboard

Copied

Hi,

 

In response to johnsmith2 and toanqlcn I've re-read this thread and the discussion brought me back to memory lane, all the way back to Windows 95 and even earlier.

 

So this is what I've gathered.

 

To johnsmith2: To find out whether a pdf is password protected using the pdf attribute information?"

 

I think that can be retrieve using a javascript action custom tool or action button. It is vaguely referenced in page 219 of the Adobe Acrobat SDK JavaScript API Reference. The example taken from this reference only focus on encryption, not password protected but is a good start in what I was able to come up with.

 

Below is how the script example looks: 

 

 

 

console.println(this.securityHandler != null ?
"This document is encrypted with " + this.securityHandler
+ " security." : "This document is unencrypted.");

 

 

 

Now, I'm aware that this is far from what you're looking for since you're using VBA scripting.  And all you need to know is if the PDFs are password protected.

 

To toanqlcn : The same applies and in your case it should be easier since you've already confirmed that all password are the same for security and able to print. So in your case you need a VBA or VB.Net script to get similar information. So because of your question I got a little bit inspired and curious with batch scripting.

 

Here is a batch sript printing code that you can embedd in your VB batch scripts:

 

 

 

SET PrinterName = Test Printer
SET file=%TEMP%\Prt.txt
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xg /n "%PrinterName%" /f "%file%" /q

IF EXIST "%file%" (
   ECHO %PrinterName% printer exists
) ELSE (
   ECHO %PrinterName% printer does NOT exists
)

 

 

The code above is from here: https://www.tutorialspoint.com/batch_script/batch_script_printing.htm

 

Remeber the days of autoexec.bat and config.sys? 

 

So very easily you can find out if a PDF is password protected and encrypted using a single line of  CMD shell scripting or creating your own mini batch script for this purpose.

 

Right-click on a PDF file in your Windows box and opn it with a text editor such as Notepad or Notepad++, for example. 

 

Once you have it open You can hit the "CTRL" + "F" in your keyboard to perform a  search for the string "Encrypt"

 

The slide below helps illustrate:

 

passwdchk1.png

 

This string descriptor is what is letting us know if this file is actually password protected or not.

 

So with a little creativity I played around over the wekend to get a little batch job to perform this action from a source list of PDF's. The key command is "Findstr" (find string).

 

Next slide shows how my first batch script is performing, and on the last slide is a little more refined with added interactions for the user of the script.

 

If you find this code useful you can actual embed this batch script in a VBScript and vice versa.  

 

passwdchk.png

 

Here is the code:

 

 

 

echo(

echo(
echo ----------------------------------------------------------------------------------------
echo                    Total Encrypted and Password protected PDF(s)  
echo ----------------------------------------------------------------------------------------
echo(

setlocal
Findstr /M "Encrypt" *.pdf | find /V /C ""

echo(

Findstr /M "Encrypt" *.pdf

echo(
echo ----------------------------------------------------------------------------------------

echo               Some Password Protected file(s) MAY NOT open for reading/printing.
echo               You can separate these PDFs from the source list by placing 
echo               them in a different folder

echo ----------------------------------------------------------------------------------------
echo(
echo The listed PDFs will be copied now to a separate folder. 
echo(
echo Hit the "Ctrl"+"C" keys to exit batch job at any time or

pause

echo(

mkdir C:\Users\--\Desktop\PDF\PASSWORD
for /f %%a in ('findstr /M "Encrypt" *.pdf') do XCOPY /Y "%%a" C:\Users\--\Desktop\PDF\PASSWORD

echo(
echo(
echo ADOBE ACROBAT WILL OPEN NOW
echo(

pause

for /f %%i in ('START ACROBAT.EXE') do START %%i 

 

 

 

NOTE: that in the end of the code I invoke to open Adobe Acrobat  but pretty much this is where you can embedd your VBScript.

 

Below is the  final result of how I wanted the output to be more appealing. But basically all you need is this line findstr /M "Encrypt" *.pdf

Run it in the current directory where all of the PDFs that you want to inspect are.

 

The same applies for this script to work. Install your batch script in the same directory or folder where all of the concerned PDFs reside.

 

 

passwdchk2.png

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

LATEST

The first link relies on a specific Window's hierarchy path to the password entry dialog. Clever, but not reliable. These types of object structures tend to change with every version. A similar and more reliable solution would be to use a mouse and keyboard simulator. 

 

The second link is for an external PDF library. It's not about Acrobat. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines