facebooktwittermenuarrow-up

GemBox Support Forum

Borders for a table overflowing to next sheet

I am generating a table with a header row and multiple data rows dynamically(ie through for/for each loops)
The header row cells are supposed to have all the borders(ie left, right, top, bottom) and I am able to achieve that.
The data row cells should NOT have the top and bottom row.
Since the table has very high number of data rows it ends up overflowing to the next page but without the bottom border. I need to have a border at bottom every time the table overflows to next page. (Screen shot below)

I have tried adding the outside borders under MultipleBorderTypes but of no use.
Requesting for any suggestions.

Hi Rashika,

How about you set those header rows to repeat on each page.

In other words, set their TableRow.RowFormat.RepeatOnEachPage property to true.

Does this solve your issue?

Regards,
Mario

Hello Mario,

I am already using that property and the header gets repeated on each page without issues.
The problem is with the border of table on overflowing.

I see now, you’re talking about cells before the overflowing, the ones that are left on the previous page.
Unfortunately, Word documents don’t have an option that would help you with that.

We could check what is the last row on a page and add bottom borders on its cells.
But that wouldn’t work if you have the TableRow.RowFormat.AllowBreakAcrossPages set to false.

That’s because in that case the cells could be split between the two pages, so if we set the bottom borders on them they will appear on both pages.

bottomborders

Anyway, can you save your current resulting document in DOCX format and send it to us?
We would investigate if there is any way to achieve your requirement.

Regards,
Mario

Hi Mario ,

Thank you for the reply.
Can you please tell me what is the way to check for last row on a page?

I have tried for table.Rows.Last() but that only works for the last row of the table.

As for the document, I will try to send it to you soon.

Regards,
Rashika

Hi Rashika,

After some investigation, I noticed that in most cases the outline borders or Table element should work.
So, can you try using the following:

table.TableFormat.Borders.SetBorders(MultipleBorderTypes.Bottom, BorderStyle.Single, Color.Black, 1);

If the problem remains, perhaps you could try using the following (but again, I think there is probably a simpler way how you could do this):

var document = DocumentModel.Load("input.docx");

// Add PAGE fields at the beginning of each TableRow in top-level Tables,
// the ones that are direct child elements of a Section.
var topTablesFields = new List<List<Field>>();
var pageField = new Field(document, FieldType.Page) { CharacterFormat = { Hidden = true } };
foreach (var table in document.Sections
    .SelectMany(section => section.GetChildElements(false, ElementType.Table))
    .Cast<Table>())
{
    var singleTableFields = new List<Field>();
    topTablesFields.Add(singleTableFields);

    foreach (var row in table.Rows)
    {
        var position = row.Content.Start.InsertRange(pageField.Content);
        singleTableFields.Add((Field)position.Parent);
    }
}

// Update PAGE fields.
document.GetPaginator(new PaginatorOptions() { UpdateFields = true });

// Update cell borders based on page numbers.
foreach (var tableFields in topTablesFields)
{
    string previousPage = tableFields.First().Content.ToString();

    for (int index = 0; index < tableFields.Count; index++)
    {
        string currentPage = tableFields[index].Content.ToString();

        if (currentPage != previousPage)
        {
            // Get row of the previous PAGE field,
            // the one that occurs before the page break, before the table is split.
            Element parent = tableFields[index - 1];
            while (parent.ElementType != ElementType.TableRow)
                parent = parent.Parent;
            var row = (TableRow)parent;

            foreach (var cell in row.Cells)
                cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Bottom, BorderStyle.Single, Color.Black, 1);
        }

        previousPage = currentPage;
    }

    tableFields.ForEach(field => field.Content.Delete());
}

document.Save("output.pdf");

Last, if you could send us your document we would be able to investigate your exact scenario and help you out with it.

Regards,
Mario

Hi Mario,

Apologies for the late reply.

Unfortunately, I cannot share the resulting document with you.
But I can try to explain you in detail,

I want to generate table with a header row with all borders visible.
The data rows will be generated further which shouldn’t have any borders between two rows.

Following is the flow which i am following

  1. Generating header row with multiple cells having all the borders.
  2. Generating the data rows through a for loop with all cells without any borders.
  3. After table generation, adding the outside border and Inside vertical borders.

Apparently the outside border and inside vertical borders are not getting added to the table , hence the last row of table at each page does not have a bottom border.

Can you suggest what is a standard flow to generate a table.?
Regards
Rashika

Hi Rashika,

Please try the following code:

var document = new DocumentModel();

var table = new Table(document);

var headerRow = new TableRow(document,
    new TableCell(document, new Paragraph(document, "Column 1")),
    new TableCell(document, new Paragraph(document, "Column 2")),
    new TableCell(document, new Paragraph(document, "Column 3")))
{
    RowFormat = { RepeatOnEachPage = true }
};

foreach (var cell in headerRow.Cells)
    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Red, 2);

table.Rows.Add(headerRow);

for (int i = 1; i <= 120; ++i)
{
    var row = new TableRow(document,
        new TableCell(document, new Paragraph(document, "Column 1 Row " + i)),
        new TableCell(document, new Paragraph(document, "Column 2 Row " + i)),
        new TableCell(document, new Paragraph(document, "Column 3 Row " + i)));

    table.Rows.Add(row);
}

table.TableFormat.Borders.SetBorders(MultipleBorderTypes.Outside | MultipleBorderTypes.InsideVertical, BorderStyle.Single, Color.Green, 2);
table.TableFormat.Borders.SetBorders(MultipleBorderTypes.InsideHorizontal, BorderStyle.None, Color.Empty, 0);

document.Sections.Add(new Section(document, table));

var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
document.Save(Path.Combine(desktop, "Table.docx"));
document.Save(Path.Combine(desktop, "Table.pdf"));

Hello,

Thank you so much for this suggestion.
It seems to have worked (I suspect the explicit removal of InsideHorizontal borders is what has worked).
I am now able to obtain the required functionality.

Thanks again.

Regards
Rashika