PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 7:14 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri May 11, 2018 6:40 pm 
Offline

Joined: Fri May 11, 2018 6:28 pm
Posts: 1
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.

Code:
            Table tbl = new Table();
            tbl.AddColumn("1.0in");
            tbl.AddColumn("1.0in");
            tbl.AddColumn("1.0in");
            tbl.AddColumn("1.0in");
            tbl.AddColumn("1.0in");
            tbl.AddColumn("1.0in");

            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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 10, 2018 10:00 am 
Offline

Joined: Fri Aug 10, 2018 9:04 am
Posts: 2
  • 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.

Code:
         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.AddColumn();
         mergeBorderTable.AddColumn();
         mergeBorderTable.AddRow();
         mergeBorderTable.AddRow();
         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.


Code:
         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.AddColumn();
         tableStyleFix.AddColumn();
         tableStyleFix.AddColumn();
         tableStyleFix.AddRow();
         tableStyleFix.AddRow();
         tableStyleFix.AddRow();
         tableStyleFix.Rows[0].Cells[1].MergeDown = 2;
         tableStyleFix.Rows[1].Cells[2].Borders.Left.Width = 3;


Attachments:
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 331 times


Last edited by JMcCready on Wed Mar 06, 2019 9:14 am, edited 2 times in total.
Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 12, 2018 11:09 am 
Offline

Joined: Fri Aug 10, 2018 9:04 am
Posts: 2
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):

Code:
/// <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();
         }
      }
   }
}


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 138 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group