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

Vertically align some text on a page in MigraDoc
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3148
Page 1 of 1

Author:  EnvelopeExcursion [ Sun Jul 19, 2015 7:06 pm ]
Post subject:  Vertically align some text on a page in MigraDoc

I have what feels like a simple request, however I've spent two days with this and still not succeeded.

My goal is simply this: I have a front cover, upon which there will be text, horizontally AND vertically centred.

The text length will vary, which is why adding x number of blank lines preceding it is not an acceptable solution.


So far, what I have discovered is that it appears only table cells have the ability to vertically-align their content.

Worse, there doesn't seem to be a way to increase a table height to that of an entire page. There IS a MigraDoc.DocumentObjectModel.Tables.Row.Height property, but setting this property does not appear to do anything at all.

What am I missing? How can I do this in MigraDoc?

Thanks in advance.

Author:  TH-Soft [ Sun Jul 19, 2015 9:05 pm ]
Post subject:  Re: Vertically align some text on a page in MigraDoc

Hi!
EnvelopeExcursion wrote:
There IS a MigraDoc.DocumentObjectModel.Tables.Row.Height property, but setting this property does not appear to do anything at all.
Which HeightRule did you set?

Author:  EnvelopeExcursion [ Sun Jul 19, 2015 10:25 pm ]
Post subject:  Re: Vertically align some text on a page in MigraDoc

TH-Soft wrote:
Hi!
EnvelopeExcursion wrote:
There IS a MigraDoc.DocumentObjectModel.Tables.Row.Height property, but setting this property does not appear to do anything at all.
Which HeightRule did you set?


Changing this does have an impact, but not the desired one.

Instead of increasing the row height, it appears to simply move further down the page?
Attachment:
RowHeight.png
RowHeight.png [ 28.07 KiB | Viewed 11176 times ]


I should clarify that this is a table cell with a paragraph in it.

To see the visible changes to the row, I'm changing the row's background colour to orange:

Code:
row.Format.Shading.Color = MigraDoc.DocumentObjectModel.Colors.Orange;


The code for what I have so far for this front-page is as follows:

Code:
MigraDoc.DocumentObjectModel.Document document = new MigraDoc.DocumentObjectModel.Document();
MigraDoc.DocumentObjectModel.Section section = document.AddSection();
MigraDoc.DocumentObjectModel.Shapes.TextFrame titleFrame = section.AddTextFrame();
titleFrame.RelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal.Margin;
titleFrame.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Page;
titleFrame.FillFormat.Color = MigraDoc.DocumentObjectModel.Colors.Blue;
titleFrame.Top = MigraDoc.DocumentObjectModel.Unit.FromPoint(0.5);
titleFrame.Left = MigraDoc.DocumentObjectModel.Unit.FromCentimeter(-0.5) + MigraDoc.DocumentObjectModel.Unit.FromPoint(0.5);
titleFrame.Width = document.DefaultPageSetup.PageWidth - (document.DefaultPageSetup.LeftMargin + document.DefaultPageSetup.RightMargin) + MigraDoc.DocumentObjectModel.Unit.FromCentimeter(1) - MigraDoc.DocumentObjectModel.Unit.FromPoint(0.5);
titleFrame.Height = document.DefaultPageSetup.PageHeight - MigraDoc.DocumentObjectModel.Unit.FromPoint(0.5);

MigraDoc.DocumentObjectModel.Tables.Table table = titleFrame.AddTable();

table.Format.Shading.Color = MigraDoc.DocumentObjectModel.Colors.Red;
table.Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Center;
MigraDoc.DocumentObjectModel.Tables.Column column = table.AddColumn();
column.Format.Shading.Color = MigraDoc.DocumentObjectModel.Colors.Green;
column.Width = document.DefaultPageSetup.PageWidth - (document.DefaultPageSetup.LeftMargin + document.DefaultPageSetup.RightMargin) + MigraDoc.DocumentObjectModel.Unit.FromCentimeter(1) + MigraDoc.DocumentObjectModel.Unit.FromPoint(6.3);
MigraDoc.DocumentObjectModel.Tables.Row row = table.AddRow();

row.HeightRule = MigraDoc.DocumentObjectModel.Tables.RowHeightRule.Exactly;
row.Height = "20cm";

row.Format.Shading.Color = MigraDoc.DocumentObjectModel.Colors.Orange;

row.VerticalAlignment = MigraDoc.DocumentObjectModel.Tables.VerticalAlignment.Center;

MigraDoc.DocumentObjectModel.Paragraph coverParagraph = row[0].AddParagraph();
coverParagraph.AddFormattedText("TEST_TITLE");


What's the desired scenario at this point with the above? That row should be orange, from the top of the page to whatever length I specify. And that length won't be 20cm, it'll be the page height, if/when it actually works.

And THEN, that text will be centred both vertically, and horizontally. If there's a better way of doing this, I'd be very interested in seeing it.

Author:  TH-Soft [ Mon Jul 20, 2015 7:08 am ]
Post subject:  Re: Vertically align some text on a page in MigraDoc

EnvelopeExcursion wrote:
What's the desired scenario at this point with the above? That row should be orange, from the top of the page to whatever length I specify.
You set orange as the background colour for text in that row, but not as the background colour for the row.
Set "Row.Shading" instead of "Row.Format.Shading" to change the colour of the row. Maybe try yellow to see the difference.

Author:  EnvelopeExcursion [ Mon Jul 20, 2015 6:55 pm ]
Post subject:  Re: Vertically align some text on a page in MigraDoc

TH-Soft wrote:
EnvelopeExcursion wrote:
What's the desired scenario at this point with the above? That row should be orange, from the top of the page to whatever length I specify.
You set orange as the background colour for text in that row, but not as the background colour for the row.
Set "Row.Shading" instead of "Row.Format.Shading" to change the colour of the row. Maybe try yellow to see the difference.


Now THIS was useful to know! Row.Format.Shading and Row.Shading. I'm a little unsure why we can set Row.Format.Shading for formatting added text, when the formatting can just be done on the text itself that we're adding, via a paragraph, etc. However, that doesn't particular matter right now. More importantly, thanks! After observing the result of setting Row.Shading to orange instead of Row.Format.Shading, I could see that the height of the row was in fact changing. This meant that it turned out my code WAS pretty much working! :D

However, for the benefit of anyone seeking to centre text on a page both vertically and horizontally, they can do the following (this has none of the unrelated fluff I had in the code in my previous post):
Code:

MigraDoc.DocumentObjectModel.Document document = new MigraDoc.DocumentObjectModel.Document();
MigraDoc.DocumentObjectModel.Section section = document.AddSection();

MigraDoc.DocumentObjectModel.Shapes.TextFrame titleFrame = section.AddTextFrame();
titleFrame.RelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal.Margin;
titleFrame.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Margin;
//titleFrame.FillFormat.Color = MigraDoc.DocumentObjectModel.Colors.Blue;
titleFrame.Width = document.DefaultPageSetup.PageWidth - (document.DefaultPageSetup.LeftMargin + document.DefaultPageSetup.RightMargin);
titleFrame.Height = document.DefaultPageSetup.PageHeight - (document.DefaultPageSetup.TopMargin + document.DefaultPageSetup.BottomMargin);

MigraDoc.DocumentObjectModel.Tables.Table table = titleFrame.AddTable();
table.Rows.LeftIndent = MigraDoc.DocumentObjectModel.Unit.Zero; //If this is not specified, for some reason the table appears to be negatively indented to the left, meaning it sits slightly outside of the text frame.
table.Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Center;

MigraDoc.DocumentObjectModel.Tables.Column column = table.AddColumn();
//column.Format.Shading.Color = MigraDoc.DocumentObjectModel.Colors.Green;
column.Width = document.DefaultPageSetup.PageWidth - (document.DefaultPageSetup.LeftMargin + document.DefaultPageSetup.RightMargin);

MigraDoc.DocumentObjectModel.Tables.Row row = table.AddRow();
row.HeightRule = MigraDoc.DocumentObjectModel.Tables.RowHeightRule.Exactly;
row.Height = document.DefaultPageSetup.PageHeight - (document.DefaultPageSetup.TopMargin + document.DefaultPageSetup.BottomMargin);
//row.Shading.Color = MigraDoc.DocumentObjectModel.Colors.Orange;
row.VerticalAlignment = MigraDoc.DocumentObjectModel.Tables.VerticalAlignment.Center;

MigraDoc.DocumentObjectModel.Paragraph coverParagraph = row[0].AddParagraph();
coverParagraph.AddFormattedText("TEST_TITLE");
//coverParagraph.AddFormattedText("TEST_TITLETEST TITLE TEST TITLE TEST TEST TEST TEST TEST TEST TEST TESTTESTTESTTESTTESTTEST_TITLETEST TITLE TEST TITLE TEST TEST TEST TEST TEST TEST TEST TESTTESTTESTTESTTESTTEST_TITLETEST TITLE TEST TITLE TEST TEST TEST TEST TEST TEST TEST TESTTESTTESTTESTTESTTEST_TITLETEST TITLE TEST TITLE TEST TEST TEST TEST TEST TEST TEST TESTTESTTESTTESTTESTTEST_TITLETEST TITLE TEST TITLE TEST TEST TEST TEST TEST TEST TEST TESTTESTTESTTESTTEST");


I've left the commented out colour-setting lines so that if anyone using this code wants to check out the different areas, they'll be able to see, visually, that everything seems to be working.

For historical purposes, if you did uncomment those colour lines, you shouldn't see any blue. You should only see an orange box, with a green box in the middle around your text. There's a slight gap between the left and right edges of the green box and the edges of the orange box.

I've attached an image to show this:
Attachment:
HorizontallyAndVerticallyAlignedText.png
HorizontallyAndVerticallyAlignedText.png [ 27.47 KiB | Viewed 11164 times ]



The final line with the longer string being passed to AddFormattedText() is left in to demonstrate that the centre area expands correctly.

I hope this helps anyone else trying to do something like this.

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