Display number of rows of a table in the footer of each page

Hello,
I have a MS Word template which contains a table. This table is filled with Aspose, ExecuteWithRegions(), and turns to a long multi-page table.
I would like to display the number of rows present in the page, in the footer of each page of the document.
I have already made a method that returns the number of rows of the table for each page, but I have no idea how to display this information. How can I manage to have different values in a field in the footer without having different sections ?
Thank you,
Mélodie

Hi Mélodie,

Thanks for your inquiry. As per my understanding, you want to copy the table from document’s body into footer of the section. Please use following code example to achieve this. Moreover, please note that Aspose.Words mimics the same behavior as MS Word does. You can only insert specific contents into the footer of a section. If table’s contents are large, all contents will not be visible in the footer of section.

Document doc = new Document(MyDir + "in.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
HeaderFooter footer = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary];
if (footer == null)
{
    footer = new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
    doc.FirstSection.HeadersFooters.Add(footer);
}
footer.AppendChild(table.Clone(true));
table.Remove();
doc.Save(MyDir + "Out.docx");

*melodiemaire:

How can I manage to have different values in a field in the footer without having different sections ?*

I suggest you please insert the footer contents inside IF filed. You can show the desired contents based on IF condition. Hope this helps you.

If you still face problem, please share following detail here for our reference.

  • Please attach your input Word document.

  • Please create a standalone/runnable simple application (for example a Console
    Application Project
    ) that demonstrates the code (Aspose.Words code) you used to generate your output document

  • Please attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document.

I will investigate as to how you are expecting your final document be generated
like.

Hello,
Thanks for your prompt answer.
Actually, I don’t want to copy the entire table in the footer of my document, but I want to display the number of rows that are present in each page.
You will find in attachement a Console Application, which contains :

  • A template : Template_items.docx
  • A program : Program.cs, which merge the template into a final document, so you can understand what kind of document I want.

I almost get the document I need, except I would like the « IF » clause in the header to be generic (Mine is only working for 2 pages documents).
Do not hesitate I you need any additionnal information.
Mélodie

Hi Mélodie,

Thanks for your inquiry. Please note that a Section can have one Body and maximum one HeaderFooter of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section.

The contents of a section may have multiple pages. The text in the HeaderFooter will be same for all pages. In your case, I suggest you please insert the ‘number of rows per page’ in the body of section as you explained in Out_items_expected.docx.

Please let us know if you have any more queries.

Hello,

Thank you for your answer.

I am afraid I don’t understand.
If I insert the ‘number of rows per page’ in the body of section, how can I tell Aspose in my template to display it in each page ?
Have you tried with the template I attached ?

Mélodie

Hi Mélodie,

Thanks
for your inquiry. Please note that in evaluation mode there are some limitations applied. E.g Aspose.Words injects an evaluation watermark at the top of the document. The document’s content are truncated after a certain number of paragraphs during export.

To
avoid this you can request a free 30-day trial license which removes
these evaluation restrictions. You can request this from here:
http://www.aspose.com/corporate/purchase/temporary-license.aspx

Please use the following code example to achieve your requirements. I have attached the output document with this post for your kind reference.

Hope this helps you. Please let us know if you have any more queries.

// Construction and merge of data
var items = new List<string>();
for (var i = 0; i < 25; i++)
{
    items.Add("Item" + i);
}
var itemsMailMergeDataSource = new ItemsMailMergeDataSource(items);
var docEmargement = new Document(@"../../Template_items.docx");
docEmargement.MailMerge.ExecuteWithRegions(itemsMailMergeDataSource);
docEmargement.UpdatePageLayout();
// Construction and merge of number of rows par page 
var numberOfRowsPerPage = GetNumberOfRowsForEachPage(docEmargement, true);
var variablesName = new List<string>();
var datas = new List<object>();
foreach (var numberOfRowsInPage in numberOfRowsPerPage)
{
    variablesName.Add("NumberPage" + numberOfRowsInPage.Key.ToString());
    datas.Add(numberOfRowsInPage.Value.ToString());
}
LayoutCollector collector = new LayoutCollector(docEmargement);
int pageIndex = 1;
foreach (Row row in docEmargement.GetChildNodes(NodeType.Row, true))
{
    if (collector.GetStartPageIndex(row) == pageIndex)
    {
        Shape shape = new Shape(docEmargement, Aspose.Words.Drawing.ShapeType.TextPlainText);
        shape.Width = 300;
        shape.Height = 20;
        shape.Left = -20;
        shape.Top = -20;
        row.FirstCell.FirstParagraph.AppendChild(shape);
        shape.AppendChild(new Paragraph(docEmargement));
        var numberOfRows = numberOfRowsPerPage[pageIndex];
        Run run = new Run(docEmargement);
        run.Text = "Number of rows in this page : " + numberOfRows.ToString();
        shape.FirstParagraph.AppendChild(run);
        pageIndex++;
    }
}
docEmargement.Save(@"../../Out_items.docx");

This is exactly what I was looking for.
Thank you very much, you have been really helpfull !

Mélodie

Hi Mélodie,

Thanks
for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.