title: How to fit text (change Fontsize until it fits) into Rectangle in iTextsharp
body: <p>I am trying to insert text into an existing PDF at absolute position with fixed width and height. When the text is too large or its fontsize the text is not visible. I tried to solve this issue as described in <a href="https://itextpdf.com/de/resources/faq/technical-support/itext-5/how-fit-string-inside-rectangle" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Yet nothing gets inserted. Does anybody know what i am doing wrong or am missing?</p>
  
  <p>Here is what i am currently doing: </p>
  
  <pre><code> TextBlock tb = (TextBlock)g.Children[0];
   ColumnText ct = new ColumnText(stamper.GetOverContent(fd.Page));
  
   float llx=0, lly=0, urx=0, ury = 0;
   float percentX = (float) ((Canvas.GetLeft(g) + tb.Padding.Left)/c.ActualWidth);
   float percentY = (float)((Canvas.GetTop(g) - tb.Padding.Top)/c.ActualHeight);
   float percentWidth = (float)(g.ActualWidth/c.ActualWidth);
   float percentHeight = (float)(g.ActualHeight/c.ActualHeight);
  
   llx = percentX * reader.GetPageSize(fd.Page).Width;
   lly =(float) (reader.GetPageSize(fd.Page).Height - percentY * reader.GetPageSize(fd.Page).Height);
   urx = llx + percentWidth * reader.GetPageSize(fd.Page).Width;
   ury = lly - percentHeight * reader.GetPageSize(fd.Page).Height;
  
   ct.SetSimpleColumn(new iTextSharp.text.Rectangle(llx, lly, urx, ury));
   float fontsize = (float) tb.FontSize - 2;
   Boolean fits;
   iTextSharp.text.Paragraph p;
   do {
       p = new iTextSharp.text.Paragraph();
       fontsize -= 0.1f;
       p.Font.Size = fontsize;
       p.Add(tb.Text);
       ct.AddElement(p);
       int status = ct.Go(true);
       fits = !ColumnText.HasMoreText(status);
       status = ct.Go(true);
   } while (!fits &amp;&amp; p.Font.Size &gt; 2);
   ct.Go();
  </code></pre>