Articles in this section
Category / Section

How to add a multi-line Text Box field to a PDF document in C#

2 mins read

The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. Using this library, you can be able to add text box using PdfTextBoxField with multiline in the PDF document.

Steps to add a multiline text box in PDF document

  1. Create a new console application project.
    Console App.png
  2. Install the Syncfusion.Pdf.Net.Core NuGet package as a reference in your console application from Nuget.org.
    NuGet Package.png
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
using System.IO;
  1. Use the following code sample in Program.cs to create a multiline text box in PDF document.

C#

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Creates a new page and adds to the document
PdfPage page = document.Pages.Add();
// Create a standard font to be used in the text field
PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12f);
// Create a text box field and add it to the page with a name
PdfTextBoxField textBoxField = new PdfTextBoxField(page, "FirstName");
// Set the bounds (position and size) of the text box field
textBoxField.Bounds = new RectangleF(0, 0, 200, 50);

// Enable multiline functionality for the text box (allows multiple lines of text)
textBoxField.Multiline = true;

// Set the initial text inside the text box field
textBoxField.Text = "Essential PDF allows you to create and manage the form (AcroForm) in PDF document by using PdfForm class. The PdfFormFieldCollection class represents the entire field collection of the form.";
// Add the text box field to the form field collection in the document
document.Form.Fields.Add(textBoxField);

// Create a FileStream to save the document
using (FileStream stream= new FileStream("Output.pdf", FileMode.Create, FileAccess.Write))
{
  // Save the PDF document to the memory stream
  document.Save(stream);
}
// Close the document
document.Close(true);

A complete working sample is available for download from GitHub.

By executing the program, you will generate the following PDF document.

Output.png

Take a moment to peruse the documentation to learn how to add form fields in a PDF document.

Conclusion
I hope you enjoyed learning on how to add text box using PdfTextBoxField with multiline in the PDF document.

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied