在ColumnText中将短语放在iText中 [英] iText placement of Phrase within ColumnText

查看:882
本文介绍了在ColumnText中将短语放在iText中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在处理一个情况,我将一个短语添加到ColumnText对象。

So I'm dealing with a situation where I have a Phrase added to a ColumnText object.

黑色标题是iText将词组文本放在ColumnText中的位置。粉红色的标题是所需的位置。

The title in black is where iText is placing the text of the Phrase within the ColumnText. The title in pink is the desired placement.

 private void addText(PdfContentByte contentByte, PdfReader threePagesReader, Project project, CoverTemplate coverTemplate)
        throws DocumentException {

    ColumnText columnText = new ColumnText(contentByte);

    // This only affects the space between Phrases, not the space between the top of the
    //ColumnText and the first Phrase
    int extraParagraphSpace = 12;
    columnText.setExtraParagraphSpace(extraParagraphSpace);

    Phrase mainTitle = new Phrase(project.getTitle(),buildCoverFont(coverTemplate.getFontSizeLine1()));
    columnText.addText(mainTitle);
       ... <other code that is not pertinent to this question>

    //these values are calculated from values that will place this columnText onto the PDF 
    //template
    columnText.setSimpleColumn(llx, lly, urx, ury);
    columnText.go();

private Font buildCoverFont(float fontSize) {
    Font font = FontFactory.getFont(FONT_ARIAL, BaseFont.WINANSI, BaseFont.EMBEDDED, fontSize, Font.NORMAL, CMYK_BLACK);
    BaseFont baseFont = font.getBaseFont();

    if (baseFont != null) {
        baseFont.setSubset(false); // fully embedded as per requirements
    }
    return font;
}

我有什么办法可以告诉iText不要在它之间放置任何空格最高字形的顶部(在这种情况下为D和Z)和ColumnText框的顶部?

Is there anything I can do to tell iText not to put any space between the top of the highest glyphs (D and Z in this case) and the top of the ColumnText box?

我试着查看BaseFont.getAscension()以查看是否存在有任何值可用,但它最终结果为0.

I tried looking at the BaseFont.getAscension() to see if there was any value available, but it ended up 0 anyway.

推荐答案

请看一下 ColumnTextAscender 示例:

在这两种情况下,我都绘制了一个红色矩形:

In both cases, I have drawn a red rectangle:

rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(0.5f);
rect.setBorderColor(BaseColor.RED);
PdfContentByte cb = writer.getDirectContent();
cb.rectangle(rect);

在左侧,您可以看到以您的方式添加的文字。在这种情况下,iText将添加额外的空间,通常称为前导。在右侧,您可以看到添加文本的方式。在这种情况下,我们告诉 ColumnText 它需要使用字体的Ascender值:

To the left, you see the text added the way you do. In this case, iText will add extra space commonly known as the leading. To the right, you see the text added the way you want to add it. In this case, we have told the ColumnText that it needs to use the Ascender value of the font:

Phrase p = new Phrase("This text is added at the top of the column.");
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(rect);
ct.setUseAscender(true);
ct.addText(p);
ct.go();

现在文本的顶部触及矩形的边框。

Now the top of the text touches the border of the rectangle.

这篇关于在ColumnText中将短语放在iText中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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