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

Set TextFrame Position
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2410
Page 1 of 1

Author:  ghendric [ Tue Apr 16, 2013 5:43 pm ]
Post subject:  Set TextFrame Position

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");

Author:  Thomas Hoevel [ Wed Apr 17, 2013 9:01 am ]
Post subject:  Re: Set TextFrame Position

Hi!

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

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

Author:  ghendric [ Fri Apr 19, 2013 2:35 pm ]
Post subject:  Re: Set TextFrame Position

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?

Author:  ghendric [ Fri Apr 19, 2013 6:24 pm ]
Post subject:  Re: Set TextFrame Position

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);             

Author:  ghendric [ Wed Jun 26, 2013 2:20 pm ]
Post subject:  Re: Set TextFrame Position

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

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