gpt4 book ai didi

java - ColumnText 中短语的 iText 放置

转载 作者:行者123 更新时间:2023-12-01 12:02:17 25 4
gpt4 key购买 nike

所以我正在处理这样一种情况:我将一个 Phrase 添加到 ColumnText 对象中。

Text alignment issue

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

 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 框的顶部之间放置任何空格?

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

最佳答案

请查看 ColumnTextAscender示例:

enter image description here

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

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

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

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

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

关于java - ColumnText 中短语的 iText 放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27906725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com