PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Table Height
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4075
Page 1 of 1

Author:  JoshD123 [ Mon Dec 23, 2019 3:12 pm ]
Post subject:  Table Height

Hi,

I'm using MigraDoc to generate a PDF report which contains tables with varying content.

I need the last row to always fill the remaining space on the page, even if the content would not fill this space.

Any ideas on how I can achieve this?

Thanks.

Author:  IRlyDunno [ Mon Dec 23, 2019 6:53 pm ]
Post subject:  Re: Table Height

something like this might work.

Measure page height;
subtract top/bottom margin; table height (minus the last row);
set the height of the row with the remainder if any.


This method here should be a good guideline to get the height of the row, and it should also show you how to get the table height
Code:
public static int GetRowHeight(Table table)
{
   if(table== null || table.Rows.Count == 0)
      return 0f; // if empty the height is 0;
   
   // create a document to dump the objects that need measuring
    var docAux = new Document();
   
   var tableAux = doc.AddTable(); // Table to put the row that need to be measured
   tableAux.TopPadding = tableAux.BottomPadding = tableAux.LeftPadding = tableAux.RightPadding = 0;
   table.Columns = table.Columns.Clone(); // we need to set the columns, not sure if this works
   
   var row = (Row)table.Rows.LastObject; // get the last row of table
   table.Add(row.Clone()) // add the row to the aux table, not sure if this works either, but you should get the ideia
   
   // render the document
    MigraDocumentRenderer _renderer = new MigraDocumentRenderer(docAux);
    _renderer.PrepareDocument(); // this method should calculate the heights and widths of the objects.

   // get the objects
    var docObjects = _renderer.GetDocumentObjectsFromPage(1);
    var info = _renderer.GetRenderInfoFromPage(1);
   
   double height = 0;
   // Loop the objects, that in this case is only 1 row
    for (int i = 0; i < docObjects.Length; ++i)
    {
      // this if isn't really necessary, but I'm putting it here anyways in case there's trash,
      // or you want to measure other type i.e: Paragraphs, Images, etc...
      // note that the tableAux only has the row that you want to measure, this means, that if the table has 0 padding,
      // the ContentArea.Height here should be pretty close to the the row height.
        if (docObjects[i] is MigraTable)
        {
            var tableInfo = (MigraDoc.Rendering.TableRenderInfo)info[i];
            height += tableInfo.LayoutInfo.ContentArea.Height;
        }
    }

   // I return int just because I like my heights being integers, change if you want
    return Double.IsNaN(height) ? 0 : Convert.ToInt32(height);
}


So you could do something akin to this:

Code:
Document doc = new Document();
...

// Use the PageSetup of the section your working with and not the one of the Doc
var sec = doc.LastSection;

// Get the Height and width of the page
Unit width, height;
PageSetup.GetPageSize(sec.PageFormat, out width, out height);

// Get the height of the table
int tableHeight = MethodThatGetsTableHeight(Params...);

// Get the height of last Row of the table
int rowHeight = GetRowHeight(sec.LastTable);

// The height of both margins combined
int margins = Convert.ToInt32(sec.PageSetup.TopMargin + sec.PageSetup.BottomMargin);

// Calculate the leftover space
var lastRowHeight = height - (margins  + (tableHeight - rowHeight));

// now set the height of the last row
....



I have not written this code in a code editor so it should have errors, but still, it should help you on what to do.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/