Knowledgebase (2328)
Children categories
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:

With Spire.PDF for .NET, we can easily set the password to encrypt the PDF document by password. We can also use Spire.PDF to remove the password from the encrypted PDF document in C# and VB.NET. We need to load the encrypted PDF file with password by calling the method PdfDocument.LoadFromFile (string filename, string password) and then set the password as empty to remove the password.
Firstly, view the PDF with user password:

Step 1: Load the password protected PDF document.
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf", "e-iceblue");
Step 2: Set the password as empty to remove the user password.
string UserPassword = string.Empty; string OwnerPassword = string.Empty; PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy(UserPassword, OwnerPassword); pdf.Encrypt(securityPolicy);
Step 3: Save the document to file.
pdf.SaveToFile("Decrypted.pdf");
Effective screenshot after removing the password from the PDF document:

Full codes:
using Spire.Pdf;
namespace RemovePassword
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf", "e-iceblue");
string UserPassword = string.Empty;
string OwnerPassword = string.Empty;
PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy(UserPassword, OwnerPassword);
pdf.Encrypt(securityPolicy);
pdf.Encrypt(securityPolicy);
pdf.SaveToFile("Decrypted.pdf");
}
}
}
Imports Spire.Pdf
Namespace RemovePassword
Class Program
Private Shared Sub Main(args As String())
Dim pdf As New PdfDocument()
pdf.LoadFromFile("Sample.pdf", "e-iceblue")
Dim UserPassword As String = String.Empty
Dim OwnerPassword As String = String.Empty
Dim securityPolicy As PdfSecurityPolicy = New PdfPasswordSecurityPolicy(UserPassword, OwnerPassword)
pdf.Encrypt(securityPolicy)
pdf.SaveToFile("Decrypted.pdf")
End Sub
End Class
End Namespace
Spire.Email supports to create Outlook message (MSG) files with rich text bodies and attachments in C# and VB.NET. The RTF body supports to set the formatting for the font, such as in bold, underline and set the color and add lists to the message body. It makes the outlook message vivid and clear to view. Here comes to the steps of how to use Spire.Email to create and save the Outlook message with RTF body and attachment in C#.
Step 1: Create an instance of MailMessage class and specify sender and recipient address.
MailAddress addressFrom = new MailAddress("daisy.zhang@e-iceblue.com", "Daisy Zhang");
MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
MailMessage mail = new MailMessage(addressFrom, addressTo);
Step 2: Add the subject, message body and attachment of the message.
mail.Subject = "This is a test message";
string htmlString = @"
<p>Dear Ms. Susan,</p>
<p>This is an example of creating an <b>outlook message</b> <u>(msg)</u>.</p>
<ul>
<li> Create a message with RTF file </li>
<li> Create a message with attachment</li>
</ul>
<p style='color:red'>This text is in red </p>
<p>Best regards, </p>
<p>Daisy </p>";
mail.BodyHtml = htmlString;
mail.Attachments.Add(new Attachment("logo.png"));
Step 3: Save the message as MSG format.
mail.Save("Sample.msg", MailMessageFormat.Msg);
Effective screenshot:

Full codes:
using Spire.Email;
namespace CreatingMSGFiles
{
class Program
{
static void Main(string[] args)
{
MailAddress addressFrom = new MailAddress("daisy.zhang@e-iceblue.com", "Daisy Zhang");
MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
MailMessage mail = new MailMessage(addressFrom, addressTo);
mail.Subject = "This is a test message";
string htmlString = @"
<p>Dear Ms. Susan,</p>
<p>This is an example of creating an <b>outlook message</b> <u>(msg)</u>.</p>
<ul>
<li> Create a message with RTF file </li>
<li> Create a message with attachment</li>
</ul>
<p style='color:red'>This text is in red </p>
<p>Best regards, </p>
<p>Daisy </p>";
mail.BodyHtml = htmlString;
mail.Attachments.Add(new Attachment("logo.png"));
mail.Save("Sample.msg", MailMessageFormat.Msg);
}
}
}