Flattening form fields is an efficient way to prevent other users from editing or deleting the information filled in PDF forms. After flattening, the filled in values (text) will remain visible in the document but the form fields will no longer be editable. This article presents how to flatten all the form fields in a PDF document by using Spire.PDF.
Below pdf file contains two form fields that are already filled with contents:
Refer to below code snippets to flatten the form fields:
Step 1: Instatiate an object of PdfDocument class and load the PDF document.
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Form.pdf");
Step 2: Set the IsFlatten property as true to flatten all the form fields in the document.
pdf.Form.IsFlatten = true;
Step 3: Save the document.
pdf.SaveToFile("Flatten.pdf");
Screenshot after flattening:
Full code:
[C#]
using Spire.Pdf; namespace Flatten_PDF_Form_Fields { class Program { static void Main(string[] args) { PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Form.pdf"); pdf.Form.IsFlatten = true; pdf.SaveToFile("Flatten.pdf"); } } }
[VB.NET]
Imports Spire.Pdf Namespace Flatten_PDF_Form_Fields Class Program Private Shared Sub Main(args As String()) Dim pdf As New PdfDocument() pdf.LoadFromFile("Form.pdf") pdf.Form.IsFlatten = True pdf.SaveToFile("Flatten.pdf") End Sub End Class End Namespace