PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue May 29, 2018 7:46 am 
Offline

Joined: Tue May 29, 2018 6:43 am
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Tue May 29, 2018 7:59 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue May 29, 2018 11:42 am 
Offline

Joined: Tue May 29, 2018 6:43 am
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Wed May 30, 2018 8:47 am 
Offline

Joined: Tue May 29, 2018 6:43 am
Posts: 3
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!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 60 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