Use the PDF Toolkit, pdftk
. It is open-source and runs on Windows as well as linux. You can add passwords, encryption, and modify permissions as follows from the examples here
Encrypt a PDF using 128-Bit Strength (the Default) and Withhold All Permissions (the Default)
pdftk mydoc.pdf output mydoc.128.pdf owner_pw foopass
Same as Above, Except a Password is Required to Open the PDF
pdftk mydoc.pdf output mydoc.128.pdf owner_pw foo user_pw baz
Same as Above, Except Printing is Allowed (after the PDF is Open)
pdftk mydoc.pdf output mydoc.128.pdf owner_pw foo user_pw baz allow printing
Then, in order to automate this for a large number of files, you'll need to create a batchfile (or powershell) to iterate. Since pdftk is all command line, this should not be hard. I wrote and tested the following batch-file. It works:
@ECHO OFF
setlocal EnableDelayedExpansion
md out
for /f %%G in ('dir /b "*.pdf"') do (
call:_pwgen passwd
pdftk %%G output out/%%G user_pw !passwd!
echo '%%G', '!passwd!' >> out/passwords.csv
)
goto :EOF
:_pwgen passwd
setlocal ENABLEEXTENSIONS
set _RNDLength=8
set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set _Str=%_Alphanumeric%987654321
set passwd=%~1
:_LenLoop
if not "%_Str:~18%"=="" set _Str=%_Str:~9%& set /A _Len+=9& GOTO :_LenLoop
set _tmp=%_Str:~9,1%
set /A _Len=_Len+_tmp
set _count=0
set _RndAlphaNum=
:_loop
set /a _count+=1
set _RND=%Random%
set /A _RND=_RND%%%_Len%
set _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
if !_count! lss %_RNDLength% goto _loop
set passwd=!_RndAlphaNum!
endlocal&set %~1=%passwd%
GOTO:EOF
Thanks to other discussion here on how to generate random passwords in a batch file.
An md5-hash of the filename is not quite random and defeats the purpose of having different passwords for different files. Even if the algorithm is kept secret, knowing one password makes finding how it is deduced trivial and thus compromises the security of the others. – Marcks Thomas – 2012-05-25T17:47:42.393