Headers and Footers Example

ABCpdf .net

 
   

This example shows one method of adding headers and footers.

 

   
Setup  

First we create an ABCpdf Doc object and define the content we're going to be adding.

[C#]
Doc theDoc = new Doc();
int theID, theCount;
string theText = "Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur. Hi omnes..."; // truncated for clarity

[Visual Basic]
Dim theDoc As Doc = New Doc()
Dim theID, theCount As Integer
Dim theText As String = "Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur. Hi omnes..." ' truncated for clarity

 

   

Content
 

We set up the style for our content.

We need to specify the Rect defining the area in which content will be inserted, a color (red) and a font size.

We add the text using the same method as employed in the Text Flow Example. We draw a frame round our content area each time so we can see how the text has been positioned.

[C#]
theDoc.Rect.String = "100 200 500 600";
theDoc.Color.String = "255 0 0";
theDoc.FontSize = 24;
theID = theDoc.AddHtml(theText);
theDoc.FrameRect();
while (theDoc.Chainable(theID)) {
  theDoc.Page = theDoc.AddPage();
  theID = theDoc.AddHtml("", theID);
  theDoc.FrameRect();
}
theCount = theDoc.PageCount;

[Visual Basic]
theDoc.Rect.String = "100 200 500 600"
theDoc.Color.String = "255 0 0"
theDoc.FontSize = 24
theID = theDoc.AddHtml(theText)
theDoc.FrameRect()
While theDoc.Chainable(theID)
  theDoc.Page = theDoc.AddPage()
  theID = theDoc.AddHtml("", theID)
  theDoc.FrameRect()
End While
theCount = theDoc.PageCount

 

   

Header
 

We set up the style for our header.

We need to specify the Rect defining the area in which the header will be inserted, a color (green) and a font size. We use the HPos and VPos parameters to center the text both horizontally and vertically.

We then iterate through the pages in the document adding headers as we go. We frame our headers so we can see the header area.

[C#]
theDoc.Rect.String = "100 650 500 750";
theDoc.TextStyle.HPos = 0.5;
theDoc.TextStyle.VPos = 0.5;
theDoc.Color.String = "0 255 0";
theDoc.FontSize = 36;
for (int i = 1; i <= theCount; i++) {
  theDoc.PageNumber = i;
  theDoc.AddText("De Bello Gallico");
  theDoc.FrameRect();
}

[Visual Basic]
theDoc.Rect.String = "100 650 500 750"
theDoc.TextStyle.HPos = 0.5
theDoc.TextStyle.VPos = 0.5
theDoc.Color.String = "0 255 0"
theDoc.FontSize = 36
For i As Integer = 1 To theCount
  theDoc.PageNumber = i
  theDoc.AddText("De Bello Gallico")
  theDoc.FrameRect()
Next

 

   

Footer
 

We set up the style for our footer.

We need to specify the Rect defining the area in which the footer will be inserted, a color (blue) and a font size. We use the HPos and VPos parameters to center the text vertically and align it to the right.

We then iterate through the pages in the document adding footers as we go. We frame our footers so we can see the footer area.

[C#]
theDoc.Rect.String = "100 50 500 150";
theDoc.TextStyle.HPos = 1.0;
theDoc.TextStyle.VPos = 0.5;
theDoc.Color.String = "0 0 255";
theDoc.FontSize = 36;
for (int i = 1; i <= theCount; i++) {
  theDoc.PageNumber = i;
  theDoc.AddText("Page " + i.ToString() + " of " + theCount.ToString());
  theDoc.FrameRect();
}

[Visual Basic]
theDoc.Rect.String = "100 50 500 150"
theDoc.TextStyle.HPos = 1.0
theDoc.TextStyle.VPos = 0.5
theDoc.Color.String = "0 0 255"
theDoc.FontSize = 36
For i As Integer = 1 To theCount
  theDoc.PageNumber = i
  theDoc.AddText("Page " + i.ToString() + " of " + theCount.ToString())
  theDoc.FrameRect()
Next

 

   

Save
 

Finally we save the PDF.

[C#]
theDoc.Save(Server.MapPath("headerfooter.pdf"));
theDoc.Clear();

[Visual Basic]
theDoc.Save(Server.MapPath("headerfooter.pdf"))
theDoc.Clear()

 

   

Results
 

headerfooter.pdf [Page 1]

headerfooter.pdf [Page 2]

headerfooter.pdf [Page 3]

headerfooter.pdf [Page 4]