Thank you for sharing us the code library to add password in pdf file, although I am not familiar with this way, last time i needed to add password to pdf which is already existed. I know Acrobat may be the right stuff to achieve this. But I don't want to pay for it for this tiny problem.
Try free or lower price tool to add password to pdf or use the JavaScript method is ideal way for me.