Table headers repeated per page

I’m having trouble trying to get the headers on a table to repeat across multiple pages.

I read this: https://docs.aspose.com/words/net/working-with-columns-and-rows/

I am not using the DocumentBuilder object to create the table as suggested in this article. I use a template that contains a table, and find it within the Document object. I then clone the template row and insert it at the end of the table like such:

var rowCLone = (Row)row.Clone(true);
table.InsertAfter(rowCLone, row);

The option builder.RowFormat.HeadingFormat = true; does not seem to apply unless I am building the table with the DocumentBuilder as suggested in the above post.

What I am looking for, is a way to retrospectively apply the option (to repeat the header per page), once the table has been created.

Something along the lines of:

var table = (Table)document.GetChild(NodeType.Table, 0, true);
table.RepeatHeaderPerPage(int32 numberOfRows);

Where numberOfRows is the number of rows at the top of the table to repeat at the top of each page.

Does anyone have any idea how to do this or should I start looking into modifying my code to build the table as suggested in the post?

Cheers

Hi there,

Thanks for your inquiry. I have tested the scenario using following code example and have not found any issue while using latest version of Aspose.Words for .NET 14.6.0. Please use Aspose.Words for .NET 14.6.0. I have attached the input and output documents with this post for your kind reference.

Document doc = new Document(MyDir + "TableHeader.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
table.FirstRow.RowFormat.HeadingFormat = true;
for (int i = 0; i < 200; i++)
{
    var rowCLone = (Row)table.LastRow.Clone(true);
    rowCLone.RowFormat.HeadingFormat = false;
    table.AppendChild(rowCLone);
}
doc.Save(MyDir + "Out.docx");

If you still face problem, please share following detail for investigation purposes.

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 the output Word file that shows the undesired behavior.
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.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi,

Template and output attached.

Code is as follows:

static void Main(string[] args)
{
    Console.WriteLine("Start");
    var document = new Document("template.docx");
    var builder = new DocumentBuilder(document);
    builder.Font.Name = "Arial";

    var table = (Table)document.GetChild(NodeType.Table, 0, true);
    table.FirstRow.RowFormat.HeadingFormat = true;

    var updateCells = table.GetChildNodes(NodeType.Cell, true).ToArray().Where(c => c.GetText().Contains("{{"));
    var editCell = updateCells.First();
    var row = ((Cell)editCell).ParentRow;

    for (int count = 0; count < 100; count++)
    {
        updateCells = table.GetChildNodes(NodeType.Cell, true).ToArray().Where(c => c.GetText().Contains("{{"));
        row.RowFormat.HeadingFormat = false;

        if (count != 99)
        {
            var rowCLone = (Row)row.Clone(true);

            // Insert cloned table after current onebuilder.
            table.AppendChild(rowCLone);
            row = rowCLone;
        }

        foreach (var updateCell in updateCells)
        {
            ((Cell)updateCell).Paragraphs.Clear();
            ((Cell)updateCell).Paragraphs.Add(new Paragraph(document));
            builder.MoveTo(((Cell)updateCell).FirstParagraph);
            builder.Writeln(count.ToString());
        }
    }
    document.Save("output.pdf", SaveFormat.Pdf);
    Console.WriteLine("End");
    Console.Read();
}

I can’t see what I am doing wrong here. Using version 14.6 of words

Thanks

UPDATE:

I have copy/pasted your code and swapped in the document template that I have. Same result. It does work with your template though.

Could it be an issue with the template I am using?

Or is there a setting I may have checked in the document that’s stopping this from working?

Cheers

Hi there,

Thanks for sharing the detail. I have tested the scenario and have managed to reproduce the same issue at my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10577. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Tahir,

Can I get an ETA on this please?

Cheers

UPDATE:

I am having the same issue with ParagraphFormat.KeepWithNext.

When I use your template, it works fine. When I use my template, the command is ignored and the table headers are split from the data.

Is this related?

Hi there,

Thanks for your inquiry.
Jamadan:

Can I get an ETA on this please?

I would
like to share with you that
issues are addressed and resolved based on first come first serve
basis. Currently, your issue is pending for analysis and is in the
queue. I am afraid, I can’t provide you any reliable estimate at the
moment. Once your issue is analyzed, we will then be able to provide you
an estimate. Thank you for your patience.
Jamadan:

I am having the same issue with ParagraphFormat.KeepWithNext.
When I use your template, it works fine. When I use my template, the command is ignored and the table headers are split from the data.

It would be great if you please share the code which you are using for your template document. I will investigate the issue and provide you more information.

Code as follows:

static void Main(string[] args)
{
    Console.WriteLine("Start");
    var document = new Document("template.docx");

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

    for (int i = 0; i < 200; i++)
    {
        foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true))
        {
            //call this method if table's cell is created on the fly
            //newly created cell does not have paragraph inside
            cell.EnsureMinimum();
            foreach (Paragraph para in cell.Paragraphs)
                if (!(cell.ParentRow.IsLastRow && para.IsEndOfCell))
                    para.ParagraphFormat.KeepWithNext = true;
        }

        //vary up the number of rows beneath the header
        if (i == 1 || i == 2 || i == 3)
        {
            var rowClone = table.LastRow.Clone(true);
            table.InsertAfter(rowClone, table.LastRow);
        }
        var tableClone = table.Clone(true);
        document.FirstSection.Body.InsertAfter(tableClone, table);

        table = (Table)tableClone;
    }

    document.Save("output_template.docx", SaveFormat.Docx);
    Console.WriteLine("End");
    Console.Read();
}

Templates Attached. Same issue, seems to work with the template you provided but not with the one i have created.

I understand that, but surely you can give me a rough estimate? Is it 1 month (in which case I will wait for the fix), or will it be 6 months (in which case I will have to consider another product)?

Hi there,

Thanks for sharing the
detail.
Jamadan:

Code as follows:

static void Main(string[] args)
{
…
}

Templates Attached. Same issue, seems to work with the template you provided but not with the one i have created.

I have tested the scenario and have managed to reproduce the
same issue at my side. For the sake of correction, I have logged this
problem in our issue tracking system as WORDSNET-10592. I have linked
this forum thread to the same issue and you will be notified via this
forum thread once this issue is resolved. We apologize for your inconvenience.
Jamadan:

I understand that, but surely you can give me a rough estimate? Is it 1 month (in which case I will wait for the fix), or will it be 6 months (in which case I will have to consider another product)?

Currently, your issues are pending for analysis and are in the queue. I am afraid, I can’t provide you any reliable estimate at the moment. Once your issue are analyzed, we will then be able to provide you an estimate.

Hi there,

Thanks for your patience.

It is to inform you that our development team has completed the work on the issue (WORDSNET-10577)
and has come to a conclusion that this issue and the undesired behavior
you’re observing is actually not a bug in Aspose.Words. So, we have
closed this issue as ‘Not a Bug’.

Please note that Aspose.Words mimics the same behavior as MS Word does. The table inside your template document is floating table. MS Word doesn’t repeat heading row on each page in this case. You can achieve your requirements by setting Text Wrapping of Table to None.

Using MS Word, Select table -> Table Properties -> Text Wrapping None.

or it can be made programmatically:

table.TextWrapping = TextWrapping.None;

Please check the following highlighted code snippet.

var document = new Document(MyDir + "template.docx");
var builder = new DocumentBuilder(document);
builder.Font.Name = "Arial";
Table table = (Table)document.GetChild(NodeType.Table, 0, true);
table.TextWrapping = TextWrapping.None;
table.FirstRow.RowFormat.HeadingFormat = true;
var updateCells = table.GetChildNodes(NodeType.Cell, true).ToArray().Where(c => c.GetText().Contains("{{"));
var editCell = updateCells.First();
var row = ((Cell)editCell).ParentRow;
for (int count = 0; count < 100; count++)
{
    updateCells = table.GetChildNodes(NodeType.Cell, true).ToArray().Where(c => c.GetText().Contains("{{"));
    row.RowFormat.HeadingFormat = false;
    if (count != 99)
    {
        var rowCLone = (Row)table.LastRow.Clone(true);
        // Insert cloned table after current onebuilder.
        table.AppendChild(rowCLone);
        //row = rowCLone;
    }
    foreach (var updateCell in updateCells)
    {
        ((Cell)updateCell).Paragraphs.Clear();
        ((Cell)updateCell).Paragraphs.Add(new Paragraph(document));
        builder.MoveTo(((Cell)updateCell).FirstParagraph);
        builder.Writeln(count.ToString());
    }
}
document.Save(MyDir + "Out.pdf");
document.Save(MyDir + "Out.docx");

Hi there,

Further to my last post, we have
closed WORDSNET-10592 issue as ‘Not a Bug’. This issue is duplicate of WORDSNET-10577. Please set the ‘Text Wrapping’ of tables in your document as ‘None’ to fix these issue. Please check the code shared in my previous post.

Please let us know if you have any more queries.

The issues you have found earlier (filed as WORDSNET-10592) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

A post was split to a new topic: Get tables from document