简体   繁体   English

如何在iTextsharp中将文本(将Fontsize更改为适合大小)调整为Rectangle

[英]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. 我正在尝试将文本插入到具有固定宽度和高度的绝对位置的现有PDF中。 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). 首先,我模拟使用Go(True)将文本插入ColumnText中。 After that i check whether the text with the given font fits into the ColumnText or not. 之后,我检查具有给定字体的文本是否适合ColumnText。 I decrease the fontsize until it fits. 我减小字体大小,直到适合为止。

It seems like calling the Method Go(false) again is not enough. 似乎再次调用方法Go(false)是不够的。 I created a new ColumnText Object and called SetSimpleColumn on that again. 我创建了一个新的ColumnText对象,并在其上再次调用SetSimpleColumn。 Calling the Go Method on this one worked. 在此方法上调用Go方法有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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