在ColumnText中将短语放在iText中

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

文本对齐问题

黑色标题是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); ...  //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示例:

在此处输入图像描述

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

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

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