We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Positioning headers and footers

I am having an issue getting my headers and footers to stay aligned correctly.

To create the report, I am creating a series of sections and indenting them according to a hierarchy. On the first page everything is fine, but then the header and footer seem to shift their alignment so that they are aligned with the first section that appears on the page.

Each section indents correctly, but they are somehow able to influence the alignment of the headers/footers.

I have attached images that show how the alignment is changing. (The parts marked in red are the headers and footers.)

Also, is there a way for me to keep sections, or at least tables, from breaking across pages?

'Function to build the report
Private Function generateReport() As Document
Dim doc As New Document
Dim builder As New DocumentBuilder(doc)

Call AddHeaderFooter()


Dim items As MyItem() = getMyItems()

For Each item As MyItem In items
Dim padLeft As Integer = item.Level
Dim currentSection As Section = builder.CurrentSection
currentSection.PageSetup.LeftMargin = (padLeft * 20) + 36

If item.Property1 IsNot Nothing Then
ElseIf item.Property2 IsNot Nothing Then
End If


Return doc
End Function

’Function to create headers and footers
Private Sub AddHeaderFooter()
Dim currentSection As Section = builder.CurrentSection
Dim pageSetup As PageSetup = currentSection.PageSetup

pageSetup.LeftMargin = 36
pageSetup.RightMargin = 36
pageSetup.TopMargin = 36
pageSetup.BottomMargin = 36

pageSetup.DifferentFirstPageHeaderFooter = False
pageSetup.HeaderDistance = 36

With builder
'build header

.CellFormat.PreferredWidth = PreferredWidth.FromPercent(50)
.CellFormat.Borders.LineStyle = LineStyle.None

.ParagraphFormat.Alignment = ParagraphAlignment.Left
builder.Writeln(“left header text”)

.ParagraphFormat.Alignment = ParagraphAlignment.Right
builder.Writeln(“right header text”)


'build footer
.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 / 3)

.ParagraphFormat.Alignment = ParagraphAlignment.Left
builder.Writeln(“left footer text”)

.ParagraphFormat.Alignment = ParagraphAlignment.Center
builder.Writeln("Page Number: ")
.InsertField(“PAGE”, “”)

.ParagraphFormat.Alignment = ParagraphAlignment.Right
builder.Writeln(“right footer text”)

End With
End Sub


Hi Charles,

Thanks for your inquiry.

May be to avoid this situation the headers and footers must be unlinked by calling the HeaderFooterCollection.LinkToPrevious method on each Section. Passing false to this method will unlink all types of headers and footers from the previous Section. Secondly, I believe you can keep Tables and Rows from breaking across Pages after reading the article suggested below:

Please let me know if I can be of any further assistance.

Best regards,

Thanks for your reply.

I tried using the HeadersFooters.LinkToPrevious(False) but now it removes the headers and footers from all but the fist page.

Is there a way to set up the headers and footers so that their alignment isn’t effected by others sections?

Hi Charles,

Thanks for your inquiry.

Please create a simple application (for example a Console Application Project) that helps us reproduce the same problem on our end and attach it here for testing. Also, please attach a Word document as to how you want your final output be generated like for our reference. Unfortunately, it is somewhat difficult to say what the problem is without your expected Document and simplified application. We need your Document and Simple Project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue and provide you code to achieve what you’re looking for. Thanks for your cooperation.

Best regards,

Here, I hope this helps.

Notice that the headers and footers will align themselves with the first element on the page.

Hi Charles,

Thanks for the additional information.

The problem occurs because you’re incorrectly specifying indentation with the help of PageSetup.LeftMargin property. Please note that PageSetup object contains all the page setup attributes of a section (left margin, bottom margin, paper size, and so on) as properties. Instead, you need to use ParagraphFormat.LeftIndent property to adjust left indent for paragraphs. Please replace line:56 (currentSection.PageSetup.LeftMargin = (padLeft * 20) + 36) of ‘Module1.vb’ with the following statement:

builder.CurrentParagraph.ParagraphFormat.LeftIndent = (padLeft * 20) + 36

I hope, this helps.

Best regards,

Thanks! This is perfect.