DataTplFixPbbNotInCell.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.Linq;
  10. using System.Globalization;
  11. using GrapeCity.Documents.Word;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This example shows how to deal with the
  16. // pbb (paragraph-block-behavior) template formatter
  17. // should start and end in the same cell error.
  18. public class DataTplFixPbbNotInCell
  19. {
  20. // Code demonstrating the problem:
  21. GcWordDocument Problem()
  22. {
  23. using var oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"));
  24. var doc = new GcWordDocument();
  25. doc.DataTemplate.DataSources.Add("ds", oceans);
  26. // Define a 2x1 table:
  27. var table = doc.Body.Tables.Add(2, 1, doc.Styles[BuiltInStyleId.GridTable1Light]);
  28. var cell_00 = table[0, 0];
  29. var cell_01 = table[0, 1];
  30. // Put the start of the pbb range in the cell [0,0]:
  31. cell_00.GetRange().Paragraphs.Add("{{#ds.seas}:pbb()}{{ds.seas.name}} ");
  32. // Put the end of the pbb range in the cell [1,1]:
  33. cell_01.GetRange().Paragraphs.Add("{{/ds.seas}}");
  34. // Incorrect: a pbb range starts in one cell and ends in another:
  35. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  36. return doc;
  37. }
  38.  
  39. // Code demonstrating the fix:
  40. GcWordDocument Fix()
  41. {
  42. using var oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"));
  43. var doc = new GcWordDocument();
  44. doc.DataTemplate.DataSources.Add("ds", oceans);
  45. // Define a 2x1 table:
  46. var table = doc.Body.Tables.Add(2, 1, doc.Styles[BuiltInStyleId.GridTable1Light]);
  47. var cell_00 = table[0, 0];
  48. // Put the start of the pbb range in the cell [0,0]:
  49. cell_00.GetRange().Paragraphs.Add("{{#ds.seas}:pbb()}{{ds.seas.name}} ");
  50. // Put the end of the pbb range in the same cell [0,0]:
  51. cell_00.GetRange().Paragraphs.Add("{{/ds.seas}}");
  52. // Correct: a pbb range starts and ends in the same table cell:
  53. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  54. return doc;
  55. }
  56.  
  57. public GcWordDocument CreateDocx()
  58. {
  59. GcWordDocument doc;
  60. try
  61. {
  62. // This fails:
  63. doc = Problem();
  64. }
  65. catch (Exception ex)
  66. {
  67. // This works:
  68. doc = Fix();
  69. // Insert a brief explanation of the problem and the fix into the generated document:
  70. doc.Body.Paragraphs.Insert(
  71. $"The error \"{ex.Message}\" occurred because a pbb (paragraph-block-behavior) formatter " +
  72. $"started in one table cell and ended in another. A pbb formatter must start and end in the same table cell.",
  73. doc.Styles[BuiltInStyleId.BlockText],
  74. InsertLocation.Start);
  75. }
  76. return doc;
  77. }
  78. }
  79. }
  80.