如何使用itext7在固定矩形内缩放文本?

时间:2019-09-11 13:49:18

标签: c# pdf asp.net-core itext7

我正在尝试使用c#中的itext7制作pdf文档,该文档应具有固定的矩形,其中包含的各种文本应在(不可见的)矩形边界内缩放。

我试图找到是否存在自动缩放功能,但到目前为止,仅发现了表单域的自动缩放功能。由于pdf将用于绘制文本,因此没有使用formfields。

下面的代码是一个片段,其中放置了具有固定尺寸的“框”,其中所有文本均应按比例显示(一行)

float fontSize = 22f;

Text lineTxt = new Text("A VERY LONG TEXT SHOULD BE SCALED").SetFont(lineFont).SetFontSize(fontSize);

iText.Kernel.Geom.Rectangle lineTxtRect = new iText.Kernel.Geom.Rectangle(100, posHeight - 200, (float)plotline.producttype_plotmaxwidthpts, (float)plotline.producttype_plotmaxheightpts);

Div lineDiv = new Div();
lineDiv.SetMaxHeight((float)plotline.producttype_plotmaxheightpts);
lineDiv.SetWidth((float)plotline.producttype_plotmaxwidthpts);
lineDiv.SetHeight((float)plotline.producttype_plotmaxheightpts);
lineDiv.SetVerticalAlignment(VerticalAlignment.MIDDLE);
lineDiv.SetBorder(new DashedBorder(1));

Paragraph linePara = new Paragraph().Add(lineTxt).
        SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).
        SetBorder(new DottedBorder(1)).
        SetMultipliedLeading(0.7f).
        SetMaxHeight((float)plotline.producttype_plotmaxheightpts).
        SetHeight((float)plotline.producttype_plotmaxheightpts);

lineDiv.Add(linePara);

new Canvas(PageCanvas, pdf, lineTxtRect).Add(lineDiv).SetBorder(new SolidBorder(1f));

1 个答案:

答案 0 :(得分:0)

iText 7的

Layout模块允许您模拟元素的渲染(通过从元素创建渲染器树,然后使用use pdx_sap_user go select po_number, po_issue_date from vw_po_header where po_issue_date > getDate() And PO_issue_date < DateAdd("d",-1,DateAdd("m",8,DateAdd("d",-(Day(getDate())-1),getDate()))) 方法)并检查其是否适合给定区域(通过检查{{1} }对象)。因此,您可以做的是检查文本是否适合给定字体大小的固定矩形。然后,您可以对字体大小进行二进制搜索。

这是示例代码:

Layout