PDF Forms. Manage via C#

Manage Acroforms in PDF document using Aspose.PDF for .NET Library

How to Manage PDF Forms Using Aspose.PDF for .NET Library

In order to add PDF forms (AcroForms) to a PDF file, we’ll use the Aspose.PDF for .NET API, which is a feature-rich, powerful, and easy-to-use document manipulation API for the .NET platform. Open the NuGet package manager, search for Aspose.PDF, and install it. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

How to Create PDF Forms using C#

You need Aspose.PDF for .NET to try the code in your environment.

  1. Load PDF in an instance of Document class.
  2. Access the Page via its index.
  3. Call the Form collection’s Add method.
  4. Create the form field you want to add.
  5. Save the PDF file.

Create PDF Forms in PDF - C#

This sample code shows how to Create PDF Forms in PDF using C#

var inputFile = Path.Combine(dataDir, "sample.pdf");
var outputFile = Path.Combine(dataDir, "sample_out.pdf");
var pdfDocument = new Aspose.Pdf.Document(inputFile);

// Create a field
var textBoxField = new Aspose.Pdf.Forms.TextBoxField(
    pdfDocument.Pages[1],
    new Aspose.Pdf.Rectangle(100, 200, 300, 300)
    )
{
    PartialName = "textbox1",
    Value = "Text Box"
};

var border = new Aspose.Pdf.Annotations.Border(textBoxField)
{
    Width = 5,
    Dash = new Aspose.Pdf.Annotations.Dash(1, 1)
};
textBoxField.Border = border;
textBoxField.Color = Aspose.Pdf.Color.Green;

pdfDocument.Form.Add(textBoxField, 1);
pdfDocument.Save(outputFile);