Marking Form Fields as Read Only
This example shows how to mark form fields as read only so that they cannot be modified once they are set.
MergeDocument document = new MergeDocument(pdfFilePath); // Set the field values document.Form.Fields["TextBox1"].Value = "My Text"; // TextBox field // Make the form read only document.Form.IsReadOnly = true; document.Draw(pdfFilePath);
Dim MyDocument As MergeDocument = New MergeDocument( pdfFilePath ) ' Set the field values MyDocument.Form.Fields("TextBox1").Value = "My Text" ' TextBox field ' Make the form read only MyDocument.Form.IsReadOnly = True MyDocument.Draw(pdfFilePath)
Marking form fields as read only does not prevent the PDF from being modified (through a PDF viewer) so that the read only property is turned off and then the form field content can be modified again. Adding encryption to the PDF document (see example below) after the form fields have been set to read only will prevent users of the PDF from being able to modify the form fields.
The example code below demonstrate how to add a read-only property to all PDF fields as well as lock the PDF down from being edited:
MergeDocument document = new MergeDocument(pdfFilePath); document.Form.IsReadOnly = true; RC4128Security security = new RC4128Security("owner", "user" ); security.AllowFormFilling = false; security.AllowUpdateAnnotsAndFields = false; security.AllowEdit = false; document.Security = security; document.Draw(pdfFilePath);
Dim MyDocument As MergeDocument = New MergeDocument(pdfFilePath) MyDocument.Form.IsReadOnly = True Dim MySecurity As RC4128Security = New RC4128Security( "owner", "user" ) MySecurity.AllowFormFilling = False MySecurity.AllowUpdateAnnotsAndFields = False MySecurity.AllowEdit = False MyDocument.Security = MySecurity MyDocument.Draw(pdfFilePath)