Knowledgebase (2344)
Children categories
This article demonstrates how to remove a particular form field and all of the form fields from an existing PDF document using Spire.PDF.
The below sample document contains some text and five different kinds of form fields:

Remove a particular form field
We can remove a particular form field by calling the PdfFieldCollection.Remove method. To remove the form field by index, call the PdfFieldCollection.RemoveAt method and pass the corresponding index of the form field as the argument.
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;
namespace RemoveSpecifiedFormfield
{
class Program
{
static void Main(string[] args)
{
//Create PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load PDF file
pdf.LoadFromFile(@"Input.pdf");
//Get form fields
PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
//Get and remove the first form field
PdfField textbox = formWidget.FieldsWidget.List[0] as PdfTextBoxFieldWidget;
formWidget.FieldsWidget.Remove(textbox);
//Get and remove the first form field using its name
//PdfField field = formWidget.FieldsWidget["Text1"];
//formWidget.FieldsWidget.Remove(field);
//Remove the form field at index 0
//formWidget.FieldsWidget.RemoveAt(0);
pdf.SaveToFile("DeleteParticularField.pdf");
}
}
}

Remove all of the form fields
To remove all of the form fields, first we need to traverse the form fields and then remove them one by one.
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;
namespace RemoveSpecifiedFormfield
{
class Program
{
static void Main(string[] args)
{
//Create PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load PDF file
pdf.LoadFromFile(@"Input.pdf");
//Get form fields
PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
//Remove all of the form fields
for (int i = formWidget.FieldsWidget.List.Count - 1; i >= 0; i--)
{
PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
formWidget.FieldsWidget.Remove(field);
}
pdf.SaveToFile("DeleteAllFields.pdf");
}
}
}

In the previous article, we have introduced how to read a PST file and get the folder information from it. This article will show you how we can add existing mail message files into PSF file for archiving.
Step 1: Load a PST file from disk into an instance of OutlookFile class.
OutlookFile outlookFile = new OutlookFile(@"C:\Users\Administrator\Documents\Outlook Files\Sample.pst");
Step 2: Load a MSG file into an instance of OutlookItme class.
OutlookItem item = new OutlookItem(); item.LoadFromFile(@"C:\Users\Administrator\Documents\Outlook Files\Sample.msg");
Step 3: Get inbox folder from PST file.
OutlookFolder inboxFolder = outlookFile.RootOutlookFolder.GetSubFolder("Inbox");
Step 4: Add the MSG file to inbox folder.
inboxFolder.AddItem(item);
Full Code:
using Spire.Email;
using Spire.Email.Outlook;
using System;
namespace AddMessageToPSTFile
{
class Program
{
static void Main(string[] args)
{
OutlookFile outlookFile = new OutlookFile(@"C:\Users\Administrator\Documents\Outlook Files\Sample.pst");
OutlookItem item = new OutlookItem();
item.LoadFromFile(@"C:\Users\Administrator\Documents\Outlook Files\Sample.msg");
OutlookFolder inboxFolder = outlookFile.RootOutlookFolder.GetSubFolder("Inbox");
inboxFolder.AddItem(item);
Console.WriteLine("Completed");
}
}
}
Imports Spire.Email
Imports Spire.Email.Outlook
Namespace AddMessageToPSTFile
Class Program
Private Shared Sub Main(args As String())
Dim outlookFile As New OutlookFile("C:\Users\Administrator\Documents\Outlook Files\Sample.pst")
Dim item As New OutlookItem()
item.LoadFromFile("C:\Users\Administrator\Documents\Outlook Files\Sample.msg")
Dim inboxFolder As OutlookFolder = outlookFile.RootOutlookFolder.GetSubFolder("Inbox")
inboxFolder.AddItem(item)
Console.WriteLine("Completed")
End Sub
End Class
End Namespace
We often come across a scenario where we need to merge data to the merge fields which are created by some others, and we are not sure about the merge fields’ names. So in order to accomplish the mail merge purpose, first we need to read the names of all the merge fields.
The MailMerge class in Spire.Doc.Reporting namespace exposes the following methods which return a collection of merge field names or group (region) names in a word document.
- public string[] GetMergeFieldNames(): return a collection of all the merge field names.
- public string[] GetMergeFieldNames(string groupName): return a collection of merge field names within a specific group.
- public string[] GetMergeGroupNames(): return a collection of group names.
For better demonstration, we use the following sample document:

The following example elaborates how to read the names of groups and merge fields in above word document.
using Spire.Doc;
using System;
namespace MailMerge
{
class Program
{
static void Main(string[] args)
{
//Creates Document instance
Document document = new Document();
//Loads the word document
document.LoadFromFile("MergeFields.docx");
//Gets the collection of group names
string[] GroupNames = document.MailMerge.GetMergeGroupNames();
//Gets the collection of merge field names in a specific group
string[] MergeFieldNamesWithinRegion = document.MailMerge.GetMergeFieldNames("Products");
// Gets the collection of all the merge field names
string[] MergeFieldNames = document.MailMerge.GetMergeFieldNames();
Console.WriteLine("----------------Group Names-----------------------------------------");
for (int i = 0; i < GroupNames.Length; i++)
{
Console.WriteLine(GroupNames[i]);
}
Console.WriteLine("----------------Merge field names within a specific group-----------");
for (int j = 0; j < MergeFieldNamesWithinRegion.Length; j++)
{
Console.WriteLine(MergeFieldNamesWithinRegion[j]);
}
Console.WriteLine("----------------All of the merge field names------------------------");
for (int k = 0; k < MergeFieldNames.Length; k++)
{
Console.WriteLine(MergeFieldNames[k]);
}
}
}
}
Screenshot:
