How to Flatten PDF Form Fields in C#

When you want to merge PDF form fields with the content in .NET application, so the fields are not editable, you wonder how to flatten PDF form fields in C# code? Aspose.PDF for .NET API helps answer this question with a few lines of code.

With this .NET API, you can flatten acroform based PDF, all of the form fields at once or select specific fields to flatten in your C# application. The code is same for ASP.NET web application, Windows desktop application, or other .NET platforms.

Follow the below simple and easy steps to programmatically flatten form fields.

Steps to Flatten PDF Form Fields in C#

  1. Install Aspose.PDF for .NET NuGet package
  2. Include Aspose.PDF and Aspose.PDF.Facades namespaces
  3. Set Aspose license using License class
  4. Create an instance of Form class
  5. Bind PDF file which you want to flatten
  6. Use FlattenAllField method to merge all form fields
  7. Use FlattenField method to flatten a specific field(s)
  8. Finally, save the flattened output PDF file

Code to Flatten PDF Form Fields in C#

using System;
//Add Aspose.Pdf for .NET package reference
//Use following namespaces to flatten PDF form Fields
using Aspose.Pdf;
using Aspose.Pdf.Facades;
namespace FlattenPDFFormFields
{
class Program
{
static void Main(string[] args)
{
//Set Aspose license before flattening PDF form fields
//with the help of Aspose.Pdf for .NET
Aspose.Pdf.License AsposePDFLicense = new Aspose.Pdf.License();
AsposePDFLicense.SetLicense(@"c:\asposelicense\license.lic");
//create an object of Form class to initiate form field flattening
Form PDFFormToBeFlattened = new Form();
//Bind the PDF file which contains the form fields to be flattened
PDFFormToBeFlattened.BindPdf("SamplePDFFormwithFields.pdf");
//Use FlattenAllFields method to flatten all of the fields in the
//PDF form loaded above
PDFFormToBeFlattened.FlattenAllFields();
//Save output PDF file with form fields flattened using Save method
//of Form object
PDFFormToBeFlattened.Save("OutputPDFwithFlattenedFormFields.pdf");
}
}
}

The following C# example uses FlattenField method to merge a particular field with the PDF file. You can flatten as many field as you want with this method by providing the field name as parameter.

Code to Flatten a Single PDF Form Field in C#

using System;
//Add Aspose.Pdf for .NET package reference
//Use following namespaces to flatten PDF form Fields
using Aspose.Pdf;
using Aspose.Pdf.Facades;
namespace FlattenPDFFormFields
{
class Program
{
static void Main(string[] args)
{
//Set Aspose license before flattening PDF form fields
//with the help of Aspose.Pdf for .NET
Aspose.Pdf.License AsposePDFLicense = new Aspose.Pdf.License();
AsposePDFLicense.SetLicense(@"c:\asposelicense\license.lic");
//create an object of Form class to initiate form field flattening
Form PDFFormToBeFlattened = new Form();
//Bind the PDF file which contains the form fields to be flattened
PDFFormToBeFlattened.BindPdf("SamplePDFFormwithFields.pdf");
//Use FlattenField method and specify the name of the particular field
//when flattening only a single form field
PDFFormToBeFlattened.FlattenField("TextFormField");
//Save output PDF file with form fields flattened using Save method
//of Form object
PDFFormToBeFlattened.Save("OutputPDFwithFlattenedFormFields.pdf");
}
}
}

Note that, you do not need Adobe Acrobat or any other PDF editing software when flattening PDF form field in C#. This API contains everything you need to programmatically flatten pdf in .NET.

 English