> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/create/document-creation-api/sections-headers-and-footers.md).

# Sections, headers, and footers

Use the document creation API to add sections, headers, and footers programmatically to docs.

A `Section` is a group of consecutive pages that share a page size, margins, and headers and footers. Every `FlowDocument` starts with one section, and content added directly to the document goes into the **current** (last) section.

## Headers and footers

Headers and footers are content containers owned by a section. `GetOrCreateHeader` and `GetOrCreateFooter` return the existing one or create an empty one; the content of an existing header is preserved.

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
Section section = doc.GetCurrentSection();

ContentContainer header = section.GetOrCreateHeader(Section.PageGroup.e_odd_pages);
Paragraph headerPara = header.AddParagraph("SYH Supply Co.");
headerPara.SetJustificationMode(Paragraph.TextJustification.e_right);
headerPara.GetTextStyledElement().SetTextColor(120, 120, 120);

ContentContainer footer = section.GetOrCreateFooter(Section.PageGroup.e_odd_pages);
Paragraph footerPara = footer.AddParagraph();
footerPara.SetJustificationMode(Paragraph.TextJustification.e_center);

footerPara.AddText("Page ");
footerPara.AddPageNumber(PageNumber.NumberType.e_current_page);
footerPara.AddText(" of ");
footerPara.AddPageNumber(PageNumber.NumberType.e_total_pages);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
Section section = doc.GetCurrentSection();

ContentContainer header = section.GetOrCreateHeader(Section::e_odd_pages);
Paragraph header_para = header.AddParagraph("SYH Supply Co.");
header_para.SetJustificationMode(Paragraph::e_text_justify_right);
header_para.GetTextStyledElement().SetTextColor(120, 120, 120);

ContentContainer footer = section.GetOrCreateFooter(Section::e_odd_pages);
Paragraph footer_para = footer.AddParagraph();
footer_para.SetJustificationMode(Paragraph::e_text_justify_center);

footer_para.AddText("Page ");
footer_para.AddPageNumber(PageNumber::e_current_page);
footer_para.AddText(" of ");
footer_para.AddPageNumber(PageNumber::e_total_pages);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
section := doc.GetCurrentSection()

header := section.GetOrCreateHeader(SectionE_odd_pages)
headerPara := header.AddParagraph("SYH Supply Co.")
headerPara.SetJustificationMode(ParagraphE_text_justify_right)
headerPara.GetTextStyledElement().SetTextColor(120, 120, 120)

footer := section.GetOrCreateFooter(SectionE_odd_pages)
footerPara := footer.AddParagraph()
footerPara.SetJustificationMode(ParagraphE_text_justify_center)

footerPara.AddText("Page ")
footerPara.AddPageNumber(PageNumberE_current_page)
footerPara.AddText(" of ")
footerPara.AddPageNumber(PageNumberE_total_pages)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Section section = doc.getCurrentSection();

ContentContainer header = section.getOrCreateHeader(Section.PageGroup.e_odd_pages);
Paragraph headerPara = header.addParagraph("SYH Supply Co.");
headerPara.setJustificationMode(Paragraph.TextJustification.e_text_justify_right);
headerPara.getTextStyledElement().setTextColor(120, 120, 120);

ContentContainer footer = section.getOrCreateFooter(Section.PageGroup.e_odd_pages);
Paragraph footerPara = footer.addParagraph();
footerPara.setJustificationMode(Paragraph.TextJustification.e_text_justify_center);

footerPara.addText("Page ");
footerPara.addPageNumber(PageNumber.NumberType.e_current_page);
footerPara.addText(" of ");
footerPara.addPageNumber(PageNumber.NumberType.e_total_pages);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const section = await doc.getCurrentSection();

const header = await section.getOrCreateHeader(PDFNet.Section.PageGroup.e_odd_pages);
const headerPara = await header.addParagraphWithText('SYH Supply Co.');
headerPara.setJustificationMode(PDFNet.Paragraph.TextJustification.e_text_justify_right);
(await headerPara.getTextStyledElement()).setTextColor(120, 120, 120);

const footer = await section.getOrCreateFooter(PDFNet.Section.PageGroup.e_odd_pages);
const footerPara = await footer.addParagraph();
footerPara.setJustificationMode(PDFNet.Paragraph.TextJustification.e_text_justify_center);

await footerPara.addText('Page ');
await footerPara.addPageNumber(PDFNet.PageNumber.NumberType.e_current_page);
await footerPara.addText(' of ');
await footerPara.addPageNumber(PDFNet.PageNumber.NumberType.e_total_pages);
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objectivec
PTSection* section = [doc GetCurrentSection];

PTContentContainer* header = [section GetOrCreateHeader:e_ptsection_odd_pages];
PTParagraph* header_para = [header AddParagraphWithText:@"SYH Supply Co."];
[header_para SetJustificationMode:e_ptpara_text_justify_right];
[[header_para GetTextStyledElement] SetTextColor:120 green:120 blue:120];

PTContentContainer* footer = [section GetOrCreateFooter:e_ptsection_odd_pages];
PTParagraph* footer_para = [footer AddParagraph];
[footer_para SetJustificationMode:e_ptpara_text_justify_center];

[footer_para AddText:@"Page "];
[footer_para AddPageNumber:e_ptpagenumber_current_page];
[footer_para AddText:@" of "];
[footer_para AddPageNumber:e_ptpagenumber_total_pages];
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$section = $doc->GetCurrentSection();

$header = $section->GetOrCreateHeader(Section::e_odd_pages);
$header_para = $header->AddParagraph("SYH Supply Co.");
$header_para->SetJustificationMode(Paragraph::e_text_justify_right);
$header_para->GetTextStyledElement()->SetTextColor(120, 120, 120);

$footer = $section->GetOrCreateFooter(Section::e_odd_pages);
$footer_para = $footer->AddParagraph();
$footer_para->SetJustificationMode(Paragraph::e_text_justify_center);

$footer_para->AddText("Page ");
$footer_para->AddPageNumber(PageNumber::e_current_page);
$footer_para->AddText(" of ");
$footer_para->AddPageNumber(PageNumber::e_total_pages);
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
section = doc.GetCurrentSection()

header = section.GetOrCreateHeader(Section.e_odd_pages)
header_para = header.AddParagraph("SYH Supply Co.")
header_para.SetJustificationMode(Paragraph.e_text_justify_right)
header_para.GetTextStyledElement().SetTextColor(120, 120, 120)

footer = section.GetOrCreateFooter(Section.e_odd_pages)
footer_para = footer.AddParagraph()
footer_para.SetJustificationMode(Paragraph.e_text_justify_center)

footer_para.AddText("Page ")
footer_para.AddPageNumber(PageNumber.e_current_page)
footer_para.AddText(" of ")
footer_para.AddPageNumber(PageNumber.e_total_pages)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
section = doc.GetCurrentSection()

header = section.GetOrCreateHeader(Section::E_odd_pages)
header_para = header.AddParagraph("SYH Supply Co.")
header_para.SetJustificationMode(Paragraph::E_text_justify_right)
header_para.GetTextStyledElement().SetTextColor(120, 120, 120)

footer = section.GetOrCreateFooter(Section::E_odd_pages)
footer_para = footer.AddParagraph()
footer_para.SetJustificationMode(Paragraph::E_text_justify_center)

footer_para.AddText("Page ")
footer_para.AddPageNumber(PageNumber::E_current_page)
footer_para.AddText(" of ")
footer_para.AddPageNumber(PageNumber::E_total_pages)
```

{% endcode %}
{% endtab %}
{% endtabs %}

<figure><img src="https://3779731113-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fziw3GiL98Xfj63F3He8h%2Fuploads%2F4SsBvr0LMTMNEIB5ZiwT%2Fheader_footer.png?alt=media&amp;token=302798dd-ea88-4106-b502-7f81314f153d" alt="Example of a section with page numbering, body text, and company name"><figcaption></figcaption></figure>

### Page groups

Each header and footer applies to one group of pages, so a section can have up to three of each.

Page group values include:

| Value          | Applies to                     |
| -------------- | ------------------------------ |
| `e_first_page` | The first page of the section. |
| `e_even_pages` | Even numbered pages.           |
| `e_odd_pages`  | Odd numbered pages.            |

To use the same footer everywhere, create one for each group.

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
Section.PageGroup[] groups = {
    Section.PageGroup.e_first_page,
    Section.PageGroup.e_even_pages,
    Section.PageGroup.e_odd_pages };

foreach (Section.PageGroup group in groups)
{
    Paragraph para = section.GetOrCreateFooter(group).AddParagraph();
    para.SetJustificationMode(Paragraph.TextJustification.e_center);
    para.AddPageNumber(PageNumber.NumberType.e_current_page);
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
Section::PageGroup const groups[] = {
    Section::e_first_page, Section::e_even_pages, Section::e_odd_pages };

for (int i = 0; i < 3; ++i)
{
    Paragraph para = section.GetOrCreateFooter(groups[i]).AddParagraph();
    para.SetJustificationMode(Paragraph::e_text_justify_center);
    para.AddPageNumber(PageNumber::e_current_page);
}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
groups := []int{SectionE_first_page, SectionE_even_pages, SectionE_odd_pages}

for _, group := range groups {
    para := section.GetOrCreateFooter(group).AddParagraph()
    para.SetJustificationMode(ParagraphE_text_justify_center)
    para.AddPageNumber(PageNumberE_current_page)
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Section.PageGroup[] groups = {
        Section.PageGroup.e_first_page,
        Section.PageGroup.e_even_pages,
        Section.PageGroup.e_odd_pages };

for (Section.PageGroup group : groups) {
    Paragraph para = section.getOrCreateFooter(group).addParagraph();
    para.setJustificationMode(Paragraph.TextJustification.e_text_justify_center);
    para.addPageNumber(PageNumber.NumberType.e_current_page);
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const groups = [
    PDFNet.Section.PageGroup.e_first_page,
    PDFNet.Section.PageGroup.e_even_pages,
    PDFNet.Section.PageGroup.e_odd_pages];

for (const group of groups) {
    const para = await (await section.getOrCreateFooter(group)).addParagraph();
    para.setJustificationMode(PDFNet.Paragraph.TextJustification.e_text_justify_center);
    await para.addPageNumber(PDFNet.PageNumber.NumberType.e_current_page);
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objectivec
PTPageGroup groups[] = {
    e_ptsection_first_page, e_ptsection_even_pages, e_ptsection_odd_pages };

for (int i = 0; i < 3; ++i)
{
    PTParagraph* para = [[section GetOrCreateFooter:groups[i]] AddParagraph];
    [para SetJustificationMode:e_ptpara_text_justify_center];
    [para AddPageNumber:e_ptpagenumber_current_page];
}
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$groups = array(Section::e_first_page, Section::e_even_pages, Section::e_odd_pages);

foreach ($groups as $group) {
    $para = $section->GetOrCreateFooter($group)->AddParagraph();
    $para->SetJustificationMode(Paragraph::e_text_justify_center);
    $para->AddPageNumber(PageNumber::e_current_page);
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
groups = [Section.e_first_page, Section.e_even_pages, Section.e_odd_pages]

for group in groups:
    para = section.GetOrCreateFooter(group).AddParagraph()
    para.SetJustificationMode(Paragraph.e_text_justify_center)
    para.AddPageNumber(PageNumber.e_current_page)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
groups = [Section::E_first_page, Section::E_even_pages, Section::E_odd_pages]

groups.each do |group|
  para = section.GetOrCreateFooter(group).AddParagraph()
  para.SetJustificationMode(Paragraph::E_text_justify_center)
  para.AddPageNumber(PageNumber::E_current_page)
end
```

{% endcode %}
{% endtab %}
{% endtabs %}

The first-page group is how you suppress a header on a title page: create an empty header for it and a populated one for the even and odd groups. Use `RemoveHeader` or `RemoveFooter` to drop one entirely.

## Page numbers

`AddPageNumber` inserts a dynamic field that is resolved during pagination, so counts stay correct when content shifts. Use the `current-page` type for the page itself and the `total-pages` type for the document total.

`SetNumberFormat` changes the numbering system - lower roman for front matter, for example. A large set of formats is supported, including:

* `e_upper_roman`
* `e_lower_letter`
* `e_upper_letter`
* `e_ordinal`
* `e_cardinal_text`
* `e_decimal_zero`
* `e_arabic_alpha`
* `e_japanese_counting`
* `e_korean_counting`
* `e_hebrew_1`
* `e_hindi_numbers`
* `e_russian_lower`

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
PageNumber number = para.AddPageNumber(PageNumber.NumberType.e_current_page);
number.SetNumberFormat(PageNumber.NumberFormat.e_lower_roman);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PageNumber number = para.AddPageNumber(PageNumber::e_current_page);
number.SetNumberFormat(PageNumber::e_lower_roman);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
number := para.AddPageNumber(PageNumberE_current_page)
number.SetNumberFormat(PageNumberE_lower_roman)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PageNumber number = para.addPageNumber(PageNumber.NumberType.e_current_page);
number.setNumberFormat(PageNumber.NumberFormat.e_lower_roman);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const number = await para.addPageNumber(PDFNet.PageNumber.NumberType.e_current_page);
number.setNumberFormat(PDFNet.PageNumber.NumberFormat.e_lower_roman);
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objectivec
PTPageNumber* number = [para AddPageNumber:e_ptpagenumber_current_page];
[number SetNumberFormat:e_ptpagenumber_lower_roman];
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$number = $para->AddPageNumber(PageNumber::e_current_page);
$number->SetNumberFormat(PageNumber::e_lower_roman);
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
number = para.AddPageNumber(PageNumber.e_current_page)
number.SetNumberFormat(PageNumber.e_lower_roman)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
number = para.AddPageNumber(PageNumber::E_current_page)
number.SetNumberFormat(PageNumber::E_lower_roman)
```

{% endcode %}
{% endtab %}
{% endtabs %}

Page numbers are not restricted to footers. They work in any paragraph, including cross-references in body text.

## Multiple sections

`AddSection` appends a section and makes it current, so subsequent content goes into it. A section always starts on a new page, and can override the document defaults for page size and margins - which is how a landscape page is mixed into a portrait document.

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
FlowDocument doc = new FlowDocument();
doc.SetDefaultPageSize(300, 200);
doc.SetDefaultMargins(18, 18, 18, 18);

doc.AddParagraph("Section 1 uses the document default portrait page.");

// A new section starts on a new page and can override the page setup.
Section landscape = doc.AddSection();
landscape.SetPageSize(400, 200);
landscape.SetMargins(18, 18, 18, 18);

doc.AddParagraph("Section 2 overrides the page size and margins.");
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
FlowDocument doc;
doc.SetDefaultPageSize(300, 200);
doc.SetDefaultMargins(18, 18, 18, 18);

doc.AddParagraph("Section 1 uses the document default portrait page.");

// A new section starts on a new page and can override the page setup.
Section landscape = doc.AddSection();
landscape.SetPageSize(400, 200);
landscape.SetMargins(18, 18, 18, 18);

doc.AddParagraph("Section 2 overrides the page size and margins.");
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
doc := NewFlowDocument()
doc.SetDefaultPageSize(300, 200)
doc.SetDefaultMargins(18, 18, 18, 18)

doc.AddParagraph("Section 1 uses the document default portrait page.")

// A new section starts on a new page and can override the page setup.
landscape := doc.AddSection()
landscape.SetPageSize(400, 200)
landscape.SetMargins(18, 18, 18, 18)

doc.AddParagraph("Section 2 overrides the page size and margins.")
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
FlowDocument doc = new FlowDocument();
doc.setDefaultPageSize(300, 200);
doc.setDefaultMargins(18, 18, 18, 18);

doc.addParagraph("Section 1 uses the document default portrait page.");

// A new section starts on a new page and can override the page setup.
Section landscape = doc.addSection();
landscape.setPageSize(400, 200);
landscape.setMargins(18, 18, 18, 18);

doc.addParagraph("Section 2 overrides the page size and margins.");
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const doc = await PDFNet.FlowDocument.create();
doc.setDefaultPageSize(300, 200);
doc.setDefaultMargins(18, 18, 18, 18);

await doc.addParagraphWithText('Section 1 uses the document default portrait page.');

// A new section starts on a new page and can override the page setup.
const landscape = await doc.addSection();
landscape.setPageSize(400, 200);
landscape.setMargins(18, 18, 18, 18);

await doc.addParagraphWithText('Section 2 overrides the page size and margins.');
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objectivec
PTFlowDocument* doc = [[PTFlowDocument alloc] init];
[doc SetDefaultPageSize:300 height:200];
[doc SetDefaultMargins:18 top:18 right:18 bottom:18];

[doc AddParagraphWithText:@"Section 1 uses the document default portrait page."];

// A new section starts on a new page and can override the page setup.
PTSection* landscape = [doc AddSection];
[landscape SetPageSize:400 height:200];
[landscape SetMargins:18 top:18 right:18 bottom:18];

[doc AddParagraphWithText:@"Section 2 overrides the page size and margins."];
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$doc = new FlowDocument();
$doc->SetDefaultPageSize(300, 200);
$doc->SetDefaultMargins(18, 18, 18, 18);

$doc->AddParagraph("Section 1 uses the document default portrait page.");

// A new section starts on a new page and can override the page setup.
$landscape = $doc->AddSection();
$landscape->SetPageSize(400, 200);
$landscape->SetMargins(18, 18, 18, 18);

$doc->AddParagraph("Section 2 overrides the page size and margins.");
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
doc = FlowDocument()
doc.SetDefaultPageSize(300, 200)
doc.SetDefaultMargins(18, 18, 18, 18)

doc.AddParagraph("Section 1 uses the document default portrait page.")

# A new section starts on a new page and can override the page setup.
landscape = doc.AddSection()
landscape.SetPageSize(400, 200)
landscape.SetMargins(18, 18, 18, 18)

doc.AddParagraph("Section 2 overrides the page size and margins.")
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
doc = FlowDocument.new()
doc.SetDefaultPageSize(300, 200)
doc.SetDefaultMargins(18, 18, 18, 18)

doc.AddParagraph("Section 1 uses the document default portrait page.")

# A new section starts on a new page and can override the page setup.
landscape = doc.AddSection()
landscape.SetPageSize(400, 200)
landscape.SetMargins(18, 18, 18, 18)

doc.AddParagraph("Section 2 overrides the page size and margins.")
```

{% endcode %}
{% endtab %}
{% endtabs %}

Section settings cascade. A section only overrides the document defaults where you explicitly set a page size or margins. Headers and footers are **not** inherited; each section defines its own.

You can also add content to a section directly rather than through the document, which is clearer when maintaining several sections.

## Further Reading

* [Paragraphs and text](/core/create/document-creation-api/paragraphs-and-text.md) for styling header and footer content.
* [Floats](/core/create/document-creation-api/floats.md) for page-anchored content such as watermarks.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/create/document-creation-api/sections-headers-and-footers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
