itext7的文本缩放问题,隐藏的边距? [英] Text scaling problem with itext7, hidden margins?

查看:568
本文介绍了itext7的文本缩放问题,隐藏的边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用itext7(7.1.7)从asp.netcore剃刀项目中获取此c#函数,以输出一个div,该div包含可扩展到高度和宽度所给定约束范围内的文本.但是,由于某种原因,似乎存在某种隐藏的边距,即使该边距设置为0,也无法将文本缩放到给定宽度和高度的边界. 目前,我得到的最大文字量仅为其应有的一半. 另外,该段落或div实际上都没有设置为最小宽度和最小高度.

I'm trying to get this c# function from within a asp.netcore razor project using itext7 (7.1.7) to output a div containing text that scales up to within the constraints given with height and width. However, for some reason it seems there's some kind of hidden margin that will not scale the text to the boundaries of the given width and height, even though the margins are set to 0. At the moment the maximum text i get is only half of what it should be. Also, the paragraph, nor the div is actually set to the min width and min height.

感谢您提供任何解决方法的帮助

Any help how to fix this is appreciated

public Div MakeScaledTxtDiv(string _displayTxt ,PdfFont _Pdffont, float _maxHeight, 
                                            float _maxWidth, Canvas _canvas, bool _showBorder)
    {
        Div nameDiv = new Div();

        iText.Kernel.Geom.Rectangle posRect = new iText.Kernel.Geom.Rectangle(_maxWidth,_maxHeight);

        Paragraph txtPara = new Paragraph()
            .SetVerticalAlignment(VerticalAlignment.MIDDLE)
            .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);

        if (_showBorder)
            txtPara.SetBorder(new DashedBorder(1));

        if (_maxHeight > 0)
            txtPara.SetMaxHeight(_maxHeight);

        if (_maxWidth > 0)
            txtPara.SetMaxWidth(_maxWidth);

        txtPara
            .SetMargin(0f)
            .SetPadding(0f)
            .SetFont(_Pdffont)
            .Add(_displayTxt);

        float fontSizeToSet = 1;
        float fontSizeMax = 42;
        float curFontSize = fontSizeMax;

        bool m_bGoodfit = false;
        while (!m_bGoodfit)
        {
            txtPara.SetFontSize(curFontSize);

            ParagraphRenderer renderer = (ParagraphRenderer)txtPara.CreateRendererSubTree().SetParent(_canvas.GetRenderer());

            LayoutContext context = new LayoutContext(new LayoutArea(1, posRect));
            var fits = renderer.Layout(context).GetStatus();
            if (fits == LayoutResult.FULL)
            {
                fontSizeToSet = curFontSize;
                m_bGoodfit = true;
            }
            else
            {
                curFontSize -= 1;
                if (curFontSize == 1)
                {
                    fontSizeToSet = curFontSize;
                    m_bGoodfit = true;
                }
            }
        }

        txtPara.SetFontSize(fontSizeToSet);

        if (_maxHeight > 0)
        {
            nameDiv.SetMinHeight(_maxHeight)
                    .SetMaxHeight(_maxHeight)
                    .SetHeight(_maxHeight);
        }

        if (_maxWidth > 0)
        {
            nameDiv.SetMinWidth(_maxWidth)
                    .SetMaxWidth(_maxWidth)
                    .SetWidth(_maxWidth);
        }

        nameDiv
            .SetMargin(0f)
            .SetPadding(0f)
            .SetVerticalAlignment(VerticalAlignment.MIDDLE)
            .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);

        if (_showBorder)
            nameDiv.SetBorder(new DottedBorder(1));

        nameDiv.Add(txtPara).SetVerticalAlignment(VerticalAlignment.MIDDLE);

        return nameDiv;
    }

推荐答案

对我有用的是在下面的代码中更改乘数0.7f稍大一些(从

What worked for me was to change the multiplier number 0.7f slightly bigger in the code below (code referenced from https://stackoverflow.com/a/57929067/5490413).

似乎乘数变大/变小,文本和字段底边框之间的距离也相应增加/缩小.也许从1.2f开始,然后找到一个合适的.

It seems as that multiplier gets bigger/smaller, the space between the text and field bottom border grows/shrinks accordingly. Maybe start with 1.2f and find one that fits.

Paragraph linePara = new Paragraph().Add(lineTxt)
    .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).SetBorder(new DottedBorder(1))
    .SetMultipliedLeading(0.7f);
lineDiv.Add(linePara);


仅供参考,我遇到了您所遇到的确切问题:如何使用itext7在固定矩形内缩放文本?

我尝试了在那里发布的答案 https://stackoverflow.com/a/57929067/5490413 ,相同的隐藏底边利润问题.

I tried with the answer posted there https://stackoverflow.com/a/57929067/5490413 and faced the same hidden bottom margin issue here.

这篇关于itext7的文本缩放问题,隐藏的边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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