How to insert table at the bottom of footer using .NET

Hello.

I have a table that I am trying to add to the bottom of an existing footer which has a generic format (page # of ##) I am able to find the footer and then using builder create the table and it adds fine. The problem I am encountering is that it is always added to the top of the footer instead of the bottom and for the life of me I cannot find to to insert the table AFTER the existing footer item.

What I would like to get is:

Footer Start
Page # of ## (From Word Document)
Table (Added by Aspose)
Footer End

But what I get is :

Footer Start
Table (Added by Aspose)
Page # of ## (From Word Document)
Footer End

Thanks

Aspose.Words.Document doc = new Aspose.Words.Document(@procFileName.ToString());
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
Aspose.Words.Tables.Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
// Row 1 Cell 1
builder.Write(“Blah Blah Cell 1”);
// Insert a cell
builder.InsertCell();
// Row 1 Cell 2
builder.Write(“Blah 2”);
builder.EndRow();
// Insert a cell
builder.InsertCell();
// Row 2 Cell 1
builder.Write(“Blah 3”);

            // Insert a cell
            builder.InsertCell();
            // Row 2 Cell 2 - EMPTY
            builder.Write("");
            builder.EndRow();
            builder.EndTable();

            doc.Save(@convertDIR + inFileName.ToString());

@dlaskey

Please ZIP and attach your input, problematic output and expected output documents 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.

Here are the two files. .ZIP contains the word doc, and .pdf the converted file with table added. Again, I would like the page number line to come before the table, not after.

Thanks

19-251cf iris.zip (102.8 KB)
Converted_19-251cf_iris_dl556 (13).pdf (243.8 KB)

@dlaskey

In your case, you need to move the cursor after the first paragraph of footer. You can do it by simply adding following line of code

builder.MoveTo(builder.CurrentParagraph);

Please use following code example to get the desired output.

Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "19-251cf iris.docx");
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.MoveTo(builder.CurrentParagraph);
Aspose.Words.Tables.Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
// Row 1 Cell 1
builder.Write("Blah Blah Cell 1");
// Insert a cell
builder.InsertCell();
// Row 1 Cell 2
builder.Write("Blah 2");
builder.EndRow();
// Insert a cell
builder.InsertCell();
// Row 2 Cell 1
builder.Write("Blah 3");

// Insert a cell
builder.InsertCell();
// Row 2 Cell 2 - EMPTY
builder.Write("");
builder.EndRow();
builder.EndTable();

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

Thanks. That worked.

Related question, is there a way to clear the footer and then just put my own things in? I tried at first doing it this way to clear the footer and then write my own page number and then table, but the table would never show, so I was guessing I was overrunning the footer space and had an extra line, as well as was not getting rid of the original footer page numbering.

I was trying things around builder.CurrentSection.ClearContent() and that did not work. Should I have tried CurrentParagraph?

I was able to remove the footers, but then when tried to add the content, I was only getting my added page number line, and again, that is because I think I was unable to set the footer size properly since it seemed lower and I did not have the half inch at the bottom anymore.

Thanks

@dlaskey

Yes, you can remove the header/footer content and insert your own content in it using Aspose.Words. You can remove the header or footer using HeaderFooter.Remove method. You can also remove all header and footer of section using HeaderFooterCollection.Clear method.

Following code example removes the content of footer of first section and insert new content in it.

Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "19-251cf iris.docx");
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

doc.Sections[0].HeadersFooters[HeaderFooterType.FooterPrimary].Remove();

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("\nThis is page ");
builder.InsertField(FieldType.FieldPage, false);

Aspose.Words.Tables.Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
// Row 1 Cell 1
builder.Write("Blah Blah Cell 1");
// Insert a cell
builder.InsertCell();
// Row 1 Cell 2
builder.Write("Blah 2");
builder.EndRow();
// Insert a cell
builder.InsertCell();
// Row 2 Cell 1
builder.Write("Blah 3");

// Insert a cell
builder.InsertCell();
// Row 2 Cell 2 - EMPTY
builder.Write("");
builder.EndRow();
builder.EndTable();

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

Moreover, you can remove the empty space from the header and footer using Aspose.Words. The empty space is empty paragraphs in your document. Please use Paragraph.HasChildNodes property to check if paragraph has child nodes or not. If this property returns false, you can remove the paragraph using Paragraph.Remove method.