简体   繁体   中英

How to fit text (change Fontsize until it fits) into Rectangle in iTextsharp

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 here .

Yet nothing gets inserted. Does anybody know what i am doing wrong or am missing?

Here is what i am currently doing:

 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 && p.Font.Size > 2);
 ct.Go();

I have found a way to get my problem to work. Just in case someone wants to know how it does work this is how i have solved this issue:

At first i simulate inserting the text into the ColumnText with Go(True). After that i check whether the text with the given font fits into the ColumnText or not. I decrease the fontsize until it fits.

It seems like calling the Method Go(false) again is not enough. I created a new ColumnText Object and called SetSimpleColumn on that again. Calling the Go Method on this one worked.

Thats the code:

 ColumnText ct = new ColumnText(contentByte);
 ct.SetSimpleColumn(rec);
 ct.AddElement(new iTextSharp.text.Paragraph(tb.Text.ToString()));
 int status = ct.Go(true);
 Boolean fits = !ColumnText.HasMoreText(status);
 if (fits)
 {
       ColumnText ctxt = new ColumnText(contentByte);
       ctxt.SetSimpleColumn(rec);
       ctxt.AddElement(new iTextSharp.text.Paragraph(tb.Text.ToString()));
       ctxt.Go();
  }else
  {
       double fontsize = tb.FontSize - 2;
       while(!fits && fontsize > 1)
       {
            fontsize -= 0.1;
            iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph(tb.Text);
            p.Font = new iTextSharp.text.Font(BaseFont.CreateFont());
            p.Font.Size = (float)fontsize;
            ColumnText ctxt = new ColumnText(contentByte);
            ctxt.SetSimpleColumn(rec);
            ctxt.AddElement(p);
            int stat = ctxt.Go(true);
            fits = !ColumnText.HasMoreText(stat);
       }
       iTextSharp.text.Paragraph par = new iTextSharp.text.Paragraph(tb.Text);
       par.Font = new iTextSharp.text.Font(BaseFont.CreateFont());
       par.Font.Size = (float)fontsize;
       ColumnText coltxt = new ColumnText(contentByte);
       coltxt.SetSimpleColumn(rec);
       coltxt.AddElement(par);
       coltxt.Go();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM