iText cell borders cutting through text
Asked Answered
C

2

25

I am writing a program that generates a pdf or rtf file with a table in it, using iText. I used the iText class table and cell, rather than the more specific RtfTable or pdfTable so that either file can be generated at the end. I needed to set the cell padding to a value of -1, or else there was too much space between each row of data on the printed sheet. However, I am now trying to add borders (specifically to the pdf file), and the cells are not lining up with the text. The bottom border of each cell cuts directly through the text. It only actually surrounds the text when the cell padding is set to 2 or higher. Below is a sample of what I am doing:

  Document document = new Document();
  Paragraph paragraph = new Paragraph();
  Font iTextFont = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL);
  try{
    PdfWriter.getInstance(document, new FileOutputStream("C:/datafiles/TestiText.pdf"));
    document.open();

    Table table = new Table(3);
    table.setPadding(-1);
    table.setWidth(90);
    Cell cell1 = new Cell();
    cell1.setBorder(Rectangle.BOX);
    cell1.setVerticalAlignment(ElementTags.ALIGN_TOP);
    table.setDefaultCell(cell1);
    paragraph = new Paragraph("header", iTextFont);
    Cell cell = new Cell(paragraph);
    cell.setHeader(true);
    cell.setColspan(3);
    table.addCell(cell);
    paragraph = new Paragraph("example cell", iTextFont);
    table.addCell(paragraph);
    paragraph = new Paragraph("one", iTextFont);
            table.addCell(cell);
    paragraph = new Paragraph("two", iTextFont);
    cell = new Cell(paragraph);
    table.addCell(paragraph);
    paragraph = new Paragraph("Does this start a new row?", iTextFont);
    table.addCell(paragraph);
    paragraph = new Paragraph("Four", iTextFont);
    table.addCell(paragraph);
    paragraph = new Paragraph("Five", iTextFont);
    table.addCell(paragraph);
    document.add(table);
  } catch (Exception e) {
    //handle exception
  }
  document.close();

  }

Is there a way to resolve this issue either by moving the whole border down a drop (without affecting the text placement), or to get rid of the spaces between each line (spacing appears to only be a problem above the text, not below) without setting the cell padding to -1?

Concatenate answered 12/6, 2009 at 17:22 Comment(3)
Did you find a solution to this? I am having the same problems. I want my cells to be less high, which seems to be caused by the padding, but when I decrease the padding the text cuts throught the bottom border.Sclerite
Nevermind, I've switched to PdfPTable and it's alright now. As I only generate PDFs, this solution is sufficient for me.Sclerite
please add the imported libraries in you java i tried to implement you example "provide full class"Essy
P
1

Write a class or common methods to build your table -- whether you're using Table or PdfPTable.

These methods will handle standard alignment, measurement based on ascenders/descenders, etc for you.. They also provide a common place to add a "3pt blank paragraph" or whatever other standard formattings, you may need.

OO software isn't meant to be about clanking out repetitive, and potentially inconsistent, sections of code.

Hope this helps.

Pout answered 3/9, 2012 at 12:5 Comment(0)
E
0

you should use PdfPTable it has many method that is useful and well wrapped component i posted this answer so any one faces the same problem know where to start it might not be the typical answer to the question but here it comes...

Organizing content in tables
The PDF output

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class Spacing {

    /** The resulting PDF file. */
    public static final String RESULT = "results/part1/chapter04/spacing.pdf";

    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args)
        throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        Phrase p = new Phrase(
            "Dr. iText or: How I Learned to Stop Worrying " +
            "and Love the Portable Document Format.");
        PdfPCell cell = new PdfPCell(p);
        table.addCell("default leading / spacing");
        table.addCell(cell);
        table.addCell("absolute leading: 20");
        cell.setLeading(20f, 0f);
        table.addCell(cell);
        table.addCell("absolute leading: 3; relative leading: 1.2");
        cell.setLeading(3f, 1.2f);
        table.addCell(cell);
        table.addCell("absolute leading: 0; relative leading: 1.2");
        cell.setLeading(0f, 1.2f);
        table.addCell(cell);
        table.addCell("no leading at all");
        cell.setLeading(0f, 0f);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(
            "Dr. iText or: How I Learned to Stop Worrying and Love PDF"));
        table.addCell("padding 10");
        cell.setPadding(10);
        table.addCell(cell);
        table.addCell("padding 0");
        cell.setPadding(0);
        table.addCell(cell);
        table.addCell("different padding for left, right, top and bottom");
        cell.setPaddingLeft(20);
        cell.setPaddingRight(50);
        cell.setPaddingTop(0);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        p = new Phrase("iText in Action Second Edition");
        table.getDefaultCell().setPadding(2);
        table.getDefaultCell().setUseAscender(false);
        table.getDefaultCell().setUseDescender(false);
        table.addCell("padding 2; no ascender, no descender");
        table.addCell(p);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(false);
        table.addCell("padding 2; ascender, no descender");
        table.addCell(p);
        table.getDefaultCell().setUseAscender(false);
        table.getDefaultCell().setUseDescender(true);
        table.addCell("padding 2; descender, no ascender");
        table.addCell(p);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(true);
        table.addCell("padding 2; ascender and descender");
        cell.setPadding(2);
        cell.setUseAscender(true);
        cell.setUseDescender(true);
        table.addCell(p);
        document.add(table);
        // step 5
        document.close();
    }
}
Essy answered 20/6, 2012 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.