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

时间:2019-03-05 07:45:46

标签: c# wpf itext

我正在尝试将文本插入具有固定宽度和高度的绝对位置的现有PDF中。当文本太大或字体大小不可见时。我试图按照here中所述解决此问题。

但是什么也没插入。有人知道我做错了什么还是想念吗?

这是我目前正在做的事情:

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

0 个答案:

没有答案