PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon May 13, 2024 11:28 pm

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
 Post subject: Set TextFrame Position
PostPosted: Tue Apr 16, 2013 5:43 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
I'm trying to set the position of a TextFramein the header but can't get it to position correctly. Seems there's a problem with using WrapFormat.Style. If I set it to WrapStyle.Through, the TextFrame positions itself directly under anything else that is above it. If I take that out, then it positions itself where I specify. The problem is, I'm trying to put another TextFrame to the left of it slightly lower but can't get that position set correctly either. Setting the TOP position does nothing when WrapStyle is set.. How can I set the TextFrame TOP position and still use WrapStyle?

Code:
TextFrame tf = sec.Headers.Primary.AddTextFrame();
tf.LineFormat.Width = 0.25;
tf.Left = new Unit(9.5, UnitType.Centimeter);
tf.Top = new Unit(3.175, UnitType.Centimeter);
tf.Width = new Unit(((sec.PageSetup.PageWidth.Centimeter - (sec.PageSetup.LeftMargin.Centimeter + sec.PageSetup.RightMargin.Centimeter)) / 2) + 0.1, UnitType.Centimeter);
tf.WrapFormat.Style = WrapStyle.Through;
paragraph = tf.AddParagraph();
paragraph.AddText("LINE 1");
paragraph.AddLineBreak();
paragraph.AddText("LINE 2");
paragraph.AddLineBreak();
paragraph.AddLineBreak();
paragraph.AddText("OTHER INFO 1");
paragraph.AddLineBreak();
paragraph.AddText("OTHER INFO 2");

TextFrame tf2 = sec.Headers.Primary.AddTextFrame();
tf2.Top = new Unit(2.5, UnitType.Centimeter);
tf2.MarginBottom = "0.1";
tf2.Height = new Unit(12);
tf2.LineFormat.Width = 0.25;
tf2.WrapFormat.Style = WrapStyle.Through;
paragraph = tf2.AddParagraph();
paragraph.Format.Font.Size = new Unit(10);
paragraph.AddText("Additional Info:");
paragraph.Format.Font.Name = "Arial";
paragraph.Format.Font.Bold = true;

TextFrame tf3 = sec.Headers.Primary.AddTextFrame();
//tf3.Top = "4.7cm";
tf3.Width = "9.0cm";
tf3.Height = "2.116666667cm";
//tf3.Top = "2.5cm";
tf3.LineFormat.Width = 0.25;
tf3.MarginTop = "0.185208333cm";
tf3.MarginLeft = "0.185208333cm";
tf3.WrapFormat.Style = WrapStyle.Through;
paragraph = tf3.AddParagraph();
paragraph.AddText("Some more info goes here");

_________________
Thanks,
George


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 17, 2013 9:01 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
Hi!

Don't set tf.Top, set tf.WrapFormat.DistanceTop instead.

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 19, 2013 2:35 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
That seems to be working! Thanks!

However; I have another question concerning TextFrames. Is there an autosize property for height available? I need to set the height of a TextFrame based on the amount of text is put into it. If I set to a static size I may get some text that will go outside of the text frame. If not, how should I calculate the height based on the text thats within it?

_________________
Thanks,
George


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 19, 2013 6:24 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
Actually, I figured out how to set a TextFrame height based on the size and length of some text.

Code:
TextFrame txf = sec.AddTextFrame();
// 8.7 is my static width
// 0.22930555555556 is the font size in centimeters (6.5 point)
TextMeasurement txtM = new TextMeasurement(new Font("Arial", new Unit(0.22930555555556, UnitType.Centimeter)));
float len = txtM.MeasureString(CustomerNote).Width;
double height = (len / (8.7 * 100)) + 0.1;
txf.Height = new Unit(height, UnitType.Centimeter);             

_________________
Thanks,
George


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 26, 2013 2:20 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
This calculation doesn't seem to work correctly if I use a font size of 24... even when converting 24 point to centimeters.

How do you use MeasureString(string).Height to set the height of a TextFrame based on the font height of the text that will go into the TextFrame?

Thanks

_________________
Thanks,
George


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 84 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group