SeqFieldOpts.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 SEQ field options
  17. // to enumerate arbitrary items in a DOCX Word document.
  18. public class SeqFieldOpts
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. var doc = new GcWordDocument();
  23.  
  24. // SeqFieldOptions provides access to properties specific to the SEQ field:
  25. const string seqId = "paragraphs";
  26. var seqFldOpts = new SeqFieldOptions(doc, seqId);
  27. seqFldOpts.NumberFormat = "'Paragraph '0':'";
  28.  
  29. // Add numbered paragraphs to the document using the SEQ field:
  30. var rnd = Util.NewRandom();
  31. for (int i = 0; i < rnd.Next(10, 20); i++)
  32. {
  33. doc.Body.AddParagraph(doc.Styles[BuiltInStyleId.Heading2]).AddComplexField(seqFldOpts);
  34. doc.Body.AddParagraph(Util.LoremIpsumPar());
  35. }
  36.  
  37. // For reference add a simple page header with page number to the document:
  38. var phdr = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph(doc.Styles[BuiltInStyleId.Closing]);
  39. phdr.AddComplexField(new PageFieldOptions(doc) { NumberFormat = "'Page '0" });
  40.  
  41. // Update fields using a specific culture:
  42. doc.UpdateFields(new GrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
  43.  
  44. // Done:
  45. return doc;
  46. }
  47. }
  48. }
  49.