|
| 1 | +using Syncfusion.Drawing; |
| 2 | +using Syncfusion.HtmlConverter; |
| 3 | +using Syncfusion.Pdf; |
| 4 | +using Syncfusion.Pdf.Security; |
| 5 | +using System.Text.Json; |
| 6 | +using SecurePdfGeneration; |
| 7 | +using System.Text; |
| 8 | +using Syncfusion.Licensing; |
| 9 | + |
| 10 | +Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Ngo9BigBOggjHTQxAR8/V1JEaF5cXmRCdkx3Qnxbf1x1ZFZMYFpbQXZPMyBoS35Rc0VkWHdec3RTQ2RfVEF3VEFd"); |
| 11 | +/// Loads and deserializes the bank statement data from a JSON file. |
| 12 | +string jsonPath = "UserAccountDetails.json"; |
| 13 | +string jsonContent = File.ReadAllText(jsonPath); |
| 14 | +StatementBuilder statement = JsonSerializer.Deserialize<StatementBuilder>(jsonContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
| 15 | + |
| 16 | +// Load HTML template |
| 17 | +string templatePath = "bank-statement-template.html"; |
| 18 | +string htmlTemplate = File.ReadAllText(templatePath); |
| 19 | + |
| 20 | +// Replace top-level placeholders with actual data. |
| 21 | +string filledHtmlContent = htmlTemplate |
| 22 | +.Replace("{{statement.issue_date}}", statement.Statement.IssueDate) |
| 23 | +.Replace("{{statement.period_start}}", statement.Statement.PeriodStart) |
| 24 | +.Replace("{{statement.period_end}}", statement.Statement.PeriodEnd) |
| 25 | +.Replace("{{bank.name}}", statement.Bank.Name) |
| 26 | +.Replace("{{bank.account_number}}", statement.Statement.AccountNumber) |
| 27 | +.Replace("{{bank.address}}", statement.Bank.Address) |
| 28 | +.Replace("{{bank.customername}}", statement.Statement.CustomerName); |
| 29 | + |
| 30 | + |
| 31 | +/// Builds the HTML table rows for each transaction in the bank statement |
| 32 | +/// and replaces the {{ transaction_rows }} placeholder in the HTML template. |
| 33 | +StringBuilder transactionRows = new StringBuilder(); |
| 34 | +foreach (var txn in statement.Transactions) |
| 35 | +{ |
| 36 | + transactionRows.AppendLine($@" |
| 37 | + <tr> |
| 38 | + <td>{txn.Date}</td> |
| 39 | + <td>{txn.Type}</td> |
| 40 | + <td>{txn.Detail}</td> |
| 41 | + <td>{txn.Debited}</td> |
| 42 | + <td>{txn.Credited}</td> |
| 43 | + <td>{txn.Balance}</td> |
| 44 | + </tr>"); |
| 45 | +} |
| 46 | +filledHtmlContent = filledHtmlContent.Replace("{{ transaction_rows }}", transactionRows.ToString()); |
| 47 | + |
| 48 | + |
| 49 | +/// Converts the filled HTML content into a PDF document using Syncfusion's HTML to PDF converter. |
| 50 | +HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); |
| 51 | +BlinkConverterSettings settings = new BlinkConverterSettings |
| 52 | +{ |
| 53 | + Scale = 0.7f |
| 54 | +}; |
| 55 | + |
| 56 | +// Apply the settings to the converter |
| 57 | +htmlConverter.ConverterSettings = settings; |
| 58 | + |
| 59 | +// Convert the filled HTML content into a PDF document |
| 60 | +PdfDocument document = htmlConverter.Convert(filledHtmlContent, Path.GetFullPath(".")); |
| 61 | + |
| 62 | +/// Sets PDF security by applying a user password and restricting permissions to printing only. |
| 63 | +// Access the security settings of the PDF document |
| 64 | +PdfSecurity security = document.Security; |
| 65 | + |
| 66 | +// Retrieve customer name and date of birth from the statement |
| 67 | +string customerName = statement.Statement.CustomerName; |
| 68 | +string dateOfBirth = statement.Statement.DateOfBirth; |
| 69 | + |
| 70 | +// Extract the first 4 characters of the customer name and convert to uppercase |
| 71 | +string namePrefix = (customerName.Substring(0, Math.Min(4, customerName.Length))).ToUpper(); |
| 72 | + |
| 73 | +// Parse the date of birth and format it to get day and month as "ddMM" |
| 74 | +DateTime dob = DateTime.Parse(dateOfBirth); |
| 75 | +string dateAndMonth = dob.ToString("ddMM"); |
| 76 | + |
| 77 | +// Combine name prefix and date/month to form the user password |
| 78 | +string userPassword = namePrefix + dateAndMonth; |
| 79 | + |
| 80 | +// Set the owner password to retain full control over the PDF, including editing and security settings. |
| 81 | +// Required to enforce user-level restrictions like printing-only access and prevent unauthorized changes. |
| 82 | +security.OwnerPassword = "G2bank1234"; |
| 83 | + |
| 84 | +// Set the user password for the PDF document |
| 85 | +security.UserPassword = userPassword; |
| 86 | + |
| 87 | +// Restrict permissions to allow only printing |
| 88 | +security.Permissions = PdfPermissionsFlags.Print; |
| 89 | + |
| 90 | +//Save the PDF document |
| 91 | +Directory.CreateDirectory("../../../Output"); |
| 92 | +string outputPath = $"../../../Output/BankStatement_" + statement.Statement.CustomerName + "_" + statement.Statement.IssueDate + ".pdf"; |
| 93 | +MemoryStream stream = new MemoryStream(); |
| 94 | +document.Save(stream); |
| 95 | +File.WriteAllBytes(outputPath, stream.ToArray()); |
| 96 | + |
| 97 | +// Close the document to release resources |
| 98 | +document.Close(true); |
| 99 | + |
| 100 | +Console.WriteLine("Documents saved successfully!"); |
| 101 | +Console.WriteLine("The password to open the document is: " + userPassword); |
| 102 | + |
| 103 | + |
0 commit comments