How to add table in Header and Footer using C#

Hi,

I am looking to add table (with multiple rows and columns) in header and footer. Could you please share the option in code for this?

This is what i am looking for.

Appreciate any help.



Regards,
Ansu

Hi Ansu,

Thanks for your inquiry. DocumentBuilder has an internal cursor that you can navigate to a different location in a document using various DocumentBuilder.MoveToXXX methods. Please move the cursor to the header of document using DocumentBuilder. MoveToHeaderFooter and insert table. Please read about moving cursor and inserting table from here:
Using DocumentBuilder to Modify a Document Easily
Use DocumentBuilder to Insert Document Elements

Please let us know if you have any more queries.

Hi,

Thanks for the answer. But we are using to insert this table in the word header. The example provided mainly adding table, not in header.

Regards,
Balu

Hi Balu,

Thanks for your inquiry. Please move the cursor to the header of document using DocumentBuilder. MoveToHeaderFooter and insert the table. I suggest you please read about moving cursor in the document from here:

Please use following code example to achieve your requirements and let us know if you have any more queries.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

Table table = builder.StartTable();

// Insert a cell

builder.InsertCell();

builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

builder.Write("This is row 1 cell 1");

// Insert a cell

builder.InsertCell();

builder.Write("This is row 1 cell 2");

builder.EndRow();

// Insert a cell

builder.InsertCell();

builder.Write("This is row 2 cell 1");

// Insert a cell

builder.InsertCell();

builder.Write("This is row 2 cell 2");

builder.EndRow();

builder.EndTable();

doc.Save(MyDir + "Out.docx");

Thanks Tahir,

It did insert the table into the header. But it is adding an extra page to it. I am attached the word and converted PDF.

Could you please tell what needs to be done.

Regards,
Balu

Hi Balu,

Thanks for your inquiry. Please note that MS Word document is flow document and Aspose.Words mimics the same behavior as MS Word does. If you insert the same table in your input document using MS Word and convert it to Docx/Pdf, you will get the same output. Please try to insert some more contents in header of document using MS Word and check the page count.

In your case, I suggest you please use ParagraphFormat.SpaceAfter property as shown below. Hope this helps you.

Document doc = new Document(MyDir + "Fdddsg.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.ParagraphFormat.SpaceBefore = 0;

builder.ParagraphFormat.SpaceAfter = 0;

Table table = builder.StartTable();

// Insert a cell

builder.InsertCell();

builder.Write("This is row 1 cell 1");

// Insert a cell

builder.InsertCell();

builder.Write("This is row 1 cell 2");

builder.EndRow();

// Insert a cell

builder.InsertCell();

builder.Write("This is row 2 cell 1");

// Insert a cell

builder.InsertCell();

builder.Write("This is row 2 cell 2");

builder.EndRow();

builder.EndTable();

doc.Save(MyDir + "Out.docx");

doc.Save(MyDir + "Out.pdf");

Hi Tahir,

Thanks for the answer.

Just a guess, it might be because of the extra text inserted. “Evaluation Only. Created with Aspose.Words. Copyright 2003-2015 Aspose Pty Ltd.”.

That should be fine i guess as it is behaving the same way as in word.

Another question,

Is there any way to remove the border for table or make the border thickness as 0.
I tried.

table.ClearBorders();
table.ClearShading();
But the table border is still there.

Regards,
Balu

Hi Balu,

Thanks for your inquiry. Please use Table.ClearBorders method to remove all table and cell borders on a table. See the following code example for your kind reference.

Please let us know if you have any more queries.

Document doc = new Document(MyDir + "Table.Document.doc");

// Remove all borders from the first table in the document.

Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

// Clear the borders all cells in the table.

table.ClearBorders();

doc.Save(MyDir + "Table.ClearBorders Out.doc");