【问题标题】:Itext7 center text and fill both sidesItext7 居中文本并填充两边
【发布时间】:2016-09-11 15:43:53
【问题描述】:

我正在尝试将文本打印为 pdf,并且到目前为止我已经完成了所有设置和工作。我有一条 6 英寸宽的线,即 (6*72) 432 设备像素。我想要做的是将文本打印到这一行,居中并填充两侧以占据该行的整个宽度。示例 ------您好,这是我的文本------。这是我的代码的 sn-p,我试图估计我的文本占用的空间,以便我可以计算在前后插入多少字符。

PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
canvas.setFontAndSize(font, 12);
System.out.println(font.getContentWidth(new PdfString("Hello there, this is my Text")));

此代码产生的数字超过两千,远远超过 432。不确定返回的单位是什么。 如何估计字符串的长度并将其居中对齐在 432dp 内 ? 多余的空格必须用特殊字符填充。它与打印支票的方式非常相似,其中金额用文字说明,如果有空格,则在两面填写。

我查看了this postthis other post,但我对此一无所知。请指教。

【问题讨论】:

    标签: itext7


    【解决方案1】:

    请查看您引用的帖子的标签:How can I find the maximum character limit for a text field? 最后一个标签是指 iText 5,而您使用的是 iText 7。换句话说:您正在查看错误的常见问题解答条目。该问题的 iText 7 常见问题解答条目是:How can I find the maximum character limit for a text field? 但这并不能回答您的问题。

    你应该阅读How to choose the optimal size for a font?

    测量文本时获得的值以字形空间表示:

    PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
    float glyphWidth = font.getContentWidth(new PdfString("Hello there, this is my Text"));
    

    这些行不足以获得用户空间的宽度是正常的(这是您正在寻找的)。为什么这是正常的?因为font 对象不知道您要使用的字体大小。

    在您的情况下,您使用的是 12pt 字体。因此PdfString 的宽度为:

    float width = glyphWidth * 0.001f * 12f;
    

    这就是你要找的宽度。

    可以使用showTextAligned() 方法将该文本置于绝对位置的中心。有许多替代方法可以实现这一目标。这可能是另一个问题的主题,因为你不需要做所有的数学。

    你可以这样做:

    /*
     * Example written in answer to a question on StackOverflow.
     * http://stackoverflow.com/questions/39437838
     */
    package com.itextpdf.sandbox.text;
    
    import com.itextpdf.kernel.color.Color;
    import com.itextpdf.kernel.geom.PageSize;
    import com.itextpdf.kernel.geom.Rectangle;
    import com.itextpdf.kernel.pdf.PdfDocument;
    import com.itextpdf.kernel.pdf.PdfWriter;
    import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
    import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
    import com.itextpdf.layout.Document;
    import com.itextpdf.layout.element.Paragraph;
    import com.itextpdf.layout.element.Tab;
    import com.itextpdf.layout.element.TabStop;
    import com.itextpdf.layout.property.TabAlignment;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author bruno
     */
    public class CenterText {
        class MyLine implements ILineDrawer {
            private float lineWidth = 1;
            private float offset = 5;
            private Color color = Color.BLACK;
            @Override
            public void draw(PdfCanvas canvas, Rectangle drawArea) {
                canvas.saveState()
                    .setStrokeColor(color)
                    .setLineWidth(lineWidth)
                    .moveTo(drawArea.getX(), drawArea.getY() + lineWidth / 2 + offset)
                    .lineTo(drawArea.getX() + drawArea.getWidth(), drawArea.getY() + lineWidth / 2 + offset)
                    .stroke()
                    .restoreState();
            }
    
            @Override
            public float getLineWidth() {
                return lineWidth;
            }
            @Override
            public void setLineWidth(float lineWidth) {
                this.lineWidth = lineWidth;
            }
            @Override
            public Color getColor() {
                return color;
            }
            @Override
            public void setColor(Color color) {
                this.color = color;
            }
            public float getOffset() {
                return offset;
            }
            public void setOffset(float poffset) {
                this.offset = offset;
            }
    
        }
    
        public static final String DEST = "results/text/center_text.pdf";
    
        public static void main(String[] args) throws IOException {
            File file = new File(DEST);
            file.getParentFile().mkdirs();
            new CenterText().createPdf(DEST);
        }
        public void createPdf(String dest) throws IOException {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            PageSize pagesize = PageSize.A4;
            Document document = new Document(pdf, pagesize);
            float w = pagesize.getWidth() - document.getLeftMargin() - document.getRightMargin();
            MyLine line = new MyLine();
            List<TabStop> tabstops = new ArrayList();
            tabstops.add(new TabStop(w / 2, TabAlignment.CENTER, line));
            tabstops.add(new TabStop(w, TabAlignment.LEFT, line));
            Paragraph p = new Paragraph();
            p.addTabStops(tabstops);
            p.add(new Tab()).add("Text in the middle").add(new Tab());
            document.add(p);
            document.close();
        }
    }
    

    【讨论】:

    • 如果 iText 可以为你做数学,你为什么要自己做?
    猜你喜欢
    • 2022-01-01
    • 2011-07-18
    • 2020-05-22
    • 1970-01-01
    • 2021-09-13
    • 2010-09-12
    • 2018-08-04
    • 2016-07-25
    • 1970-01-01
    相关资源
    最近更新 更多