如何在LineSeparator itext中减小行距 [英] How to decrease the line spacing in LineSeparator itext

查看:414
本文介绍了如何在LineSeparator itext中减小行距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个构建自定义PDF的Android应用程序,我将itext用作库支持,但是分隔符存在很大问题.

I'm developing an Android application that build a custom PDF, I used itext as library support, but I have a big problem with the separator.

要分离数据,我使用此功能:

To separate the data I use this function:

public void addSeparator(PdfPCell cellToAdd){
    LineSeparator ls = new LineSeparator();
    ls.setLineWidth(1);
    cellToAdd.addElement(new Chunk(ls));
}

cellToAdd是我需要添加LineSeparator的单元格,结果的想法是:

cellToAdd is the cell where I need to add the LineSeparator, the idea of the result is:

my fantastic data

-----------------

Other data

我需要增加行和数据之间的间距,但是我不知道该怎么做.

I need to increase the space beetween the line and the data, but I don't know how to do this.

推荐答案

看看以下屏幕截图:

第一个单元格显示您描述的问题.第二行通过更改LineSeparator的属性来解决该问题.如果这还不够,则需要更改Paragraph实例.

The first cell shows the problem you describe. The second row solves it by changing a property of the LineSeparator. If that isn't sufficient, you need to change the Paragraph instances.

这是创建完整表的代码:

This is the code to create the full table:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(1);
    table.addCell(getCell1());
    table.addCell(getCell2());
    table.addCell(getCell3());
    table.addCell(getCell4());
    document.add(table);
    document.close();
}

第一个单元格是这样创建的:

The first cell is created like this:

public PdfPCell getCell1() {
    PdfPCell cell = new PdfPCell();
    Paragraph p1 = new Paragraph("My fantastic data");
    cell.addElement(p1);
    LineSeparator ls = new LineSeparator();
    cell.addElement(ls);
    Paragraph p2 = new Paragraph("Other data");
    cell.addElement(p2);
    return cell;
}

我们只需添加LineSeparator,它就会坚持到第一段.我们可以通过引入负偏移量来避免这种情况:

We just add the LineSeparator and it sticks to the first paragraph. We can avoid this by introducing a negative offset:

public PdfPCell getCell2() {
    PdfPCell cell = new PdfPCell();
    Paragraph p1 = new Paragraph("My fantastic data");
    cell.addElement(p1);
    LineSeparator ls = new LineSeparator();
    ls.setOffset(-4);
    cell.addElement(ls);
    Paragraph p2 = new Paragraph("Other data");
    cell.addElement(p2);
    return cell;
}

如果需要更多空间,我们可以增加第二段的开头并增加偏移量:

If more space is needed, we can increase the leading of the second paragraph and increase the offset:

public PdfPCell getCell3() {
    PdfPCell cell = new PdfPCell();
    Paragraph p1 = new Paragraph("My fantastic data");
    cell.addElement(p1);
    LineSeparator ls = new LineSeparator();
    ls.setOffset(-8);
    cell.addElement(ls);
    Paragraph p2 = new Paragraph("Other data");
    p2.setLeading(25);
    cell.addElement(p2);
    return cell;
}

这可能是不可接受的,因为如果其他数据包含多于一行,则行首也会影响后续行.

This may not be acceptable, because the leading will also have an effect on the subsequent lines if the Other data consists of more than one line.

最好的解决方案是使用setSpacingBefore()setSpacingAfter()(或同时使用):

The best solution is to use setSpacingBefore() or setSpacingAfter() (or both):

public PdfPCell getCell4() {
    PdfPCell cell = new PdfPCell();
    Paragraph p1 = new Paragraph("My fantastic data");
    p1.setSpacingAfter(20);
    cell.addElement(p1);
    LineSeparator ls = new LineSeparator();
    cell.addElement(ls);
    Paragraph p2 = new Paragraph("Other data");
    p2.setSpacingBefore(10);
    cell.addElement(p2);
    return cell;
}

这篇关于如何在LineSeparator itext中减小行距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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