News Category

C#/VB.NET: Flatten Form Fields in PDF

2022-08-15 09:07:00 Written by  support iceblue
Rate this item
(0 votes)

Flattening form fields is an efficient way to prevent others from modifying or deleting the form field contents in PDF. After flattening, the editing or filling capability of the form fields will be removed and their contents will appear as regular text. In this article, you will learn how to flatten form fields in a PDF document in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Flatten a Specific Form Field in PDF in C# and VB.NET

The following are the steps to flatten a specific form field in a PDF document using Spire.PDF for .NET:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the form widget collection from the document.
  • Get a specific form field from the widget collection by its name or index through PdfFormWidget.FieldsWidget["fieldName"] property or PdfFormWidget.FieldsWidget.List[fieldIndex] property.
  • Flatten the form field through PdfField.Flatten property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;

namespace FlattenSpecificFormField
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Form.pdf");

            //Get the form widget collection
            PdfFormWidget formWidget = (PdfFormWidget)pdf.Form;
            //Get a specific form field by its name
            PdfField form = formWidget.FieldsWidget["Address"];
            //Get a specific form field by its index
            //PdfField form = formWidget.FieldsWidget.List[2] as PdfField;
            //Flatten the form
            form.Flatten = true;

            //Save the result document
            pdf.SaveToFile("FlattenSpecific.pdf");
        }
    }
}

C#/VB.NET:  Flatten Form Fields in PDF

Flatten All Form Fields in PDF in C# and VB.NET

The following are the steps to flatten all the form fields in a PDF document using Spire.PDF for .NET:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Flatten all the form fields in the document through PdfDocument.Form.IsFlatten property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace FlattenAllFormFields
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Form.pdf");

            //Flatten all the forms in the document
            pdf.Form.IsFlatten = true;

            //Save the result document
            pdf.SaveToFile("FlattenAll.pdf");
        }
    }
}

C#/VB.NET:  Flatten Form Fields in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 20 June 2023 02:07