PDFsharp & MigraDoc Foundation

Problem setting right cell border on merged cell
Page 1 of 1

Author:  ndkindt [ Fri May 11, 2018 6:40 pm ]
Post subject:  Problem setting right cell border on merged cell

As part of a project I am building a table that contains 6 columns and an unspecified number of rows (generated programatically.) I am running into an issue where I can't get a cell to display a right border when two conditions are met:
1. The cell is merged via merge right.
2. The cell merge carries to the last column.

In my case I am taking a cell in the first row (cell[2]) and merging it right 3 cells (for a total of 4). This carries to the last column.

If I merge it one column less it works fine. As it does when I apply a border to a cell in the last column without merging.

My work around right now is to create an additional column of a very small size and simply never use it. But I imagine I'm doing something wrong.

            Table tbl = new Table();

            Row tRow = tbl.AddRow();
            tRow.Cells[0].Borders.Width = 0.5;      // Works
            tRow.Cells[1].Borders.Width = 0.5;      // Works
            tRow.Cells[2].MergeRight = 3;
            tRow.Cells[2].Borders.Width = 0.5;      // No right border

Any help is appreciated.

Edit: The document has a width of 7.5in so the table is NOT exceeding the page edge.

Author:  JMcCready [ Fri Aug 10, 2018 10:00 am ]
Post subject:  Re: Problem setting right cell border on merged cell

  • Using MigraDoc Foundation
  • Using WPF
  • Using Nuget Package
  • Version: 1.50.4845.0

I have noticed a similar problem. I have noticed that this behaviour also applies to merging down to the last row.

I have discovered that if you set the border that is not displaying, in the final cell of the merge, it does apply.

         Table mergeBorderTable = section.AddTable();
         mergeBorderTable.Borders.Bottom.Width = 0.5;
         mergeBorderTable.Borders.Top.Width = 0.5;
         mergeBorderTable.Borders.Left.Width = 0.5;
         mergeBorderTable.Borders.Right.Width = 0.5;
         mergeBorderTable.Rows[0].Cells[1].MergeDown = 1;

         mergeBorderTable.Rows[0].Cells[1].Borders.Bottom.Width = 3;
         //The above line does NOT display

         //mergeBorderTable.Rows[1].Cells[1].Borders.Bottom.Width = 3;
         //The above line DOES display

I have noticed a second issue with styling borders when it comes to merging. If you set a border on a cell adjacent to a merged cell it can also set the same border on the other side of the merged cell. This can either set the whole length of the merged cell or just one rows/columns worth. In the case below it only set one row.

         Table tableStyleFix = section.AddTable();
         tableStyleFix.Borders.Right.Width = 0.5;
         tableStyleFix.Borders.Top.Width = 0.5;
         tableStyleFix.Borders.Left.Width = 0.5;
         tableStyleFix.Borders.Bottom.Width = 0.5;
         tableStyleFix.Borders.Color = TableBorder;

         tableStyleFix.Rows[0].Cells[1].MergeDown = 2;
         tableStyleFix.Rows[1].Cells[2].Borders.Left.Width = 3;

File comment: The file I used to run the project that demonstrates the problem. It contains a comment with the required command to download the appropriate references from nuget.
program.zip [820 Bytes]
Downloaded 61 times

Author:  JMcCready [ Wed Sep 12, 2018 11:09 am ]
Post subject:  Re: Problem setting right cell border on merged cell

I made a fix for the merge border bug that is a bit more convenient than adding an extra row/column and can just be run on your table after it has been made. The comments in the code should help to explain how the code fixes the bug (though I've likely butchered the explanation):

/// <summary>
/// This code handles an error produced by MigraDoc that arises when cells are merged. This error results in borders not displaying properly.
/// This code introduces a fix for this error.
/// Assigning the border to the cell that would have existed at the required position had it not been merged into, results in the border
/// rendering properly for the merged cell. This applies to both merged rows and merged columns.
/// Full Explanation of Error and how it is handled.
/// Post-Merge
///   [0]|1  2|
///   [3]|4  5|   [4 Cells]
///   [6]|7  8|
/// In this example, cells '1','2','4','5','7' & '8' are merged. Setting a BOTTOM BORDER to cell '1', the parent cell SHOULD result in the bottom border of the merged cell being set.
/// In effect, the bottom border of cells '7' & '8' SHOULD be set. This does not happen in a few scenarios.
/// To work around this error you instead set the BOTTOM BORDER to cell '7'. To do this for the RIGHT BORDER would mean setting cell '2'.
/// Setting the BOTTOM BORDER for the merged cell requires setting the bottom border for the bottom most cell in the column of the parent cell.
/// Setting the RIGHT BORDER for the merged cell requires setting the RIGHT BORDER for the right most cell in the row of the parent cell.
/// No work around is required for LEFT BORDER or TOP BORDER
/// This work around ONLY needs to apply if the merged cell is on the right most or bottom most edge of the table.
/// The error does not arise if there are cells between the merged cell and the edge of the table.
/// </summary>
/// <param name="migraTable"></param>
private void MigraDocBorderMergeBugFix(Table migraTable)
   for (int x = 0; x < migraTable.Columns.Count; x++)
      for (int y = 0; y < migraTable.Rows.Count; y++)
         var parentCell = migraTable.Rows[y].Cells[x];
         if (parentCell.MergeDown != 0)
            migraTable.Rows[y + parentCell.MergeDown].Cells[x].Borders.Bottom = parentCell.Borders.Bottom.Clone();

         if (parentCell.MergeRight != 0)
            migraTable.Rows[y].Cells[x + parentCell.MergeRight].Borders.Right = parentCell.Borders.Right.Clone();

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group