PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Sep 26, 2020 6:04 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Table Height
PostPosted: Mon Dec 23, 2019 3:12 pm 
Offline

Joined: Tue Dec 03, 2019 5:00 pm
Posts: 1
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Table Height
PostPosted: Mon Dec 23, 2019 6:53 pm 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 41
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.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 16 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group