Hi
I’m trying to add header and footer text to word documents. I have followed the sample code provide here
https://docs.aspose.com/words/net/working-with-headers-and-footers/
The footer is as expected but the header text is not visible? I can remove an existing header but can’t add a new header.
This is the code I am using…
DocumentBuilder builder = new DocumentBuilder(document);
Aspose.Words.PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Name = "Arial";
builder.Font.Color = System.Drawing.Color.Black;
builder.Font.Size = 8;
// Specify header title for the other page.
builder.Write("Aspose.Words Header/Footer Creation Primer - Title Page.");
builder.InsertField("PAGE", "");
Any help appreciated.
Thanks
Kathy
This message was posted using Page2Forum from How-to: Create Headers/Footers using DocumentBuilder - Aspose.Words for .NET and Java
Hi
Thanks for your inquiry. You should use HeaderFirst instead of HeaderPrimary
DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Name = "Arial";
builder.Font.Color = System.Drawing.Color.Black;
builder.Font.Size = 8;
// Specify header title for the other page.
builder.Write("Aspose.Words Header/Footer Creation Primer - Title Page.");
builder.InsertField("PAGE", "");
Hope this helps.
Best regards,
Thank you for the reply, using teh code suggested
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
the header appears on the first page only. I don’t want the header to appaer on the first page, I only want it on subsequent pages.
In addition to the above code I have the following,
Aspose.Words.PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Name = "Arial";
builder.Font.Color = System.Drawing.Color.Black;
builder.Font.Size = 8;
// Specify header title for the other page.
builder.Write("Aspose.Words Header/Footer Creation Primer - Title Page.");
builder.InsertField("PAGE", "");
This results in the header appearing twice on the first page and no header on any other pages.
Please advise?
Thanks
Kathy
I forgot to add that I have also tried using HeaderEven, although this doesn’t appear to work either!
pageSetup.HeaderDistance = 20;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven); //other pages footer
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
// Set font properties for header text.
builder.Font.Name = "Arial";
builder.Font.Color = System.Drawing.Color.Black;
builder.Font.Size = 8;
Thanks,
Kathy
Hi
Thank you for additional information. Please try using the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Name = "Arial";
builder.Font.Color = System.Drawing.Color.Black;
builder.Font.Size = 8;
// Specify header title for the other page.
builder.Write("Aspose.Words Header/Footer Creation Primer - Title Page.");
builder.InsertField("PAGE", "");
// Create pages in the document.
builder.MoveToDocumentStart();
builder.Writeln("Page1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page2");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page3");
doc.Save("out.doc");
In this case the header appears just on the Page2 and Page3. I have attached my out.doc here.
Best regards,
Thanks Andrey,
This issue has now been resolved. There was a problem with the template that I was using which was blocking the header from appearing. The code provided is now working correctly.
Many thanks for your help.
Kathy