DateAndTime.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 GrapeCity.Documents.Word;
  9. using System.Globalization;
  10.  
  11. namespace DsWordWeb.Demos
  12. {
  13. // This sample shows how to add simple date and time fields to a Word document.
  14. // Note that DsWord does not yet support field calculation. But fields' values
  15. // calculated in code can be supplied for a field, as this sample demonstrates.
  16. public class DateAndTime
  17. {
  18. public GcWordDocument CreateDocx()
  19. {
  20. var now = Util.TimeNow();
  21.  
  22. GcWordDocument doc = new GcWordDocument();
  23. var section = doc.Body.Sections.First;
  24.  
  25. var p0 = section.GetRange().Paragraphs.Add("DATE field with default formatting: ");
  26. p0.GetRange().SimpleFields.Add("DATE", now.ToString("d", CultureInfo.GetCultureInfo("en-US")));
  27. p0.GetRange().Runs.Add("\rTIME field with default formatting: ");
  28. p0.GetRange().SimpleFields.Add("TIME", now.ToString("t", CultureInfo.GetCultureInfo("en-US")));
  29.  
  30. section.GetRange().Paragraphs.Add(
  31. "The following table demonstrates some custom date/time formats. " +
  32. "The left column shows the field code, right column contains the actual field using that code.");
  33.  
  34. // Add and setup the table to contain the samples:
  35. var t = section.GetRange().Tables.Add(0, 0);
  36. t.Style = doc.Styles.Add("Table style 1", StyleType.Table);
  37. foreach (var border in t.Style.Table.Borders)
  38. {
  39. border.LineStyle = LineStyle.Single;
  40. border.LineWidth = 0.5f;
  41. border.Color.RGB = Color.Black;
  42. }
  43. // Sample DATE formats:
  44. string[] sampleDateFormats = new string[]
  45. {
  46. @"DATE \@ ""M/d/yyyy""",
  47. @"DATE \@ ""dddd, MMMM dd, yyyy""",
  48. @"DATE \@ ""MMMM d, yyyy""",
  49. @"DATE \@ ""M/d/yy""",
  50. @"DATE \@ ""yyyy-MM-dd""",
  51. @"DATE \@ ""d-MMM-yy""",
  52. @"DATE \@ ""M.d.yyyy""",
  53. @"DATE \@ ""MMM. d, yy""",
  54. @"DATE \@ ""d MMMM yyyy""",
  55. @"DATE \@ ""MMMM yy""",
  56. @"DATE \@ ""MMM-yy""",
  57. @"DATE \@ ""M/d/yyyy h:mm am/pm""",
  58. @"DATE \@ ""M/d/yyyy h:mm:ss am/pm""",
  59. @"DATE \@ ""h:mm am/pm""",
  60. @"DATE \@ ""h:mm:ss am/pm""",
  61. @"DATE \@ ""HH:mm""",
  62. @"DATE \@ ""'Today is 'MMMM d, yyyy""",
  63. };
  64.  
  65. // Add sample format strings and corresponding fields with pre-calculated values:
  66. foreach (string fmt in sampleDateFormats)
  67. {
  68. var r = t.Rows.Add(new string[] { fmt });
  69. // DsWord does not yet support field calculation, but it does allow you
  70. // to provide values calculated in code, so that's what we do here.
  71. // We use the fact that most date/time format strings are the same
  72. // in Word and .NET, but do replace Word's 'am/pm' with .NET 'tt':
  73. var f = fmt.Substring(@"DATE \@ ".Length).Trim('"').Replace("am/pm", "tt");
  74. r.Cells.Add().GetRange().Paragraphs.First.GetRange().SimpleFields.Add(fmt, now.ToString(f, CultureInfo.GetCultureInfo("en-US")));
  75. }
  76.  
  77. // Done:
  78. return doc;
  79. }
  80. }
  81. }
  82.