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

Free Support Forum - aspose.com

How to create header in left and top right corner

Hi Support,

I need to create header in left and top right corner, I used the below code but both got right aligned:

Document doc = new Document(strHtmlFileName);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.CurrentSection.PageSetup.DifferentFirstPageHeaderFooter = true; 
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.Write("Left header");
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Right header");

Please find the Created document and Expected document.

This message was posted using Email2Forum by Imran Rafique.

Hi,

Thanks for your inquiry. Your 242.doc has different headers/footers for first and remaining pages. You can achieve the same using following code:

Document doc = new
Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// write some content in body
builder.Writeln("first page");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("other page");
PageSetup ps = builder.PageSetup;
ps.DifferentFirstPageHeaderFooter = true; 
// create empty first page header/footer
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right; 
builder.Writeln("Left header Right header"); 
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right; 
builder.Write("Page ");
builder.InsertField("PAGE");
builder.Write(" of ");
builder.InsertField("NUMPAGES");
doc.Save(MyDir + @"out.docx");

I hope, this helps.

Best regards,

Hi,

I achieved this before with my code itself, I need to write “Left header” should be on top left corner and “Right header” should be on top right corner.

Regards,

Kavi

Hi Kavi,

Thanks for the additional information. You can achieve this by inserting a TabStop between these two words. Please try executing the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// write some content in body
builder.Writeln("first page");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("other page");
PageSetup ps = builder.PageSetup;
ps.DifferentFirstPageHeaderFooter = true;
// create empty first page header/footer
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.TabStops.Add(474.9, TabAlignment.Right, TabLeader.None);
builder.Write("Left header " + ControlChar.Tab + "Right header");
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Page ");
builder.InsertField("PAGE");
builder.Write(" of ");
builder.InsertField("NUMPAGES");
doc.Save(MyDir + @"out.docx");

I hope, this helps.
Best regards,

Hi,

I used the below code to generate word document, But it is not Writing the Header, Please check the below code and find the attachment of html.

string[] values = "HTMLContent".Split(new string[] { "<div style=\"page-break-after: always;\"><span style=\"display:none\">&nbsp;</span></div>" }, StringSplitOptions.RemoveEmptyEntries);
List listPage = new List(values);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup ps = builder.PageSetup;
ps.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.TabStops.Add(474.9, TabAlignment.Right, TabLeader.None);
builder.Write("Left header " + ControlChar.Tab + "Right header");
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Page ");
builder.InsertField("PAGE");
builder.Write(" of ");
builder.InsertField("NUMPAGES");
doc.Sections.Clear();
Section newSec = new Section(doc);
for (int i = 0; i < listPage.Count; i++)
{
    newSec = new Section(doc);
    if (i == 0)
        newSec.PageSetup.SectionStart = SectionStart.Continuous;
    else
        newSec.PageSetup.SectionStart = SectionStart.NewPage;
    doc.Sections.Add(newSec);
    builder.MoveToSection(iSectionIndex);
    iSectionIndex += 1;
    if (listPage[i].Contains("<input name="hdnLandscape" type="hidden" value="MakeLandscape" />"))
    {
        newSec.PageSetup.Orientation = Orientation.Landscape;
        builder.InsertHtml(listPage[i].Replace("<input name="hdnLandscape" type="hidden" value="MakeLandscape" />", ""));
    }
    else
    {
        newSec.PageSetup.Orientation = Orientation.Portrait;
        builder.InsertHtml(listPage[i]);
    }
}
var wordDoc = new WordDocumentVisitor();
doc.Accept(wordDoc);
doc.UpdatePageLayout();
doc.UpdateFields();
doc.Save(strFilePath, SaveFormat.Doc);

Hi Kavi,

Thanks for your inquiry. To ensure a timely and accurate response, please create a standalone runnable simple console application that helps us reproduce your problem on our end and attach it here for testing. As soon as you get this simple application ready, we’ll start investigation into your issue and provide you more information. Also, please attach the output Word document and expected documents for our reference.

Best regards,

Hi,

Please find the Sample console application to create Sample document with Header and the Expected document with Header. Please try to give me the solution ASAP.

Regards,

Kavi

Hi Kavi,

Thanks for the additional information. I have modified your code (Please see attachment). I hope, this helps.

Best regards,

Hi,

Still there is no pagination and Header/Footer even after used your modified code, Please find the attachment of your modified code and the document which i was created using your modified code.

Is there any solution for this? If you have Please provide ASAP.

Regards,

Kavi

Hi Kavi,

Thanks for your inquiry. Considering 989.doc, Microsoft Word application does not show the “primary” header/footer content on the first page because “Different First Page” option is turned on. You can disable this option to be able to display primary header/footer content on all pages. Please see the following code:

Document doc = new Document(MyDir + @"989.doc"); 
doc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = false;
doc.Save(MyDir + @"out.doc");

I hope, this helps.

PS: Output document is also attached with this post.

Best regards,

Hi,

After implementing the below code i can get header/footer in the first page only, It is not displaying the header/footer in the other pages. Do you have any solution for this.

Document doc = new Document(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + @"989.doc");
doc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = false;
doc.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + @"out.doc");

I need header in all the pages except 1st page and need footer(page number) in all the pages including 1st page.

Regards,

Kavi

Hi Kavi,

Thanks for your inquiry. Please note that there can be a maximum of only one HeaderFooter of each HeaderFooterType per Section. For example, talking about footers, they are of three types i.e. FooterFirst, FooterPrimary and FooterEven; so up to three different footers are possible in a Section. Same is the case with headers.

If you have content in all types of headers/footers in a particular section, you can control how Microsoft Word application should display them by using “PageSetup.DifferentFirstPageHeaderFooter” and “PageSetup.OddAndEvenPagesHeaderFooter” options. Here is a good article to understand how headers/footers work:

https://docs.aspose.com/words/net/working-with-headers-and-footers/

Best regards,