How To Decrease The Line Spacing In Lineseparator 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 functio
Solution 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:
publicvoidcreatePdf(String dest) throws IOException, DocumentException {
Documentdocument = newDocument();
PdfWriter.getInstance(document, newFileOutputStream(dest));
document.open();
PdfPTable table = newPdfPTable(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() {
PdfPCellcell=newPdfPCell();
Paragraphp1=newParagraph("My fantastic data");
cell.addElement(p1);
LineSeparatorls=newLineSeparator();
cell.addElement(ls);
Paragraphp2=newParagraph("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() {
PdfPCellcell=newPdfPCell();
Paragraphp1=newParagraph("My fantastic data");
cell.addElement(p1);
LineSeparatorls=newLineSeparator();
ls.setOffset(-4);
cell.addElement(ls);
Paragraphp2=newParagraph("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() {
PdfPCellcell=newPdfPCell();
Paragraphp1=newParagraph("My fantastic data");
cell.addElement(p1);
LineSeparatorls=newLineSeparator();
ls.setOffset(-8);
cell.addElement(ls);
Paragraphp2=newParagraph("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() {
PdfPCellcell=newPdfPCell();
Paragraphp1=newParagraph("My fantastic data");
p1.setSpacingAfter(20);
cell.addElement(p1);
LineSeparatorls=newLineSeparator();
cell.addElement(ls);
Paragraphp2=newParagraph("Other data");
p2.setSpacingBefore(10);
cell.addElement(p2);
return cell;
}
Post a Comment for "How To Decrease The Line Spacing In Lineseparator Itext"