TcFieldOpts.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. using GrapeCity.Documents.Word.Fields;
  13.  
  14. namespace DsWordWeb.Demos
  15. {
  16. // This sample shows how to create a Word document with a Table of Contents (TOC)
  17. // that is built with custom TOC entries created using TC (TOC Entry Item) fields.
  18. public class TcFieldOpts
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. var doc = new GcWordDocument();
  23.  
  24. // Add a page header:
  25. var pageOpts = new PageFieldOptions(doc);
  26. pageOpts.NumberStyle = NumberStyle.Decimal;
  27. var sectionPagesOpts = new SectionPagesFieldOptions(doc);
  28. sectionPagesOpts.NumberStyle = NumberStyle.Decimal;
  29. var par = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph("Page ");
  30. par.AddComplexField(pageOpts);
  31. par.GetRange().Runs.Add(" of ");
  32. par.AddComplexField(sectionPagesOpts);
  33.  
  34. // Add a 'TOC' field:
  35. var tocOpts = new TocFieldOptions(doc);
  36. // Tell TOC to collect TC fields (it is off by default):
  37. tocOpts.TcFields.Collect = true;
  38. // Set other options:
  39. tocOpts.EntryFormatting.CreateHyperlink = true;
  40. // Tell the TOC to include all outline levels:
  41. foreach (TocStyleLevel style in tocOpts.Styles)
  42. style.Collect = true;
  43.  
  44. // Add TOC and a page break to the beginning of the document:
  45. doc.Body.Paragraphs.Add("Table of Contents", doc.Styles[BuiltInStyleId.TocHeading]).AddComplexField(tocOpts);
  46. doc.Body.Paragraphs.Last().GetRange().Runs.Last.AddBreak(BreakType.Page);
  47.  
  48. // Create random content with TC fields at 3 nesting levels:
  49. var rnd = Util.NewRandom();
  50. for (int i = 0; i < rnd.Next(2, 4); i++)
  51. {
  52. var headerText = $"Top-level header {i + 1}";
  53. var p = doc.Body.AddParagraph(headerText, doc.Styles[BuiltInStyleId.MessageHeader]);
  54.  
  55. var tcOpts = new TcFieldOptions(doc);
  56. tcOpts.Content.Text = $"TC Field for {headerText}";
  57. tcOpts.DisplayLevel = OutlineLevel.Level1;
  58. doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts);
  59.  
  60. for (int j = 0; j < rnd.Next(2, 5); j++)
  61. {
  62. headerText = $"Second-level header {j + 1}";
  63. p = doc.Body.AddParagraph("\t" + headerText, doc.Styles[BuiltInStyleId.MessageHeader]);
  64.  
  65. tcOpts = new TcFieldOptions(doc);
  66. tcOpts.Content.Text = $"TC Field for {headerText}";
  67. tcOpts.DisplayLevel = OutlineLevel.Level2;
  68.  
  69. doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts);
  70.  
  71. for (int k = 0; k < rnd.Next(2, 6); k++)
  72. {
  73. headerText = $"Third-level header {k + 1}";
  74. p = doc.Body.AddParagraph("\t\t" + headerText, doc.Styles[BuiltInStyleId.MessageHeader]);
  75.  
  76. tcOpts = new TcFieldOptions(doc);
  77. tcOpts.Content.Text = $"TC Field for {headerText}";
  78. tcOpts.DisplayLevel = OutlineLevel.Level3;
  79.  
  80. doc.Body.AddParagraph(Util.LoremIpsumPar()).AddComplexField(tcOpts);
  81. }
  82. }
  83. }
  84. doc.Body.AddParagraph("The End.", doc.Styles[BuiltInStyleId.IntenseQuote]);
  85.  
  86. // Update fields using a specific culture:
  87. doc.UpdateFields(new GrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
  88.  
  89. // Done:
  90. return doc;
  91. }
  92. }
  93. }
  94.