PageRefFieldOpts.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 PAGEREF field options to add
  17. // and customize a reference to a page in the current document
  18. // containing a specified bookmark.
  19. public class PageRefFieldOpts
  20. {
  21. public GcWordDocument CreateDocx()
  22. {
  23. var doc = new GcWordDocument();
  24.  
  25. // The name of the target bookmark:
  26. const string bmkName = "myBookmark";
  27.  
  28. // The PageRefFieldOptions class provides a convenient strong-typed access
  29. // to options specific to the 'PAGEREF' MS Word field:
  30. var pgRefOpt = new PageRefFieldOptions(doc, bmkName);
  31. pgRefOpt.NumberStyle = NumberStyle.UpperRoman;
  32. pgRefOpt.Hyperlink = true;
  33.  
  34. // Add a paragraph with the PAGEREF to the document:
  35. var p = doc.Body.AddParagraph("Go to page ", doc.Styles[BuiltInStyleId.IndexHeading]).AddComplexField(pgRefOpt);
  36. p.GetRange().Runs.Add("...");
  37.  
  38. // Add some pages:
  39. var rnd = Util.NewRandom();
  40. for (int i = 0; i < rnd.Next(20, 25); i++)
  41. {
  42. doc.Body.AddParagraph(Util.LoremIpsumPar());
  43. }
  44. // End with the bookmarked paragraph:
  45. doc.Body.AddParagraph("The end.").GetRange().Bookmarks.Add(bmkName);
  46.  
  47. // For reference add a simple page header with page number to the document:
  48. var phdr = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.AddParagraph(doc.Styles[BuiltInStyleId.Closing]);
  49. phdr.AddComplexField(new PageFieldOptions(doc) { NumberFormat = "'Page '0" });
  50.  
  51. // Update fields using a specific culture:
  52. doc.UpdateFields(new GrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
  53.  
  54. // Done:
  55. return doc;
  56. }
  57. }
  58. }
  59.