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

Table headers unusually large
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2181
Page 1 of 1

Author:  somnatic [ Sat Oct 27, 2012 8:06 pm ]
Post subject:  Table headers unusually large

Hi

At first, let me congratulate you to MigraDoc + SharpPDF, two great products. I started using them yesterday and have come quite far already. Sometimes it requires a Change in thinking but I haven't come across a real Problem yet (except this one :) ).

I try to draw a table with 8 columns where each cell only contains some numbers. Therefore I'd like to write the headers rotated by 90°. I got this working, but there is an unusual large space after the text and I do not know where this comes from. I made a Picture of the resulting PDF so you can see what I mean.

The code Looks like this:
Code:

            Table t = InitializeTable(doc, s, 8, Unit.FromMillimeter(15));
            t.AddHeaderRowToTable(descs, colors);


Where the second function is an Extension method to the MigraDoc "Table".

Code:
       
        private static Table InitializeTable(Document doc, Section s, int nrColumns, Unit colWidth)
        {
            Table t = s.AddTable();
            t.Borders = new Borders {Visible = true, Width = 0.1};

            for (int i = 0; i < nrColumns; i++)
            {
                t.AddColumn(colWidth);
            }

            double leftIndentForTable = (doc.DefaultPageSetup.PageWidth.Millimeter - doc.DefaultPageSetup.LeftMargin.Millimeter - doc.DefaultPageSetup.RightMargin.Millimeter - (t.Columns.Count * t.Columns[0].Width.Millimeter)) / 2;
            t.Rows.LeftIndent = Unit.FromMillimeter(leftIndentForTable);

            return t;
        }

public static void AddHeaderRowToTable(this Table t, List<string> descriptions, List<Color> backgroundColors)
        {
            Row rFirst = t.AddRow();
            //rFirst.HeadingFormat = true;

            for (int i = 0; i < descriptions.Count; i++)
            {
                TextFrame tr = rFirst.Cells[i].AddTextFrame();
                rFirst.Cells[i].Shading.Color = backgroundColors[i];
                rFirst.Cells[i].Shading.Visible = true;
                rFirst.Cells[i].Format.Alignment = ParagraphAlignment.Center;
                tr.Orientation = TextOrientation.Upward;
               
                tr.AddParagraph(descriptions[i]);

            }
        }


Do you have any idea what I am doing wrong? Why is there so much space above the table Header?

btw: Embedding Graphs made by OxyPlot works very nicely.

Attachments:
2012-10-27_21-57-11.png
2012-10-27_21-57-11.png [ 106.48 KiB | Viewed 9906 times ]

Author:  somnatic [ Sat Oct 27, 2012 8:48 pm ]
Post subject:  Re: Table headers unusually large

Update (but not yet resolved):

It seems to have to do with the TextFrame. If I do not add a Textframe but only a Paragraph, the Height appears to be fine.
Yet, I Need to add the textframe because I want the text rotated ;)

Strange.

Author:  somnatic [ Sun Oct 28, 2012 4:17 pm ]
Post subject:  Re: Table headers unusually large

I also forgot to add the Version numbers I'm using:
PdfSharp: 1.32.2602.0
MigraDoc: 1.32.3879.0
(These are the Version which NuGet retrieved)

Author:  Thomas Hoevel [ Mon Oct 29, 2012 10:05 am ]
Post subject:  Re: Table headers unusually large

Hi!
somnatic wrote:
Why is there so much space above the table Header?
I don't know.
Maybe the row height is determined by the width of the textframes. You do not set the width - and I don't know which value will be used.

Try setting the width of the textframe (maybe to 1 cm) to see if that makes a difference.

Author:  somnatic [ Mon Oct 29, 2012 8:05 pm ]
Post subject:  Re: Table headers unusually large

Hi

Been there, done that:


Code:
TextFrame tr = rFirst.Cells[i].AddTextFrame();
tr.Width = Unit.FromMillimeter(15);
tr.Height = Unit.FromMillimeter(15);
(tried this in all combinations)


Basically, setting the Height has an effect, but my text will then overflow if it's too small or the frame will be too large.
Setting the Width, does not have an effect at all.

Author:  Thomas Hoevel [ Tue Oct 30, 2012 10:34 am ]
Post subject:  Re: Table headers unusually large

somnatic wrote:
Basically, setting the Height has an effect, but my text will then overflow if it's too small or the frame will be too large.
You can use gfx.MeasureString() from PDFsharp to calculate the width (in your case height) a specific text will need. You may have to add a few points for border and padding.
And if you set the correct width for the paragraph in the textframe, the text should wrap to the next line (only needed if you want long labels to wrap).

IMHO setting the Height to a value calculated from the length of label texts is the way to go.

Author:  sgtKK [ Thu Dec 05, 2013 2:49 pm ]
Post subject:  Re: Table headers unusually large

somnatic wrote:
Hi
btw: Embedding Graphs made by OxyPlot works very nicely.


Hi, Can you post an example of how you got this working please ?

I'm trying to use MigraDoc with OxyPlot, but can't see a way to render OxyPlot to a MigraDoc document.
I can get OxyPlot to render to a BitmapSource, but can't find a way to add that to a migradoc (without saving it as a temp file first, which I don't like really)

Thanks in advance

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