iText单元格边框切割文本

时间:2021-11-18 01:19:00

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:

我正在编写一个程序,使用iText生成带有表格的pdf或rtf文件。我使用iText类表和单元格,而不是更具体的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();

  }

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?

有没有办法解决这个问题,要么通过向下移动整个边框(不影响文本放置),要么摆脱每行之间的空格(间距似乎只是文本上方的问题,而不是下面)没有将单元格填充设置为-1?

2 个解决方案

#1


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

编写一个类或常用方法来构建表 - 无论您使用的是Table还是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.

这些方法将为您处理标准对齐,基于上升/下降的测量等。它们还提供了一个常见的位置来添加“3pt空白段”或您可能需要的任何其他标准格式。

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

OO软件并不意味着要克服重复的,可能不一致的代码段。

Hope this helps.

希望这可以帮助。

#2


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

你应该使用PdfPTable它有很多有用的方法和包装良好的组件我发布了这个答案所以任何一个面临同样的问题知道从哪里开始它可能不是问题的典型答案但是它来了......

Organizing content in tables
The PDF output

组织表格中的内容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();
    }
}

#1


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

编写一个类或常用方法来构建表 - 无论您使用的是Table还是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.

这些方法将为您处理标准对齐,基于上升/下降的测量等。它们还提供了一个常见的位置来添加“3pt空白段”或您可能需要的任何其他标准格式。

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

OO软件并不意味着要克服重复的,可能不一致的代码段。

Hope this helps.

希望这可以帮助。

#2


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

你应该使用PdfPTable它有很多有用的方法和包装良好的组件我发布了这个答案所以任何一个面临同样的问题知道从哪里开始它可能不是问题的典型答案但是它来了......

Organizing content in tables
The PDF output

组织表格中的内容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();
    }
}