Width of string in TextArea constructor

Width of string in TextArea constructor

Forum member · Oct 25, 2012
I have a scenario where I need to know the width of a string to add to a textarea constructor. All I can find in the help was the TextLine GetWidth() but could not see how to apply that here. string myText = "Hello World" float txtWidth = ???How do I get this for the next statement??? <=== template.Elements.Add(new TextArea(myText, PageWidth - txtWidth, y, txtWidth, Height, Font, FontSize, Align_Left, Black)); Basically what I want to accomplish for example is if I have an address Contact Name Address Line 1 Address Line 2 Address Line 3 I want to determine which line is the longest and then place each line of the address on the page starting at the pagewidth minus longest line width so if my page was 500 wide and the longest line was 80 then all lines of the address would begin at 420.

Re: Width of string in TextArea constructor

Support · Oct 26, 2012
Hello, It is suggested to use the Font class to calculate the text width and use this calculated text width accordingly to meet your business needs. You will need to use the desired font and font size to calculate the text width and use this value to set it to the TextArea. Please make sure that you are setting the exact font and font size to the TextArea page element which you used to calculate the text width. Below is the sample code for it. string str="This is example to find the textwidth and set this widht to TextArea"; Font font = Font.Helvetica; //Get the text widht using Font class method. float textWidth = font.GetTextWidth(str, 10); Document document = new Document(); Page page = new Page(); document.Pages.Add(page); Rectangle rectangle = new Rectangle(0, 100, textWidth, 40); page.Elements.Add(rectangle); //Create TextArea and set the exact font and fontsize to the text area which is used to calculate the text width. TextArea area = new TextArea(str, 0, 100, textWidth, 40, font, 10); page.Elements.Add(area); document.Draw(@"C:\MyDocument.pdf"); Thanks, ceTe Software Support Team.

Re: Width of string in TextArea constructor

Forum member · Oct 26, 2012
Awesome. That was exactly what I was looking for. Thanks!!! float[] widths = { DPDF_Font_Helvetica.GetTextWidth(FullName, FontSize - 1), DPDF_Font_Helvetica.GetTextWidth(Address1, FontSize - 1), DPDF_Font_Helvetica.GetTextWidth(Address2, FontSize - 1), DPDF_Font_Helvetica.GetTextWidth(Phone, FontSize - 1), DPDF_Font_Helvetica.GetTextWidth(Fax, FontSize - 1) }; float rtHdrWidth = widths.Max() + 2;