iText 单元格边框穿过文本

发布于 2024-07-23 08:40:32 字数 1681 浏览 4 评论 0原文

我正在编写一个程序,使用 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();

  }

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷子口的你 2024-07-30 08:40:32

编写一个类或常用方法来构建您的表 - 无论您使用的是 Table 还是 PdfPTable。

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

面向对象的软件并不是要消除重复的、可能不一致的代码部分。

希望这可以帮助。

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.

五里雾 2024-07-30 08:40:32

你应该使用 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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文