Dynamic text width for label

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Generator for .NET (v5.x)  /  Display Thread

DynamicPDF Generator for .NET (v5.x) Forum

 May 28 2014 7:47 PM
Hi.

I try to create a PDF with labels on it. The labels show the name of a person and as a name of a person can be very short or very long I need a dynamic sized text for that.

Is there a feature within this library that I can use?

I have a label with a certain width and want the text to always fit in there...

Thanks, Nik
 May 28 2014 8:01 PM
Sorry have to correct the version of the Generator.

It is:
ceTe.DynamicPDF.40.dll

Thanks
 May 29 2014 9:48 AM
Posted by a ceTe Software moderator
Hello,

Yes, you can calculate the text width and set this value to Label without any problem using our DynamicPDF Generator for .NET API. You will need to use the Font class members to do this. You can get the text width using GetTextWidth method  of Font class which takes text, font size as arguments. Please refer to the documentation on Font class members here. Also please make sure of setting same font and font size for the Label page element. Below is the sample code for it.

            Document docuemnt = new Document();
            Page page = new Page();
            docuemnt.Pages.Add(page);
            string text = "This is for calculating text width";
            Font font = Font.Helvetica;
            float textwidth = font.GetTextWidth(text, 12);
            Label label = new Label(text, 100, 100, textwidth, 30, font,12);
            page.Elements.Add(label);
            Rectangle rect = new Rectangle(100, 100, textwidth, 30);
            page.Elements.Add(rect);
            docuemnt.Draw(@"C:\Temp\MyDocument.pdf");

Thanks,
ceTe Software Support Team.
 May 29 2014 1:14 PM
Posted by a ceTe Software moderator
Hello,

The above code and description which we posted is for getting the text width and setting that width to the label. It looks like you would like to fit the text into label keeping the label width fixed. You can achieve this by reducing or increasing the font size so that it gets fit into the label. Below is the sample code for it.

            Document docuemnt = new Document();
            Page page = new Page();
            docuemnt.Pages.Add(page);
            string text = "This is to test text width";
            Font font = Font.Helvetica;
            Label label = new Label(text, 100, 100, 200, 30);
            //code to calulate the font size to fit into the desired width.
            label.Font = font;
            float fontSize = label.FontSize;
            float textwidth = font.GetTextWidth(text, fontSize);
            if (textwidth > label.Width)
            {
                while (textwidth > label.Width-1)
                {
                   textwidth = font.GetTextWidth(text, fontSize);
                   fontSize = fontSize - 0.001F;
                }
            }
            else
            {
                while (textwidth < label.Width-1)
                {
                    textwidth = font.GetTextWidth(text, fontSize);
                    fontSize = fontSize + 0.001F;
                }
            }
            label.FontSize = fontSize;
            page.Elements.Add(label);
            docuemnt.Draw(@"C:\Temp\MyDocument.pdf");

Thanks,
ceTe Software Support Team.
 May 29 2014 6:50 PM
Perfect! That is exactly what I need!! Thank you very much!! Great support!!

Kind regards, Nik

All times are US Eastern Standard time. The time now is 8:47 AM.