SectionFieldOpts.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 use the SECTION and SECTIONPAGES field options
  17. // to insert the number of the current section and the number of pages in
  18. // the current section into the document.
  19. public class SectionFieldOpts
  20. {
  21. public GcWordDocument CreateDocx()
  22. {
  23. var doc = new GcWordDocument();
  24.  
  25. // SectionFieldOptions provides access to properties specific to the SECTION field:
  26. var sectionFldOpts = new SectionFieldOptions(doc);
  27. sectionFldOpts.NumberStyle = NumberStyle.UpperRoman;
  28.  
  29. // SectionPagesFieldOptions provides access to properties specific to the SECTIONPAGES field:
  30. var sectionPagesFldOpts = new SectionPagesFieldOptions(doc);
  31. sectionPagesFldOpts.NumberFormat = "', '0' page(s)'";
  32.  
  33. // Add a few sections with random content:
  34. var rnd = Util.NewRandom();
  35. var numSections = rnd.Next(3, 6);
  36. for (int i = 0; i < numSections; i++)
  37. {
  38. var p = doc.Body.AddParagraph("Section ", doc.Styles[BuiltInStyleId.Heading1]);
  39. p.AddComplexField(sectionFldOpts);
  40. p.AddComplexField(sectionPagesFldOpts);
  41. for (int j = 0; j < rnd.Next(5, 10); j++)
  42. {
  43. var par = Util.LoremIpsumPar();
  44. doc.Body.AddParagraph(par);
  45. }
  46. if (i < numSections - 1)
  47. doc.Body.Paragraphs.Last.AddSectionBreak(SectionStart.Continuous);
  48. }
  49.  
  50. // For reference add a simple page header with page number to the document:
  51. var phdr = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph(doc.Styles[BuiltInStyleId.Closing]);
  52. phdr.AddComplexField(new PageFieldOptions(doc) { NumberFormat = "'Page '0" });
  53.  
  54. // Update fields using a specific culture:
  55. doc.UpdateFields(new GrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
  56.  
  57. // Done:
  58. return doc;
  59. }
  60. }
  61. }
  62.