Header Borders

All,
I’m creating a header but I want a horizontal line to be under the name. I tried below. Also I loose the formatting when I try to Add page numbers it doesn’t print on this page. Where can I set KeepSourceFormatting? Please help

Sub CreateTOCHeader2(ByVal strSourceDoc As String, ByVal strTheme As String)
Dim doc As Aspose.Words.Document = New Aspose.Words.Document(strSourceDoc)
Dim builder As New DocumentBuilder(doc)
doc.FirstSection.HeadersFooters.LinkToPrevious(False)
Dim currentSection As Aspose.Words.Section = builder.CurrentSection
currentSection.ClearHeadersFooters()
Dim pageSetup As PageSetup = currentSection.PageSetup
pageSetup.DifferentFirstPageHeaderFooter = True
' --- Create header for the first page. ---
pageSetup.HeaderDistance = 45
builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.HeaderFirst)
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left
' Set font properties for header text.
builder.Font.Name = "Arial"
builder.Font.Bold = True
builder.Font.Size = 18
builder.Font.Color = System.Drawing.Color.Black
'Set builder to insert horizontal line between paragraphs.
builder.CellFormat.Borders.Bottom.LineWidth = 1.0
builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
builder.CellFormat.Borders.Bottom.Color = System.Drawing.Color.Black <-Not Working
' Specify header title for the first page.
builder.Write(strTheme)
'builder.InsertField("PAGE")
doc.Save(strSourceDoc)
End Sub

I also tried to do this to change the text in the Header and it didn’t work. If I can’t get the Pagenumbers to print is there a way to change the header.

Sub CreateTOCHeader(ByVal strSourceDoc As String, ByVal strDestDoc As String), byval bByTheme as boolean,byval strTheme as string)
Dim dstDoc As Aspose.Words.Document = New Aspose.Words.Document(strDestDoc)
Dim srcDoc As Aspose.Words.Document = New Aspose.Words.Document(strSourceDoc)
Dim builderA As DocumentBuilder = New DocumentBuilder(srcDoc)
Dim iCount As Integer = 0
For Each sectB As Aspose.Words.Section In srcDoc.Sections
Dim sectA As Aspose.Words.Section = Nothing
Dim sectBIndex As Integer = srcDoc.Sections.IndexOf(sectB)
'Check whether document A conteins section with same index 
If dstDoc.Sections.Count > sectBIndex Then
'Get correcponding section 
sectA = dstDoc.Sections(sectBIndex)
Else 'Insert empty section 
builderA.MoveToDocumentEnd()
builderA.InsertBreak(BreakType.SectionBreakContinuous)
sectA = builderA.CurrentSection
End If
'Remove Headers/Footers from new section
sectA.ClearHeadersFooters()
sectA.PageSetup.DifferentFirstPageHeaderFooter = sectB.PageSetup.DifferentFirstPageHeaderFooter
sectA.PageSetup.OddAndEvenPagesHeaderFooter = sectB.PageSetup.OddAndEvenPagesHeaderFooter
For Each childB As Node In hfB.ChildNodes
iCount = iCount + 1
'Import node 
Dim childA As Node = dstDoc.ImportNode(childB, True, ImportFormatMode.KeepSourceFormatting)
If (bByTheme) Then
If iCount = 2 Then
childA.Range.Text.Remove(0)
childA.Range.UpdateFields()
ElseIf iCount = 3 Then
childA.Range.Text.Replace(childA.Range.Text, strTheme)
childA.Range.UpdateFields() <-Doesn't update name
End If
End If
'Appent node to h/f 
sectA.HeadersFooters(hfB.HeaderFooterType).AppendChild(childA)
Next
Else 'Copy whole h/f 
Dim hfA As Node = dstDoc.ImportNode(hfB, True, ImportFormatMode.KeepSourceFormatting)
'Insert h/f 
sectA.HeadersFooters.Add(hfA)
End If
Next
Next
'dstDoc.UpdatePageLayout()
'Save output
dstDoc.Save(strDestDoc)

Hi Jon,

Thanks for your inquiry.

*jtingres:

I’m creating a header but I want a horizontal line to be under the name. I tried below. Also I loose the formatting when I try to Add page numbers it doesn’t print on this page. Where can I set KeepSourceFormatting? Please help*

In your case, I suggest you please use the ParagraphFormat.Borders property as shown in following highlighted code snippet.

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate, how you want your final Word output be generated like. We will then provide you more information on this along with code.

Dim doc As Aspose.Words.Document = New Aspose.Words.Document()
Dim builder As New DocumentBuilder(doc)
doc.FirstSection.HeadersFooters.LinkToPrevious(False)
Dim currentSection As Aspose.Words.Section = builder.CurrentSection
currentSection.ClearHeadersFooters()
Dim pageSetup As PageSetup = currentSection.PageSetup
pageSetup.DifferentFirstPageHeaderFooter = True
' --- Create header for the first page. ---
pageSetup.HeaderDistance = 45
builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.HeaderFirst)
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left
' Set font properties for header text.
builder.Font.Name = "Arial"
builder.Font.Bold = True
builder.Font.Size = 18
builder.Font.Color = System.Drawing.Color.Black
'Set builder to insert horizontal line between paragraphs.
builder.ParagraphFormat.Borders.Bottom.LineWidth = 1.0
builder.ParagraphFormat.Borders.Bottom.LineStyle = LineStyle.Single
builder.ParagraphFormat.Borders.Bottom.Color = System.Drawing.Color.Black

*jtingres:

I also tried to do this to change the text in the Header and it didn’t work. If I can’t get the Pagenumbers to print is there a way to change the header.

Sub CreateTOCHeader(ByVal strSourceDoc As String, ByVal strDestDoc As String), byval bByTheme as boolean,byval strTheme as string)
Dim dstDoc As Aspose.Words.Document = New Aspose.Words.Document(strDestDoc)
Dim srcDoc As Aspose.Words.Document = New Aspose.Words.Document(strSourceDoc)
Dim builderA As DocumentBuilder = New DocumentBuilder(srcDoc)
…*

It would be great if you please share your input Word documents along with expected output Word document. I will investigate the issue on my side and provide you more information.

ASPOSE,
Attached in zip is the info you requested. Please see the info.txt file for more info. I noticed if I include this code and set to True my pagenum doesn’t print. But if I do include it my pagenum prints but the header doesn’t print. This is the CreateTOCHeader2 code you adjusted for me.
pageSetup.DifferentFirstPageHeaderFooter = True
JT

Hi there,

Thanks for your inquiry. HeaderFooter class represents a container for the header or footer text of a section. HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section.

If Section does not have a HeaderFooter of a specific type or the HeaderFooter has no child nodes, this header/footer is considered linked to the header/footer of the same type of the previous section in Microsoft Word. When HeaderFooter contains at least one Paragraph, it is no longer considered linked to previous in Microsoft Word.

PageSetup.DifferentFirstPageHeaderFooter : True if a different header or footer is used on the first page.

In your case, I suggest you please copy the HeaderPrimary/FooterPrimary into HeaderFirst/FooterFirst to get the page number at each page of document. Please read following documentation link and check the following code snippet for your kind reference.
https://reference.aspose.com/words/net/aspose.words/headerfootertype/
https://docs.aspose.com/words/net/working-with-headers-and-footers/

For Each section As Section In doc
Dim ps As PageSetup = section.PageSetup
If ps.DifferentFirstPageHeaderFooter Then
CopyHeaderFooter(section, HeaderFooterType.HeaderPrimary, HeaderFooterType.HeaderFirst)
CopyHeaderFooter(section, HeaderFooterType.FooterPrimary, HeaderFooterType.FooterFirst)
ElseIf ps.OddAndEvenPagesHeaderFooter AndAlso ps.PageStartingNumber Mod 2 = 0 Then
CopyHeaderFooter(section, HeaderFooterType.HeaderPrimary, HeaderFooterType.HeaderEven)
CopyHeaderFooter(section, HeaderFooterType.FooterPrimary, HeaderFooterType.FooterEven)
End If
Next
Public Shared Sub CopyHeaderFooter(ByVal section As Section, ByVal source As HeaderFooterType, ByVal dest As HeaderFooterType)
Dim hfNodes As NodeCollection = section.HeadersFooters(source).ChildNodes
section.HeadersFooters(dest).ChildNodes.Clear()
For Each node As Node In hfNodes
section.HeadersFooters(dest).ChildNodes.Add(node)
Next
End Sub

Okay I changed it to this

Dim pageSetup As PageSetup = currentSection.PageSetup
'pageSetup.DifferentFirstPageHeaderFooter = True <- Comment this out
' --- Create header for the first page. ---
pageSetup.HeaderDistance = 45
builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.HeaderPrimary)<-Change this to headerprimary
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left

It worked then.
JT

Hi there,

Thanks for your feedback. It is nice to hear from you that your issue has been solved. Please let us know if you have any more queries.