gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:40:48 31 4
gpt4 key购买 nike

我试图找到是否有自动缩放,但到目前为止只发现了表单域的自动缩放。由于 pdf 将用于绘制文本,因此表单字段没有用。

下面的代码是放置一个固定尺寸的“盒子”的片段,其中所有文本都应按比例显示(在一行上)

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

最佳答案

iText 7 的布局模块允许您模拟元素的渲染(通过从元素创建渲染器树然后使用 Layout 方法)并检查它是否适合给定区域(通过检查 LayoutResult 对象)。因此,您可以做的是检查文本是否适合具有给定字体大小的固定矩形。然后你可以只对字体大小进行二进制搜索。

这是一个示例代码:

PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));

Text lineTxt = new Text("A VERY LONG TEXT SHOULD BE SCALED");

iText.Kernel.Geom.Rectangle lineTxtRect =
new iText.Kernel.Geom.Rectangle(100,200,100,100);

Div lineDiv = new Div();
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);
lineDiv.Add(linePara);

// 1 is the font size that is definitely small enough to draw all the text
float fontSizeL = 1;

// 20 is the maximum value of the font size you want to use
float fontSizeR = 20;

Canvas canvas =
new Canvas(
new PdfCanvas(pdfDocument.AddNewPage()),
pdfDocument,
lineTxtRect);

// Binary search on the font size
while (Math.Abs(fontSizeL - fontSizeR) > 1e-1) {

float curFontSize = (fontSizeL + fontSizeR) / 2;
lineDiv.SetFontSize(curFontSize);

// It is important to set parent for the current element renderer
// to a root renderer.
IRenderer renderer =
lineDiv.CreateRendererSubTree()
.SetParent(canvas.GetRenderer());

LayoutContext context =
new LayoutContext(
new LayoutArea(1, lineTxtRect));

if (renderer.Layout(context).GetStatus() == LayoutResult.FULL) {
// we can fit all the text with curFontSize
fontSizeL = curFontSize;
} else {
fontSizeR = curFontSize;
}
}

// Use the biggest font size that is still small enough to fit all the
// text.
lineDiv.SetFontSize(fontSizeL);
canvas.Add(lineDiv);

pdfDocument.Close();

关于c# - 如何使用 itext7 在固定矩形内缩放文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57890666/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com