How to decrease the line spacing in LineSeparator

2019-09-20 07:06发布

问题:

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 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.

回答1:

Take a look at the following screen shot:

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

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.

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


标签: android itext