DataTplClosingDisclosure.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Globalization;
  13. using GrapeCity.Documents.Word;
  14.  
  15. namespace DsWordWeb.Demos
  16. {
  17. // This data template sample prints the closing disclosure page
  18. // of a loan application. It uses data from the sample database,
  19. // adding a few calculated values based on the sample data.
  20. public class DataTplClosingDisclosure
  21. {
  22. public GcWordDocument CreateDocx(int _ = 0)
  23. {
  24. using var ds = new DataSet();
  25. // Read the sample's data from DsWordTplDataSet.xml:
  26. ds.ReadXml(Path.Combine("Resources", "data", "DsWordTplDataSet.xml"));
  27. var dtClosing = ds.Tables["ClosingDisclosure"];
  28. var row = dtClosing.Rows[0];
  29.  
  30. // Calculate monthly loan payments using a standard formula:
  31. var monthlyPay = CalcMonthlyPayment(Convert.ToDecimal(row["loanAmnt"]), Convert.ToDecimal(row["loanInt"]), Convert.ToInt32(row["loanTerm"]));
  32. // Other calculated data:
  33. var calc = new
  34. {
  35. totMon1 = monthlyPay + Convert.ToDecimal(row["insurance"]) + Convert.ToDecimal(row["escrow"]),
  36. totMon8 = monthlyPay + Convert.ToDecimal(row["escrow"]),
  37. closingCosts = Convert.ToDecimal(row["loanCosts"]) + Convert.ToDecimal(row["otherCosts"]) - Convert.ToDecimal(row["lenderCredit"]),
  38. };
  39. // Combined data for the template:
  40. var data = new
  41. {
  42. // issueDate = row["issueDate"].ToString(),
  43. issueDate = Convert.ToDateTime(row["issueDate"]).ToString("s"),
  44. closingDate = Convert.ToDateTime(row["closingDate"]).ToString("s"),
  45. disDate = Convert.ToDateTime(row["disDate"]).ToString("s"),
  46. agent = row["agent"].ToString(),
  47. fileNo = row["fileNo"].ToString(),
  48. propAddr = row["propAddr"].ToString(),
  49. salePrice = Convert.ToDecimal(row["salePrice"]),
  50. borrower = row["borrower"].ToString(),
  51. seller = row["seller"].ToString(),
  52. lender = row["lender"].ToString(),
  53. loanTerm = Convert.ToInt32(row["loanTerm"]),
  54. purpose = row["purpose"].ToString(),
  55. product = row["product"].ToString(),
  56. loanID = row["loanID"].ToString(),
  57. micNum = row["micNum"].ToString(),
  58. loanAmnt = Convert.ToDecimal(row["loanAmnt"]),
  59. loanInt = Convert.ToDecimal(row["loanInt"]),
  60. penalty = Convert.ToDecimal(row["penalty"]),
  61. insurance = Convert.ToDecimal(row["insurance"]),
  62. escrow = Convert.ToDecimal(row["escrow"]),
  63. taxes = Convert.ToDecimal(row["taxes"]),
  64.  
  65. cash2close = Convert.ToDecimal(row["cash2close"]),
  66. loanCosts = Convert.ToDecimal(row["loanCosts"]),
  67. otherCosts = Convert.ToDecimal(row["otherCosts"]),
  68. lenderCredit = Convert.ToDecimal(row["lenderCredit"]),
  69.  
  70. monthlyPay,
  71. calc.totMon1,
  72. calc.totMon8,
  73. calc.closingCosts,
  74. };
  75.  
  76. // Load the template DOCX, add the data source and process the template:
  77. var doc = new GcWordDocument();
  78. doc.Load(Path.Combine("Resources", "WordDocs", "Closing_Disclosure_Template.docx"));
  79. doc.DataTemplate.DataSources.Add("ds", data);
  80. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  81.  
  82. // Done:
  83. return doc;
  84. }
  85.  
  86. // We use this formula to calculate loan payments for the sample:
  87. // https://cdn.vertex42.com/ExcelArticles/Images/amortization-calculation-formula.png
  88. private static decimal CalcMonthlyPayment(decimal principal, decimal yearlyInterest, int years)
  89. {
  90. var r = (double)(yearlyInterest / 12);
  91. var n = 12 * years;
  92. var q = Math.Pow(1 + r, n);
  93. var a = (double)principal * ((r * q) / (q - 1));
  94. return (decimal)a;
  95. }
  96.  
  97. public static List<string[]> GetSampleParamsList()
  98. {
  99. return new List<string[]>()
  100. {
  101. new string[] { "@data-templates/Closing Disclosure", "Generate the closing disclosure page of a loan application", null },
  102. new string[] { "@use-data-tpl/Closing Disclosure", "Generate the closing disclosure page of a loan application", null },
  103. };
  104. }
  105. }
  106. }
  107.