Headers.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Word
  8.  
  9. '' Shows how to add headers and footers to a document.
  10. Public Class Headers
  11. Public Function CreateDocx() As GcWordDocument
  12. Const NPARS = 30
  13.  
  14. Dim doc = New GcWordDocument()
  15.  
  16. '' Add a section and some text to it
  17. Dim sec1 = doc.Body.Sections.First
  18. Dim pars1 = sec1.GetRange().Paragraphs
  19. pars1.Add("Section 1").Style = doc.Styles(BuiltInStyleId.Heading2)
  20. For i = 0 To NPARS - 1
  21. pars1.Add($"Section 1, paragraph {i + 1}: " + LoremIpsumPar())
  22. Next
  23.  
  24. '' Add styles for primary, first and even page headers
  25. Const snHdrPrimary = "Primary page header"
  26. Dim sHdrPrimary = doc.Styles.Add(snHdrPrimary, StyleType.Paragraph)
  27. sHdrPrimary.ParagraphFormat.Alignment = ParagraphAlignment.Left
  28. sHdrPrimary.Font.Color.RGB = Color.Blue
  29.  
  30. Const snHdrFirstPage = "First page header"
  31. Dim sHdrFirstPage = doc.Styles.Add(snHdrFirstPage, StyleType.Paragraph)
  32. sHdrFirstPage.ParagraphFormat.Alignment = ParagraphAlignment.Center
  33. sHdrFirstPage.Font.Color.RGB = Color.Gray
  34.  
  35. Const snHdrEvenPages = "Even header pages"
  36. Dim sHdrEvenPages = doc.Styles.Add(snHdrEvenPages, StyleType.Paragraph)
  37. sHdrEvenPages.ParagraphFormat.Alignment = ParagraphAlignment.Right
  38. sHdrEvenPages.Font.Color.RGB = Color.Blue
  39.  
  40. '' Use a different header on the first, even and odd pages
  41. Dim p = sec1.Headers(HeaderFooterType.Primary).Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - Primary Header - Page ")
  42. p.GetRange().SimpleFields.Add("PAGE")
  43. p.Style = sHdrPrimary
  44.  
  45. sec1.PageSetup.DifferentFirstPageHeaderFooter = True
  46. p = sec1.Headers(HeaderFooterType.FirstPage).Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - First Page")
  47. p.Style = sHdrFirstPage
  48.  
  49. sec1.PageSetup.OddAndEvenPagesHeaderFooter = True
  50. p = sec1.Headers(HeaderFooterType.EvenPages).Body.Paragraphs.Add("DsWord Headers Sample - Section 1 - Even Pages - Page ")
  51. p.GetRange().SimpleFields.Add("PAGE")
  52. p.Style = sHdrEvenPages
  53.  
  54. '' Add another section with different page orientation
  55. Dim sec2 = doc.Body.Paragraphs.Last.AddSectionBreak()
  56. Dim pars2 = sec2.GetRange().Paragraphs
  57. pars2.Add("Section 2").Style = doc.Styles(BuiltInStyleId.Heading2)
  58. For i = 0 To NPARS - 1
  59. pars2.Add($"Section 2, paragraph {i + 1}: " + LoremIpsumPar())
  60. Next
  61.  
  62. '' Set page orientation to landscape
  63. sec2.PageSetup.Size.Orientation = PageOrientation.Landscape
  64.  
  65. '' Unlink and specify own headers for section 2
  66. Dim hdr = sec2.Headers(HeaderFooterType.Primary)
  67.  
  68. '' Add styles for primary and even page headers in secton 2
  69. Const snHdrPrimary2 = "Primary page header 2"
  70. Dim sHdrPrimary2 = doc.Styles.Add(snHdrPrimary2, StyleType.Paragraph)
  71. sHdrPrimary2.ParagraphFormat.Alignment = ParagraphAlignment.Left
  72. sHdrPrimary2.Font.Color.RGB = Color.Purple
  73.  
  74. Const snHdrEvenPages2 = "Even header pages 2"
  75. Dim sHdrEvenPages2 = doc.Styles.Add(snHdrEvenPages2, StyleType.Paragraph)
  76. sHdrEvenPages2.ParagraphFormat.Alignment = ParagraphAlignment.Right
  77. sHdrEvenPages2.Font.Color.RGB = Color.Purple
  78.  
  79. '' NOTE: This property must be set BEFORE changing the header,
  80. '' otherwise it will affect headers in previous sections too
  81. hdr.LinkToPrevious = False
  82. '' NOTE: OddAndEvenPagesHeaderFooter applies To the WHOLE document,
  83. '' Not just to the current section:
  84. '' sec2.PageSetup.OddAndEvenPagesHeaderFooter = false
  85. sec2.PageSetup.DifferentFirstPageHeaderFooter = False
  86. p = hdr.Body.Paragraphs.Add("DsWord Headers Sample - Section 2 - Primary Header - Page ")
  87. p.GetRange().SimpleFields.Add("PAGE")
  88. p.Style = sHdrPrimary2
  89.  
  90. hdr = sec2.Headers(HeaderFooterType.EvenPages)
  91. hdr.LinkToPrevious = False
  92. p = hdr.Body.Paragraphs.Add("DsWord Headers Sample - Section 2 - Even Pages - Page ")
  93. p.GetRange().SimpleFields.Add("PAGE")
  94. p.Style = sHdrEvenPages2
  95.  
  96. '' Done
  97. Return doc
  98. End Function
  99. End Class
  100.