Headers.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.Linq;
  9. using GrapeCity.Documents.Word;
  10.  
  11. namespace DsWordWeb.Demos
  12. {
  13. // Shows how to add headers and footers to a document.
  14. public class Headers
  15. {
  16. public GcWordDocument CreateDocx()
  17. {
  18. const int NPARS = 30;
  19. var doc = new GcWordDocument();
  20.  
  21. // Add a section and some text to it:
  22. var sec1 = doc.Body.Sections.First;
  23. var pars1 = sec1.GetRange().Paragraphs;
  24. pars1.Add("Section 1").Style = doc.Styles[BuiltInStyleId.Heading2];
  25. for (int i = 0; i < NPARS; ++i)
  26. pars1.Add($"Section 1, paragraph {i + 1}: " + Util.LoremIpsumPar());
  27.  
  28. // Add styles for primary, first and even page headers:
  29. const string snHdrPrimary = "Primary page header";
  30. var sHdrPrimary = doc.Styles.Add(snHdrPrimary, StyleType.Paragraph);
  31. sHdrPrimary.ParagraphFormat.Alignment = ParagraphAlignment.Left;
  32. sHdrPrimary.Font.Color.RGB = Color.Blue;
  33.  
  34. const string snHdrFirstPage = "First page header";
  35. var sHdrFirstPage = doc.Styles.Add(snHdrFirstPage, StyleType.Paragraph);
  36. sHdrFirstPage.ParagraphFormat.Alignment = ParagraphAlignment.Center;
  37. sHdrFirstPage.Font.Color.RGB = Color.Gray;
  38.  
  39. const string snHdrEvenPages = "Even header pages";
  40. var sHdrEvenPages = doc.Styles.Add(snHdrEvenPages, StyleType.Paragraph);
  41. sHdrEvenPages.ParagraphFormat.Alignment = ParagraphAlignment.Right;
  42. sHdrEvenPages.Font.Color.RGB = Color.Blue;
  43.  
  44. // Use a different header on the first, even and odd pages:
  45. var p = sec1.Headers[HeaderFooterType.Primary].Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - Primary Header - Page ");
  46. p.GetRange().SimpleFields.Add("PAGE");
  47. p.Style = sHdrPrimary;
  48.  
  49. sec1.PageSetup.DifferentFirstPageHeaderFooter = true;
  50. p = sec1.Headers[HeaderFooterType.FirstPage].Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - First Page");
  51. p.Style = sHdrFirstPage;
  52.  
  53. sec1.PageSetup.OddAndEvenPagesHeaderFooter = true;
  54. p = sec1.Headers[HeaderFooterType.EvenPages].Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - Even Pages - Page ");
  55. p.GetRange().SimpleFields.Add("PAGE");
  56. p.Style = sHdrEvenPages;
  57.  
  58. // Add another section with different page orientation and different headers:
  59. var sec2 = doc.Body.Paragraphs.Last.AddSectionBreak();
  60. var pars2 = sec2.GetRange().Paragraphs;
  61. pars2.Add("Section 2").Style = doc.Styles[BuiltInStyleId.Heading2];
  62. for (int i = 0; i < NPARS; ++i)
  63. pars2.Add($"Section 2, paragraph {i + 1}: " + Util.LoremIpsumPar());
  64.  
  65. // Set page orientation to landscape:
  66. sec2.PageSetup.Size.Orientation = PageOrientation.Landscape;
  67.  
  68. // Unlink and specify own headers for section 2:
  69. var hdr = sec2.Headers[HeaderFooterType.Primary];
  70.  
  71. // Add styles for primary and even page headers in secton 2:
  72. const string snHdrPrimary2 = "Primary page header 2";
  73. var sHdrPrimary2 = doc.Styles.Add(snHdrPrimary2, StyleType.Paragraph);
  74. sHdrPrimary2.ParagraphFormat.Alignment = ParagraphAlignment.Left;
  75. sHdrPrimary2.Font.Color.RGB = Color.Purple;
  76.  
  77. const string snHdrEvenPages2 = "Even header pages 2";
  78. var sHdrEvenPages2 = doc.Styles.Add(snHdrEvenPages2, StyleType.Paragraph);
  79. sHdrEvenPages2.ParagraphFormat.Alignment = ParagraphAlignment.Right;
  80. sHdrEvenPages2.Font.Color.RGB = Color.Purple;
  81.  
  82. // NOTE: This property must be set BEFORE changing the header,
  83. // otherwise it will affect headers in previous sections too:
  84. hdr.LinkToPrevious = false;
  85. // NOTE: OddAndEvenPagesHeaderFooter applies to the WHOLE document,
  86. // not just to the current section:
  87. // sec2.PageSetup.OddAndEvenPagesHeaderFooter = false;
  88. sec2.PageSetup.DifferentFirstPageHeaderFooter = false;
  89. p = hdr.Body.Paragraphs.Add("DsWord Headers Sample - Section 2 - Primary Header - Page ");
  90. p.GetRange().SimpleFields.Add("PAGE");
  91. p.Style = sHdrPrimary2;
  92.  
  93. hdr = sec2.Headers[HeaderFooterType.EvenPages];
  94. hdr.LinkToPrevious = false;
  95. p = hdr.Body.Paragraphs.Add("DsWord Headers Sample - Section 2 - Even Pages - Page ");
  96. p.GetRange().SimpleFields.Add("PAGE");
  97. p.Style = sHdrEvenPages2;
  98.  
  99. // Done:
  100. return doc;
  101. }
  102. }
  103. }
  104.