2009-06-12 69 views
25

我在写一个程序,它使用iText生成一个带有表格的pdf或rtf文件。我使用了iText类的表格和单元格,而不是更具体的RtfTable或pdfTable,这样可以在最后生成任何文件。我需要将单元格填充值设置为-1,否则打印页面上每行数据之间的空间太大。但是,我现在试图添加边框(特别是pdf文件),并且单元格不与文本排列在一起。每个单元格的底部边框直接切入文本。当单元格填充设置为2或更高时,它仅实际包围文本。下面是我在做什么的样本:iText细胞边界切割文本

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?

+0

您是否找到解决方案?我有同样的问题。我希望我的单元格不太高,这似乎是由填充引起的,但是当我减少填充时,文本将穿过底部边框。 – 2009-09-08 08:43:36

回答

0

你应该使用PdfPTable它有很多的方法,该方法是有用的,穿得组件 我张贴了这个答案,所以任何一个面临着同样的问题。你知道从哪里开始 它可能不是典型的问题的答案,但这里说到...

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

写一个类或常用的方法来建立你的表 - 您是否使用表或P dfPTable。

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

面向对象软件并不意味着要关注重复的,可能不一致的代码段。

希望这会有所帮助。