2009-06-12 46 views
25

iTextを使用して、テーブルを含むPDFまたはrtfファイルを生成するプログラムを作成しています。より具体的なRtfTableまたはpdfTableではなく、iTextクラステーブルとセルを使用して、いずれかのファイルを最後に生成できるようにしました。セルのパディングを-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

解決方法を見つけましたか?私は同じ問題を抱えています。私は、パディングによって引き起こされるように見えますが、私のセルはあまり高くしたくありませんが、パディングを減らすと、テキストが下の境界線をカットします。 –

+2

私はPdfPTableに切り替えましたが、今は問題ありません。 PDFを生成するだけなので、このソリューションで十分です。 –

+0

インポートしたライブラリをjavaに追加してください。私はあなたに "完全なクラスを提供する" – shareef

答えて

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の空白の段落」などの標準的な書式を追加する共通の場所を提供します。

OOソフトウェアは、反復的で、矛盾する可能性があるコードセクションを除外することを意図したものではありません。

これが役に立ちます。

関連する問題