iText 单元格边框切断文本

25
我正在编写一个使用iText生成PDF或RTF文件,并在其中包含表格的程序。我使用了iText类table和cell,而不是更具体的RtfTable或pdfTable,以便最终可以生成任一个文件。我需要将单元格填充设置为-1的值,否则打印出来的数据表中每一行之间的空间太大了。
然而,现在我想要添加边框(特别是到PDF文件中),但是单元格与文本不对齐。每个单元格的底部边框直接穿过文本,只有当将单元格填充设置为2或更高时,才实际上围绕着文本。以下是我正在做的示例:
  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();

  }
有没有办法解决这个问题,即将整个边框向下移动一点(不影响文本位置),或者消除每行之间的空白(看起来只有在文本上方存在间距问题,而不是下方),而不设置单元格填充为-1?

你找到解决方法了吗?我也遇到了同样的问题。我想让单元格更矮一些,这似乎是由于填充引起的,但当我减少填充时,文本会穿过底部边框。 - Michael Klement
2
没关系,我已经转换到PdfPTable了,现在一切都好了。由于我只生成PDF文件,所以这个解决方案对我来说已经足够了。 - Michael Klement
请在你的Java代码中添加所需的库,我尝试实现你提供的完整类示例。 - shareef
2个回答

1
编写一个类或通用方法来构建您的表格 - 无论您是使用Table还是PdfPTable。
这些方法将为您处理标准对齐、基于上升/下降的测量等。它们还提供了一个常见的地方来添加“3pt空段落”或其他您可能需要的标准格式。
面向对象的软件不应该只是关于重复和潜在不一致的代码部分。
希望这可以帮助到您。

0

你应该使用 PdfPTable,它有许多有用的方法,并且是一个很好封装的组件。我发布了这个答案,以便任何遇到相同问题的人都知道从哪里开始。虽然这可能不是问题的典型答案,但它在这里...

在表格中组织内容
PDF输出

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接