'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsGrapeCity.Documents.PdfImportsGrapeCity.Documents.Text '' Demonstrates a simple way to generate left/centered/right aligned page headers and footers.PublicClassPageHeaders '' The document being generated:Private_docAsGcPdfDocument '' Utility method to draw a part of a page header or footer.'' Parameters:'' - text: The part's text.'' - tf: The text format to use.'' - pageIdx: The page index.'' - header: True if this is a header, false if a footer.'' - horzAlign: Horizontal alignment (left/center/right).PrivateSubRenderHeader(ByVal text AsString, ByVal tf AsTextFormat, ByVal pageIdx AsInteger, ByVal header AsBoolean, ByVal horzAlign AsTextAlignment)Dim page = _doc.Pages(pageIdx)Dim tl = page.Graphics.CreateTextLayout() tl.MaxWidth = page.Size.Width tl.MaxHeight = page.Size.Height'' 1" margins, adjust as needed: tl.MarginLeft = 72 tl.MarginRight = 72'' 1/3" spacing above top/below bottom header, adjust as needed: tl.MarginTop = 72 / 3 tl.MarginBottom = tl.MarginTop'' Vertical alignment: tl.ParagraphAlignment = If(header, ParagraphAlignment.Near, ParagraphAlignment.Far)'' Horizontal alignment: tl.TextAlignment = horzAlign tl.Append(text, tf)'' NOTE: if some part of a header or footer is static, we could cache the corresponding TextLayout'' object and save some cycles by just drawing that cached TextLayout on each page w/out anything else: tl.PerformLayout(True)'' Draw the header at (0,0) (header located by margins and alignment): page.Graphics.DrawTextLayout(tl, PointF.Empty)EndSub '' The main program.FunctionCreatePDF(ByVal stream AsStream) AsInteger_doc = NewGcPdfDocument()Dim page = _doc.NewPage()'' Add a note about flipping landscape:Dim noteRect = Util.AddNote("We flip page orientation in this sample only to show that these page headers can adapt to the changing page size.", page)'' '' Prepare a TextLayout with some long text and print it (see PaginatedText for details):Dim tl = page.Graphics.CreateTextLayout() tl.DefaultFormat.Font = StandardFonts.Times tl.DefaultFormat.FontSize = 12 tl.MaxWidth = _doc.PageSize.Width tl.MaxHeight = _doc.PageSize.Height tl.MarginAll = tl.Resolution tl.MarginTop = noteRect.Bottom + 18'' Add sample text: tl.Append(Util.LoremIpsum(20))'' Calculate glyphs and perform layout (see also PerformLayout call in the loop below): tl.PerformLayout(True)'' In a loop, split and render the text:WhileTrueDim rest AsTextLayout = NothingDim splitResult = tl.Split(Nothing, rest) page.Graphics.DrawTextLayout(tl, PointF.Empty)If splitResult <> SplitResult.SplitThenExitWhileEndIf tl = rest tl.MarginTop = tl.Resolution page = _doc.Pages.Add()'' For sample sake, toggle page orientation: page.Landscape = Not_doc.Pages(_doc.Pages.Count - 2).Landscape'' Update layout size to reflect the new page orientation: tl.MaxWidth = page.Size.Width tl.MaxHeight = page.Size.Height'' Because we changed layout size, we must perform layout again -'' but can do it without recalculating glyphs: tl.PerformLayout(False)EndWhile'' Render the headers in a separate loop (so that we can provide 'Page X of Y' header):Dim tf = NewTextFormat() With {.Font = StandardFonts.Helvetica, .FontSize = 10, .ForeColor = Color.Gray}Dim now = Util.TimeNow().ToString("u")For pageIdx = 0To_doc.Pages.Count - 1RenderHeader(now, tf, pageIdx, True, TextAlignment.Leading)RenderHeader("Easy Page Headers Sample", tf, pageIdx, True, TextAlignment.Center)RenderHeader($"Page {pageIdx + 1} of {_doc.Pages.Count}", tf, pageIdx, True, TextAlignment.Trailing)RenderHeader("Page footer - left", tf, pageIdx, False, TextAlignment.Leading)RenderHeader("DsPdf", tf, pageIdx, False, TextAlignment.Center)RenderHeader("Page footer - right", tf, pageIdx, False, TextAlignment.Trailing)Next'''' Done:_doc.Save(stream)Return_doc.Pages.CountEndFunctionEndClass