PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jul 19, 2024 11:30 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sun Jul 19, 2015 7:06 pm 
Offline

Joined: Sun May 24, 2015 2:49 pm
Posts: 7
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.


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 19, 2015 9:05 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 951
Location: CCAA
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?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 19, 2015 10:25 pm 
Offline

Joined: Sun May 24, 2015 2:49 pm
Posts: 7
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 11178 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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 20, 2015 7:08 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 951
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 20, 2015 6:55 pm 
Offline

Joined: Sun May 24, 2015 2:49 pm
Posts: 7
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 11166 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.


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

All times are UTC


Who is online

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