Free Support Forum - aspose.com

Re: Headers and footers (Help needed urgently)

Hi,

I am facing a few issue in adding of header to the first, odd and even pages. I will list them out in point 1 by 1,

- how do i add a page number (page x of y) to the header programmatically.
- i had successfully added a table to the header how do i align in the center of the header.
- not too sure what is the reason, but my odd page header and even page header are showing different even thou i using the same piece of codes to create them.
- i had issue merging the cell, basically, i had 3 row and 3 cell. i want all the cell in the 1st row to be merge and the 2nd and 3rd cell in the 3rd row to be merge. The 2nd cell in the 2nd row will had 2 rows. Somehow i not able to merge the 1st row and the 3rd row with the condition.

If you need i can actually send you the aspx that i had created.

Hope to hear from you soon as this is quite urgent.

Regards,
Eric Tan
Commerce Online



Hi,

would like to find out how come after so many day, nobody actually response to my questions?

I thou aspose team should be very helpful and responsive?

Regards,
Eric Tan

Sorry we started the weekend early here and I went wakeboarding.

1. To insert Page x or y, just see what fields in MS Word you use for that.
2. Table cannot be centered yet because table alignment is not supported. You need to insert extra empty cells on the left and maybe on the right if you want.
3. Not sure about the different headers/footers question. Please give more details.

This example shows how to insert Page x of y and merge cells.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//Insert “Page x of y” into the header.

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.Write("Page “);

builder.InsertField(“PAGE”, “”);

builder.Write(” of ");

builder.InsertField(“NUMPAGES”, “”);

builder.InsertParagraph();

//First row, all cells merged

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.Write(“1.1”);

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.EndRow();

//Second row, 2nd and 3rd cells merged.

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write(“2.1”);

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.Write(“2.2”);

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.EndRow();

//Third row, no merge.
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Write(“3.1”);
builder.InsertCell();
builder.Write(“3.2”);
builder.InsertCell();
builder.Write(“3.3”);
builder.EndRow();
builder.EndTable();

This sample builds a table according to your “what i want” sample document.

To center the table I insert extra empty column on the left.
All is left to do is specify column widths, paragraph alignment and cell borders if you like.

The key with DocumentBuilder is you cannot split cells, you can only merge them. This means your table needs to have 4 rows, not 3.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

//1st row

builder.InsertCell();

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.Writeln(“Heading”);

builder.Writeln(“Sub heading”);

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.EndRow();

//2nd row

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.First;

builder.Writeln(“Title: eric”);

builder.Writeln(“eric”);

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.Write(“Document Number: 321”);

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.First;

//Insert “Page x of y” into the header.

builder.Write("Page “);

builder.InsertField(“PAGE”, “”);

builder.Write(” of ");

builder.InsertField(“NUMPAGES”, “”);

builder.EndRow();

//3rd row

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.Write(“Revision No: 50”);

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.Previous;

builder.EndRow();

//4th row

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.CellFormat.VerticalMerge = CellMerge.None;

builder.InsertCell();

builder.Writeln(“Issued by: eric”);

builder.InsertCell();

builder.Writeln(“Approved by: eric”);

builder.InsertCell();

builder.EndRow();

builder.EndTable();