|
| 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