0

i am working on a project that requires a password to access every downloaded PDF file. The password is to be fetched from a database (I am using MySQL). I have searched for a java code that would relate to this type of task but I have barely got any. Most answers are inclined to form filling after the document is already downloaded. I have thought about making the PDF files templates that would only display information if the password field that would be auto-filled (In case i am forced to use this option) but i am afraid that would take a lot of doing. i have read on the context of iTEXT by Bruno Lowagie directed by a question on Stackoverflow but the closest i got was this snippet that answers a question by "affan" on how to fill a PDF automatically using external data from a database. I recon that this snippet is to be used to fill in an already open PDF document.

This is the snippet:

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField(key, value);
stamper.setFormFlattening(true);
stamper.close();
reader.close();

Someone please help out with how i could automatically fill the password field of a PDF that requires a password for access.

1 Answers1

0

It seems that you are mixing two different things:

  1. Form fields, in which you have a text field for which a specific field flag is set so that the text that is added is obfuscated (e.g. "Password" is shown as "********"). Reading your requirement, this is not what you want. Also: I see that you set the form flattening to true and that removes all the field information, including the field flag that changes a text field into a password field.
  2. Encryption, in which you protect a document with one or two passwords.

I assume that you want to protect a document, but I have to admit that my assumption could be wrong. I have no idea why you are mentioning form field in your question.

If you protect a document with an owner password only, everyone has access to that document, but you can put in place some permissions. This is not a secure solution, because many viewers ignore the owner password; moreover, it is very easy to remove the owner password.

If you protect a document with an owner and a user password, only people who know either the owner password or the user password can view the document. All the content of the document is encrypted (except for the Metadata if you want the metadata to be accessible).

Please read the answer to the following questions:

For a more elaborate answer, please consult the FAQ on the official web site.

Form fields (even when using the password flag on a text field) and using encryption to protect a document, are two completely separate topics in the PDF specification (ISO-32000). You can't "automatically fill a password field" in a PDF form, and hope that you can use the value of that field to open a protected document.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165