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

Automatic column width on tables
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3797
Page 1 of 1

Author:  ReineE [ Tue May 29, 2018 7:46 am ]
Post subject:  Automatic column width on tables

Hi

I am trying to create a generic report framework for our application and would like it to actually fit the column width to the data instead of setting a fixed width per column. Is that a possibility today? (using 1.50-rc2)

If it's not possible, I guess I could update each column width by getting the width of the contained text for each row I add as well, in that case, how do I get the width of the text in the cell?

Best regards
Reine

Author:  Thomas Hoevel [ Tue May 29, 2018 7:59 am ]
Post subject:  Re: Automatic column width on tables

Hi!
ReineE wrote:
[...] in that case, how do I get the width of the text in the cell?
Maybe look here:
viewtopic.php?f=8&t=3196

Bear in mind that table cell texts will break to new lines if they do not fit the assigned width of the column.
Depending on the priorities in your algorithm you can get many different results for "automatic column widths". No such algorithm is implemented in MigraDoc yet.

Author:  ReineE [ Tue May 29, 2018 11:42 am ]
Post subject:  Re: Automatic column width on tables

Hi

Yes, I am aware of that I can get interesting result with an automatic column width :)

Most of the times we won't have an issue with it, it would just be easier for us if there was an option like that. We might be able to actually set static width's as well, but the combination of to have it automatic in most cases and fixed widths in special cases would be a great solution for us.

Thanks for the link, I will take a look at that and check which will be the best way forward for us.

BR
Reine

Author:  ReineE [ Wed May 30, 2018 8:47 am ]
Post subject:  Re: Automatic column width on tables

Hi again

Short report of the outcome :)

Looks good and works good for our purpose, the only small issue that I encountered was that when I created a style from another style like this:
Code:
style = doc.Styles.AddStyle("TableHeader", "Normal");

It did not include the font in the new style as the renderer unwrapped it and looked at the parent, so I just had to set the font and size in the style and it worked like a charm.

So how I do it is pretty much like this:
Code:
TextMeasurement MeasTextTableHead = new TextMeasurement(doc.Styles["TableHeader"].Font);
TextMeasurement MeasTextTableText = new TextMeasurement(doc.Styles["TableText"].Font);

int i = 1;   
foreach (string head in table.TableHeader)
{
    paragraph = row.Cells[i].AddParagraph(head);
    paragraph.Style = "TableHeader";
    textSize = MeasTextTableHead.MeasureString(head, UnitType.Millimeter);
    row.Cells[i].Column.Width = Unit.FromMillimeter(textSize.Width+5);
    i++;
}
Unit unit;
foreach(List<string> rowData in table.TableData)
{
    row= pdfTable.AddRow();
    i = 0;
    foreach(string value in rowData)
    {
        paragraph=row.Cells[i].AddParagraph(value);
        if (i == 0)
        {
            paragraph.Style =  "TableHeader";
            textSize = MeasTextTableHead.MeasureString(value, UnitType.Millimeter);
            unit= Unit.FromMillimeter(textSize.Width + 5);
            row.Cells[i].Column.Width = (row.Cells[i].Column.Width < unit) ? unit : row.Cells[i].Column.Width;
        }
        else
        {
            paragraph.Style = "TableData";
            textSize = MeasTextTableText.MeasureString(value, UnitType.Millimeter);
            unit = Unit.FromMillimeter(textSize.Width + 5);
            row.Cells[i].Column.Width = (row.Cells[i].Column.Width < unit) ? unit : row.Cells[i].Column.Width;
        }
        i++;
    }
}


Thank you very much!

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