如何使用itext7在固定矩形内缩放文本? [英] How to scale text within a fixed rectangle with itext7?

查看:598
本文介绍了如何使用itext7在固定矩形内缩放文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a pdf document with itext7 in c# which should have fixed rectangles containing varying text that should scale within the boundaries of the (invisible) rectangles.

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

I have tried to find if there's automatic scaling, but so far only found auto-scaling for formfields. Since the pdf will be used for plotting text, formfields are of no use.

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

Code below is a snippet placing a 'box' with fixed dimensions, where all the text should be shown scaled (on one line)

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对象).因此,您可以做的是检查文本是否适合给定字体大小的固定矩形.然后,您可以对字体大小进行二进制搜索.

Layout module of iText 7 allows you to simulate rendering of an element (by creating the renderer tree from the element and then using Layout method) and check whether it fits the given area (by checking LayoutResult object). Thus what you can do is check whether the text fits into your fixed rectangle with the given font size. Then you can just do a binary search on the font size.

这是示例代码:

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

float fontSizeL = 1; // 1 is the font size that is definitely small enough to draw all the text 
float fontSizeR = 20; // 20 is the maximum value of the font size you want to use

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

这篇关于如何使用itext7在固定矩形内缩放文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆