Knowledgebase (2344)
Children categories
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");
}
}
}

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");
}
}
}

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.
XFA forms are XML-based forms created by Adobe's LiveCycle Designer tool, which offer enhanced features over the old AcroForms, like changeable text fields and support for running JavaScript. Spire.PDF supports to access the XFA forms in an existing PDF file and fill the fields with data.
Step 1: Initialize an instance of PdfDocument class and load a sample PDF file containing dynamic XFA forms.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("DynamicXFA.pdf");
Step 2: Get all form widgets from the document.
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
Step 3: Get a list of XFA Fields.
List<XfaField> xfafields = formWidget.XFAForm.XfaFields;
Step 4: Traverse each XfaField in the list and judge if it is an XfaTextField, if yes, convert the type of XfaField as an XfaTextField and then assign value to the field based on the field name.
foreach (XfaField xfaField in xfaFields)
{
if (xfaField is XfaTextField)
{
XfaTextField xf = xfaField as XfaTextField;
switch (xfaField.Name)
{
case "EmployeeName":
xf.Value = "Gary";
break;
case "Address":
xf.Value = "Chengdu, China";
break;
case "StateProv":
xf.Value = "Sichuan Province";
break;
case "ZipCode":
xf.Value = "610093";
break;
case "SSNumber":
xf.Value = "000-00-0000";
break;
case "HomePhone":
xf.Value = "86-028-81705109";
break;
case "CellPhone":
xf.Value = "123456789";
break;
case "Comments":
xf.Value = "This demo shows how to fill XFA forms using Spire.PDF";
break;
default:
break;
}
}
}
Step 5: Save the file.
doc.SaveToFile("FillXfaField.pdf",FileFormat.PDF);
Output:

Full Code:
using Spire.Pdf;
using Spire.Pdf.Widget;
using System.Collections;
using System.Collections.Generic;
namespace FillXFAFormFields
{
class Program
{
private static IEnumerable xfaFields;
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("DynamicXFA.pdf");
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
List <xfafields> = formWidget.XFAForm.XfaFields;
foreach (XfaField xfaField in xfafields)
{
if (xfaField is XfaTextField)
{
XfaTextField xf = xfaField as XfaTextField;
switch (xfaField.Name)
{
case "EmployeeName":
xf.Value = "Gary";
break;
case "Address":
xf.Value = "Chengdu, China";
break;
case "StateProv":
xf.Value = "Sichuan Province";
break;
case "ZipCode":
xf.Value = "610093";
break;
case "SSNumber":
xf.Value = "000-00-0000";
break;
case "HomePhone":
xf.Value = "86-028-81705109";
break;
case "CellPhone":
xf.Value = "123456789";
break;
case "Comments":
xf.Value = "This demo shows how to fill XFA forms using Spire.PDF";
break;
default:
break;
}
}
}
doc.SaveToFile("FillXfaField.pdf", FileFormat.PDF);
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Widget
Imports System.Collections.Generic
Namespace FillXFAFormFields
Class Program
Private Shared xfaFields As IEnumerable(Of XfaField)
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument()
doc.LoadFromFile("DynamicXFA.pdf")
Dim formWidget As PdfFormWidget = TryCast(doc.Form, PdfFormWidget)
Dim xfafields__1 As List(Of XfaField) = formWidget.XFAForm.XfaFields
For Each xfaField As XfaField In xfaFields
If TypeOf xfaField Is XfaTextField Then
Dim xf As XfaTextField = TryCast(xfaField, XfaTextField)
Select Case xfaField.Name
Case "EmployeeName"
xf.Value = "Gary"
Exit Select
Case "Address"
xf.Value = "Chengdu, China"
Exit Select
Case "StateProv"
xf.Value = "Sichuan Province"
Exit Select
Case "ZipCode"
xf.Value = "610093"
Exit Select
Case "SSNumber"
xf.Value = "000-00-0000"
Exit Select
Case "HomePhone"
xf.Value = "86-028-81705109"
Exit Select
Case "CellPhone"
xf.Value = "123456789"
Exit Select
Case "Comments"
xf.Value = "This demo shows how to fill XFA forms using Spire.PDF"
Exit Select
Case Else
Exit Select
End Select
End If
Next
doc.SaveToFile("FillXfaField.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace
Simplifying your Excel workbooks by removing redundant or unused worksheets can be a beneficial organizational practice. This process allows you to eliminate clutter and improve file structure by focusing only on the most relevant data. Removing unneeded worksheets frees up storage space, streamlines navigation, and keeps your workbooks clean and efficient.
In this article, you will learn how to remove worksheets from an Excel workbook in C# by using the Spire.XLS for .NET library.
- Remove a Worksheet by Its Index from a Workbook
- Remove a Worksheet by Its Name from a Workbook
- Remove All Worksheets from a Workbook at Once
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Remove a Worksheet by Its Index from a Workbook in C#
Spire.XLS for .NET provides the WorksheetsCollection.RemoveAt(int index) method, which allows you to remove a specific worksheet by its index from a workbook. Here are the detailed steps:
- Create a Workbook object.
- Load an Excel file from a given path.
- Get the worksheets collection from the document using Workbook.Worksheets property.
- Remove a worksheet by its index using WorksheetsCollection.RemoveAt(int index) method.
- Save the workbook to a different Excel document.
- C#
using Spire.Xls;
using Spire.Xls.Collections;
namespace RemoveWorksheetByIndex
{
class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook wb = new Workbook();
// Load an Excel file
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\Input.xlsx");
// Get the worksheets collection from the document
WorksheetsCollection worksheets = wb.Worksheets;
// Remove a specific worksheet by its index
worksheets.RemoveAt(0);
// Save the workbook to a different Excel file
wb.SaveToFile("RemoveByIndex.xlsx", ExcelVersion.Version2016);
// Dispose resources
wb.Dispose();
}
}
}
Remove a Worksheet by Its Name from a Workbook in C#
If you already know the name of the worksheet that you want to remove, you can do so by using the WorksheetsCollection.Remove(string sheetName) method. The detailed steps are as follows:
- Create a Workbook object.
- Load an Excel file from a given path.
- Get the worksheets collection from the document using Workbook.Worksheets property.
- Remove a worksheet by its name using WorksheetsCollection.Remove(string sheetName) method.
- Save the workbook to a different Excel document.
- C#
using Spire.Xls;
using Spire.Xls.Collections;
namespace RemoveWorksheetByName
{
class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook wb = new Workbook();
// Load an Excel file
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\Input.xlsx");
// Get the worksheets collection from the document
WorksheetsCollection worksheets = wb.Worksheets;
// Remove a specific worksheet by its name
worksheets.Remove("sheet2")
// Save the workbook to a different Excel file
wb.SaveToFile("RemoveByName.xlsx", ExcelVersion.Version2016);
// Dispose resources
wb.Dispose();
}
}
}
Remove All Worksheets from a Workbook at Once in C#
To remove all worksheets at once, you can use the WorksheetsCollection.Clear() method. Here are the detailed steps:
- Create a Workbook object.
- Load an Excel file from a given path.
- Get the worksheets collection from the document using Workbook.Worksheets property.
- Remove all worksheet at once using WorksheetsCollection.Clear() method.
- Save the workbook to a different Excel document.
- C#
using Spire.Xls;
using Spire.Xls.Collections;
namespace RemoveAllWorksheets
{
class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook wb = new Workbook();
// Load an Excel file
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\Input.xlsx");
// Get the worksheets collection from the document
WorksheetsCollection worksheets = wb.Worksheets;
// Remove all worksheets
worksheets.Clear();
// Save the workbook to a different Excel file
wb.SaveToFile("RemoveAllWorksheets.xlsx", ExcelVersion.Version2016);
// Dispose resources
wb.Dispose();
}
}
}
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.