java itext columntext,在ColumnText中将短语放在iText中

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

Suk1Q.png

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

...

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

}

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?

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

解决方案

Please take a look at the ColumnTextAscender example:

eljI6.png

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值