We have a library of PDF forms that contain fillable fields and that can be opened in Adobe Reader and filled out by our end users. Note that we are NOT generating PDFs but simply using existing PDFs. All of these forms are protected against editing non-fillable fields in Acrobat by means of a password.
The problem is that when we open the PDF in our code to prefill some of the fields the password protection is lost and the form that is served up is editable in Acrobat.
So:
- Is there a way to prefill fillable fields using iTextSharp without unlocking the PDF and the resulting output stream?
- Is there a way to relock the form while still allowing an end user
to open in Reader, fill fillable fields, print, etc., without
requiring a password?
Here is a simplified example of the code we are currently using. We have tried locking the output stream (code not currently available) but the file could not then Reader prompted the user for a password.
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] password = encoding.GetBytes("secretPassword");
PdfReader reader = null;
MemoryStream outputStream = null;
PdfStamper stamper = null;
pdfStream.Seek(0, SeekOrigin.Begin);
reader = new PdfReader(pdfStream, password);
outputStream = new MemoryStream();
stamper = new PdfStamper(reader, outputStream);
AcroFields fields = stamper.AcroFields;
fields.SetField("prefillField1", "Some text");
stamper.Close();
outputStream.Close();
reader.Close();
// outputStream sent to user/browser. PDF opens in Reader, but the form can be opened in
// Acrobat without a password and edited.
You cannot edit a PDF if it is password protected. You will like you are doing have to open it with the password to edit it. With that it looks like you are creating a copy of that document and presenting to the user. You can encrypt the PDF with an edit password. Here is a copy of a method that I found that I use with itextsharp. If you set the
editPasswordwhile leaving theopenPasswordnull then your users should be able to view the pdf and interact with it but won’t be able to edit it based on the permissions you set. Hope this helps.