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

Vertical alignment of text in a table cell with image & text
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2498
Page 1 of 1

Author:  claus.net [ Wed Jul 03, 2013 12:20 pm ]
Post subject:  Vertical alignment of text in a table cell with image & text

Hi,

I have table cells where I add a paragraph.

To this paragraph, I'm adding an image and some text.

While the image respects vertical alignment settings (for example top aligned) in the cell, the text is always at the bottom of the image. How do I get the text to be for example top or center aligned?

The code looks like this:

Code:
paragraph = row.Cells[3].AddParagraph();
paragraph.AddImage("filename"));
paragraph.AddText("Some text");


Thanks in advance!

Author:  Thomas Hoevel [ Wed Jul 03, 2013 1:34 pm ]
Post subject:  Re: Vertical alignment of text in a table cell with image &

Hi!

MigraDoc treats the image like a character. Text will be to the right of the image if it fits (text vertically aligned at the bottom of the image) or below the image.
Add the text before the image and it will be the other way around.

Search for "WrapStyle.Through" for other options (text layouter will then ignore the image).

See also:
viewtopic.php?p=6820#p6820
viewtopic.php?p=5267#p5267

What are you trying to achieve?

Author:  claus.net [ Wed Jul 03, 2013 1:52 pm ]
Post subject:  Re: Vertical alignment of text in a table cell with image &

Look at the attached image file for what I'm trying to achieve.
I want the text 'Regular' to be vertically centered like the text 'Snowboard Standposition' so that both texts are on the same line.

I tried using "WrapStyle.Through" before but it didn't seem to change anything.

Attachments:
Untitled.gif
Untitled.gif [ 1.98 KiB | Viewed 28514 times ]

Author:  Thomas Hoevel [ Wed Jul 03, 2013 3:25 pm ]
Post subject:  Re: Vertical alignment of text in a table cell with image &

claus.net wrote:
I tried using "WrapStyle.Through" before but it didn't seem to change anything.
Well, you'll need some more tricks.
Maybe "image.RelativeVertical = RelativeVertical.Paragraph" will get you further.
You'll have to set the height of the row to make room for the image.
You'll have to add non-breaking spaces at the start of your text to move the text to the right of your image.

Author:  claus.net [ Thu Jul 04, 2013 7:47 am ]
Post subject:  Re: Vertical alignment of text in a table cell with image &

Thanks for your help!

Unfortunately, I tried everything you suggested but nothing seems to change. It still looks like in the image I attached earlier.

I even tried adding the image directly to the cell and the text in a paragraph but then, regardless of what I do, the text is always above or below the image.

Author:  claus.net [ Thu Jul 04, 2013 8:15 am ]
Post subject:  Re: Vertical alignment of text in a table cell with image &

What I also don't understand: I tried using a TextFrame instead of an image in order to see if I could maybe wrestle the frame and the text to the correct positions by using absolute positioning. After all, they are both shapes (the Image and TextFrame I mean), right?

So when I set "WrapStyle.Through" for the frame, the text paragraph is still influenced by settings like "DistanceTop" or "DistanceBottom" of the frame. I thought setting WrapStyle to "Through" would let you position the shape freely without altering the positions of other elements?

Author:  Thomas Hoevel [ Thu Jul 04, 2013 9:25 am ]
Post subject:  Re: Vertical alignment of text in a table cell with image &

claus.net wrote:
After all, they are both shapes (the Image and TextFrame I mean), right?
Right, so there is no need to put the image into a textframe to position it.

Maybe I can give it a try later today.

Could you send me a DDL file of your table? Might save me some time. Should be small, just ZIP it and attach it here. Please include the image.
http://www.pdfsharp.net/wiki/MigraDocDDL.ashx

Author:  Thomas Hoevel [ Thu Jul 04, 2013 4:59 pm ]
Post subject:  Re: Vertical alignment of text in a table cell with image &

It seems that floating images inside table cells do not work as intended.

As a workaround, I used a separate column for the image.

My test code:
Code:
var table = section.AddTable();
table.AddColumn("7cm");
table.AddColumn("12mm");
table.AddColumn("7cm");

var row = table.AddRow();
var paragraph = row.Cells[0].AddParagraph("First Column");
row.Cells[0].VerticalAlignment = VerticalAlignment.Center;

var image = row.Cells[1].AddImage(@"C:\Users\thho\Pictures\ProgMan.png");
image.Height = "10mm";
paragraph = row.Cells[2].AddParagraph();
paragraph.AddText(" Second Column");
row.Cells[2].VerticalAlignment = VerticalAlignment.Center;
row.Cells[1].Borders.Right.Visible = false;
row.Cells[2].Borders.Left.Visible = false;
table.Borders.Width = 1;


And the results:
Attachment:
DummyColumn.png
DummyColumn.png [ 5.68 KiB | Viewed 28494 times ]


You can set the borders to make the column invisible.
For rows that don't have an image, you can set MergeRight to 1 and put the text into the image column. Same for header row.

I hope this helps.

Author:  claus.net [ Fri Jul 05, 2013 6:49 am ]
Post subject:  Re: Vertical alignment of text in a table cell with image &

Yeah, that's what I ended up doing as a workaround, too.

It would be nice if the slightly simpler way would work as intended but at least we have the ability to work around this issue.

Thanks for your help!

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