diff --git a/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry.sln b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry.sln new file mode 100644 index 000000000..b31ac0d24 --- /dev/null +++ b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35527.113 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +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}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Data/Template.docx b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Data/Template.docx new file mode 100644 index 000000000..4bf2ed692 Binary files /dev/null and b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Data/Template.docx differ diff --git a/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/MailMerge-signed-PDF-each-entry.csproj b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/MailMerge-signed-PDF-each-entry.csproj new file mode 100644 index 000000000..fb255a827 --- /dev/null +++ b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/MailMerge-signed-PDF-each-entry.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + MailMerge_signed_PDF_each_entry + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Output/.gitkeep b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Program.cs b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Program.cs new file mode 100644 index 000000000..34972b495 --- /dev/null +++ b/Word-to-PDF-Conversion/MailMerge-signed-PDF-each-entry/.NET/MailMerge-signed-PDF-each-entry/Program.cs @@ -0,0 +1,121 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIORenderer; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Pdf.Parsing; +using Syncfusion.Drawing; + + +// Load the Word document +using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"))) +{ + // Get the recipient details as a list of .NET objects + List recipients = GetRecipients(); + + // Perform mail merge using the recipient data + document.MailMerge.Execute(recipients); + + // Initialize the DocIO to PDF converter + using (DocIORenderer renderer = new DocIORenderer()) + { + // Preserve form fields (textboxes, checkboxes, etc.) + renderer.Settings.PreserveFormFields = true; + + // Convert the merged Word document to PDF + using (PdfDocument pdfDocument = renderer.ConvertToPDF(document)) + { + // Save the PDF to memory stream + using (MemoryStream stream = new MemoryStream()) + { + pdfDocument.Save(stream); + stream.Position = 0; + + // Add digital signature fields in place of textboxes + AddSignatureField(stream, "Signature"); + } + } + } +} + + +#region Mail Merge Data +/// +/// Gets the data to perform mail merge. +/// +/// List of recipient details. +List GetRecipients() +{ + List recipients = new List(); + // Initialize sample recipient data + recipients.Add(new Recipient("Nancy Davolio", "NorthWinds", "507 - 20th Ave. E.Apt. 2A", "507-345-2309")); + recipients.Add(new Recipient("Janet Leverling", "NorthWinds", "722 Moss Bay Blvd.", "542-754-2843")); + return recipients; +} +#endregion + +#region Add Digital Signature Field +/// +/// Replaces text box fields in the PDF with signature fields at the same positions. +/// +/// PDF stream to modify. +/// Placeholder text to identify the field (not used directly in this sample). +void AddSignatureField(MemoryStream stream, string bookmarkTitle) +{ + // Load the generated PDF document + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream)) + { + // Access the form fields in the PDF + PdfLoadedForm loadedForm = loadedDocument.Form; + + // Loop through each form field + for (int i = loadedForm.Fields.Count - 1; i >= 0; i--) + { + if (loadedForm.Fields[i] is PdfLoadedTextBoxField) + { + // Get the loaded text box field + PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[i] as PdfLoadedTextBoxField; + + // Get its bounds and page to use for placing the signature field + RectangleF bounds = loadedTextBoxField.Bounds; + PdfPageBase loadedPage = loadedTextBoxField.Page; + + // Create a signature field with the same name and location + PdfSignatureField signatureField = new PdfSignatureField(loadedPage, loadedTextBoxField.Text.Trim()); + signatureField.Bounds = bounds; + + // Add the signature field to the document + loadedDocument.Form.Fields.Add(signatureField); + + // Remove the original textbox field + loadedDocument.Form.Fields.Remove(loadedTextBoxField); + } + } + + // Save the modified PDF with signature fields + using (FileStream outputFile = new FileStream(Path.GetFullPath(@"../../../Output/Result.pdf"), FileMode.Create)) + { + loadedDocument.Save(outputFile); + } + } +} +#endregion + +/// +/// Represents the Recipient details for the mail merge. +/// +class Recipient +{ + public string FirstName { get; set; } + public string CompanyName { get; set; } + public string Address { get; set; } + public string PhoneNumber { get; set; } + + public Recipient(string firstName, string companyName, string address, string phone) + { + FirstName = firstName; + CompanyName = companyName; + Address = address; + PhoneNumber = phone; + } +} +