2009-06-12 68 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空白段落」或任何其他標準格式,您可能需要。

面向對象軟件並不意味着要關注重複的,可能不一致的代碼段。

希望這會有所幫助。