如何摆脱 PdfPCell、iText 5 中的顶部填充
How to get rid of top padding in PdfPCell, iText 5
我正在使用 iText 5 表格创建标签(如 Avery 标签)。标签元素的定位需要一些非常严格的公差,以便适合标签上的所有内容。我的问题是我在标签上有不同的区域作为 PdfPCells。我需要将文本放入这些区域,浪费 0 space。但我似乎总是在单元格顶部有额外的 space 。使用 .setVerticalAlignment(Element.ALIGN_TOP) 可以很好地说明这一点;这不会将文本带到我的单元格顶部。
我会显示图像,但显然我不允许。
如何摆脱这个 space?
package actions.test;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfCellTest
{
public static void main(String[] args) throws Exception {
System.out.println("Cell Test");
BaseFont bf = BaseFont.createFont
(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
Font companyFont =new Font(bf);
companyFont.setSize(10.5f);
companyFont.setColor(BaseColor.BLUE);
companyFont.setStyle(Font.BOLD);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("c:\temp\celltest.pdf"));
document.open();
PdfPTable main = new PdfPTable(1);
main.setWidthPercentage(30);
Phrase companyPhrase = new Phrase("My Company Name, LLC",companyFont);
PdfPCell companyCell = new PdfPCell(companyPhrase);
companyCell.setHorizontalAlignment(Element.ALIGN_CENTER);
companyCell.setVerticalAlignment(Element.ALIGN_TOP);
companyCell.setBorder(Rectangle.BOX);
companyCell.setBorderColor(BaseColor.RED);
companyCell.setPadding(0);
companyCell.setFixedHeight(10.5f);
companyCell.setBackgroundColor(BaseColor.WHITE);
main.addCell(companyCell);
document.add(main);
document.close();
}
}
您已经非常接近解决方案了。您设置的所有属性都可以,现在尝试添加:
companyCell.setUseAscender(true);
companyCell.setUseDescender(true);
这些方法有什么作用?它们考虑存储在正在使用的字体中的指标。您谈论的是顶部填充,但您会注意到 "descender" 对底部填充也有很好的效果。
顶部 "padding" 并不是真正的填充。这是 "leading"。您使用的是默认字体,即 Helvetica 12pt。默认行距是字体大小的 1.5 倍。那是 18pt。您在 文本模式 下工作,这意味着您可以在单元格级别定义行距(与您定义的 复合模式 相反元素级别的领先者)。例如:您可以像这样从顶部 "padding" 删除 4pt:
companyCell.setLeading(14);
重要提示:这也会减少不同行之间的间距。如果这不是一个选项,您可能需要切换到复合模式。
我正在使用 iText 5 表格创建标签(如 Avery 标签)。标签元素的定位需要一些非常严格的公差,以便适合标签上的所有内容。我的问题是我在标签上有不同的区域作为 PdfPCells。我需要将文本放入这些区域,浪费 0 space。但我似乎总是在单元格顶部有额外的 space 。使用 .setVerticalAlignment(Element.ALIGN_TOP) 可以很好地说明这一点;这不会将文本带到我的单元格顶部。
我会显示图像,但显然我不允许。
如何摆脱这个 space?
package actions.test;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfCellTest
{
public static void main(String[] args) throws Exception {
System.out.println("Cell Test");
BaseFont bf = BaseFont.createFont
(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
Font companyFont =new Font(bf);
companyFont.setSize(10.5f);
companyFont.setColor(BaseColor.BLUE);
companyFont.setStyle(Font.BOLD);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("c:\temp\celltest.pdf"));
document.open();
PdfPTable main = new PdfPTable(1);
main.setWidthPercentage(30);
Phrase companyPhrase = new Phrase("My Company Name, LLC",companyFont);
PdfPCell companyCell = new PdfPCell(companyPhrase);
companyCell.setHorizontalAlignment(Element.ALIGN_CENTER);
companyCell.setVerticalAlignment(Element.ALIGN_TOP);
companyCell.setBorder(Rectangle.BOX);
companyCell.setBorderColor(BaseColor.RED);
companyCell.setPadding(0);
companyCell.setFixedHeight(10.5f);
companyCell.setBackgroundColor(BaseColor.WHITE);
main.addCell(companyCell);
document.add(main);
document.close();
}
}
您已经非常接近解决方案了。您设置的所有属性都可以,现在尝试添加:
companyCell.setUseAscender(true);
companyCell.setUseDescender(true);
这些方法有什么作用?它们考虑存储在正在使用的字体中的指标。您谈论的是顶部填充,但您会注意到 "descender" 对底部填充也有很好的效果。
顶部 "padding" 并不是真正的填充。这是 "leading"。您使用的是默认字体,即 Helvetica 12pt。默认行距是字体大小的 1.5 倍。那是 18pt。您在 文本模式 下工作,这意味着您可以在单元格级别定义行距(与您定义的 复合模式 相反元素级别的领先者)。例如:您可以像这样从顶部 "padding" 删除 4pt:
companyCell.setLeading(14);
重要提示:这也会减少不同行之间的间距。如果这不是一个选项,您可能需要切换到复合模式。