DataTplUseCases.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. // The code in this example loads a DOCX document (created in MS Word)
  18. // containing a report template, and depending on the selected demo,
  19. // either adds a data source and generates the data bound report,
  20. // or simply returns the unmodified template for reference.
  21. public class DataTplUseCases
  22. {
  23. public GcWordDocument CreateDocx(string[] sampleParams)
  24. {
  25. var doc = new GcWordDocument();
  26.  
  27. // Load the template DOCX:
  28. doc.Load(Path.Combine("Resources", "WordDocs", sampleParams[3]));
  29.  
  30. using var ds = new DataSet();
  31. // Load the data set:
  32. ds.ReadXml(Path.Combine("Resources", "data", "DsWordTplDataSet.xml"));
  33.  
  34. // Return the unmodified template document for reference:
  35. if (sampleParams.Length >= 6 && sampleParams[5] == "template")
  36. return doc;
  37.  
  38. // Add the data source to the data template data sources:
  39. if (sampleParams.Length >= 5 && !string.IsNullOrEmpty(sampleParams[4]))
  40. doc.DataTemplate.DataSources.Add("ds", ds.Tables[sampleParams[4]]);
  41. else
  42. doc.DataTemplate.DataSources.Add("ds", ds);
  43.  
  44. // The document already has all the necessary bindings,
  45. // so we only need to process the data template:
  46. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  47.  
  48. // Done:
  49. return doc;
  50. }
  51.  
  52. public GcWordDocument CreateDocx(int parsIdx = 0)
  53. {
  54. return CreateDocx(GetSampleParamsList()[parsIdx]);
  55. }
  56.  
  57. public static List<string[]> GetSampleParamsList()
  58. {
  59. // Mandatory: name, description, info
  60. // Custom: template DOCX [[, table] , "template"]
  61. return new List<string[]>()
  62. {
  63. new string[] { "@use-data-tpl/Lease Agreement", "Generate a lease agreement", null,
  64. "Lease_Agreement_Template.docx", "LeaseAgreement" },
  65. new string[] { "@use-data-tpl/Consulting Agreement", "Generate a consultancy agreement", null,
  66. "Consulting_Agreement_Template.docx", "ConsAgreement" },
  67. new string[] { "@use-data-tpl/Rental Agreement", "Generate a house rental agreement", null,
  68. "House_Rental_Template.docx", "HouseRentalAgreement" },
  69. new string[] { "@use-data-tpl/Employment Contract", "Generate an employment contract", null,
  70. "Employment_Contract_Template.docx", "EmploymentContract" },
  71.  
  72. // Parameter sets below simply return the template used to produce the bound document in corresponding
  73. // data template samples:
  74. new string[] { "@use-data-tpl-src/Lease Agreement", "Template Lease_Agreement_Template.docx", null,
  75. "Lease_Agreement_Template.docx", null, "template" },
  76. new string[] { "@use-data-tpl-src/Consulting Agreement", "Template Consulting_Agreement_Template.docx", null,
  77. "Consulting_Agreement_Template.docx", null, "template" },
  78. new string[] { "@use-data-tpl-src/Rental Agreement", "Template House_Rental_Template.docx", null,
  79. "House_Rental_Template.docx", null, "template" },
  80. new string[] { "@use-data-tpl-src/Employment Contract", "Template Employment_Contract_Template.docx", null,
  81. "Employment_Contract_Template.docx", null, "template" },
  82. new string[] { "@use-data-tpl-src/Order Invoice", "Template InvoiceTemplate.docx", null,
  83. "InvoiceTemplate.docx", null, "template" },
  84. new string[] { "@use-data-tpl-src/Closing Disclosure", "Template Closing_Disclosure_Template.docx", null,
  85. "Closing_Disclosure_Template.docx", null, "template" },
  86. new string[] { "@use-data-tpl-src/Vacation Itinerary", "Template Vacation_Itinerary_Template.docx", null,
  87. "Vacation_Itinerary_Template.docx", null, "template" },
  88. };
  89. }
  90. }
  91. }
  92.