Skip to content

Commit 53746fb

Browse files
ES-957423-MailMerge-signed-PDF-each-entry
1 parent 410b6a3 commit 53746fb

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MailMerge-signed-PDF-each-entry", "MailMerge-signed-PDF-each-entry\MailMerge-signed-PDF-each-entry.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>MailMerge_signed_PDF_each_entry</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIORenderer;
3+
using Syncfusion.Pdf;
4+
using Syncfusion.Pdf.Interactive;
5+
using Syncfusion.Pdf.Parsing;
6+
using Syncfusion.Drawing;
7+
8+
9+
// Load the Word document
10+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
11+
{
12+
// Get the recipient details as a list of .NET objects
13+
List<Recipient> recipients = GetRecipients();
14+
15+
// Perform mail merge using the recipient data
16+
document.MailMerge.Execute(recipients);
17+
18+
// Initialize the DocIO to PDF converter
19+
using (DocIORenderer renderer = new DocIORenderer())
20+
{
21+
// Preserve form fields (textboxes, checkboxes, etc.)
22+
renderer.Settings.PreserveFormFields = true;
23+
24+
// Convert the merged Word document to PDF
25+
using (PdfDocument pdfDocument = renderer.ConvertToPDF(document))
26+
{
27+
// Save the PDF to memory stream
28+
using (MemoryStream stream = new MemoryStream())
29+
{
30+
pdfDocument.Save(stream);
31+
stream.Position = 0;
32+
33+
// Add digital signature fields in place of textboxes
34+
AddSignatureField(stream, "Signature");
35+
}
36+
}
37+
}
38+
}
39+
40+
41+
#region Mail Merge Data
42+
/// <summary>
43+
/// Gets the data to perform mail merge.
44+
/// </summary>
45+
/// <returns>List of recipient details.</returns>
46+
List<Recipient> GetRecipients()
47+
{
48+
List<Recipient> recipients = new List<Recipient>();
49+
// Initialize sample recipient data
50+
recipients.Add(new Recipient("Nancy Davolio", "NorthWinds", "507 - 20th Ave. E.Apt. 2A", "507-345-2309"));
51+
recipients.Add(new Recipient("Janet Leverling", "NorthWinds", "722 Moss Bay Blvd.", "542-754-2843"));
52+
return recipients;
53+
}
54+
#endregion
55+
56+
#region Add Digital Signature Field
57+
/// <summary>
58+
/// Replaces text box fields in the PDF with signature fields at the same positions.
59+
/// </summary>
60+
/// <param name="stream">PDF stream to modify.</param>
61+
/// <param name="bookmarkTitle">Placeholder text to identify the field (not used directly in this sample).</param>
62+
void AddSignatureField(MemoryStream stream, string bookmarkTitle)
63+
{
64+
// Load the generated PDF document
65+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream))
66+
{
67+
// Access the form fields in the PDF
68+
PdfLoadedForm loadedForm = loadedDocument.Form;
69+
70+
// Loop through each form field
71+
for (int i = loadedForm.Fields.Count - 1; i >= 0; i--)
72+
{
73+
if (loadedForm.Fields[i] is PdfLoadedTextBoxField)
74+
{
75+
// Get the loaded text box field
76+
PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[i] as PdfLoadedTextBoxField;
77+
78+
// Get its bounds and page to use for placing the signature field
79+
RectangleF bounds = loadedTextBoxField.Bounds;
80+
PdfPageBase loadedPage = loadedTextBoxField.Page;
81+
82+
// Create a signature field with the same name and location
83+
PdfSignatureField signatureField = new PdfSignatureField(loadedPage, loadedTextBoxField.Text.Trim());
84+
signatureField.Bounds = bounds;
85+
86+
// Add the signature field to the document
87+
loadedDocument.Form.Fields.Add(signatureField);
88+
89+
// Remove the original textbox field
90+
loadedDocument.Form.Fields.Remove(loadedTextBoxField);
91+
}
92+
}
93+
94+
// Save the modified PDF with signature fields
95+
using (FileStream outputFile = new FileStream(Path.GetFullPath(@"../../../Output/Result.pdf"), FileMode.Create))
96+
{
97+
loadedDocument.Save(outputFile);
98+
}
99+
}
100+
}
101+
#endregion
102+
103+
/// <summary>
104+
/// Represents the Recipient details for the mail merge.
105+
/// </summary>
106+
class Recipient
107+
{
108+
public string FirstName { get; set; }
109+
public string CompanyName { get; set; }
110+
public string Address { get; set; }
111+
public string PhoneNumber { get; set; }
112+
113+
public Recipient(string firstName, string companyName, string address, string phone)
114+
{
115+
FirstName = firstName;
116+
CompanyName = companyName;
117+
Address = address;
118+
PhoneNumber = phone;
119+
}
120+
}
121+

0 commit comments

Comments
 (0)