2016-08-25 20 views

答えて

0

は実際に、あなたの問題の答えが含まれている:あなたは、テーブルイベントを使用する必要があり、私は下の画像のようにカレンダーのテーブルを作成したいので

理由があります。 RowBackgroundの例を見てください。これにはテーブルイベントを作成して1行の背景を描画するテーブルイベントRowBackgroundEventが含まれています。

public class RowBackgroundEvent implements PdfPTableEvent { 
    // the row number of the row that needs a background 
    protected int row; 

    // creates a background event for a specific row 
    public RowBackgroundEvent(int row) { 
     this.row = row; 
    } 

    /** 
    * Draws the background of a row. 
    */ 
    @Override 
    public void tableLayout(PdfPTable table, float[][] widths, float[] heights, 
     int headerRows, int rowStart, PdfContentByte[] canvases) { 
     float llx = widths[row][0]; 
     float lly = heights[row]; 
     float urx = widths[row][widths[row].length - 1]; 
     float ury = heights[row - 1]; 
     float h = ury - lly; 
     PdfContentByte canvas = canvases[PdfPTable.BASECANVAS]; 
     canvas.saveState(); 
     canvas.arc(llx - h/2, lly, llx + h/2, ury, 90, 180); 
     canvas.lineTo(urx, lly); 
     canvas.arc(urx - h/2, lly, urx + h/2, ury, 270, 180); 
     canvas.lineTo(llx, ury); 
     canvas.setColorFill(BaseColor.LIGHT_GRAY); 
     canvas.fill(); 
     canvas.restoreState(); 
    } 
} 

これは、このイベントは使用されている方法です。

public void createPdf(String filename) throws SQLException, DocumentException, IOException { 
    // step 1 
    Document document = new Document(PageSize.A4.rotate()); 
    // step 2 
    PdfWriter.getInstance(document, new FileOutputStream(filename)); 
    // step 3 
    document.open(); 
    // step 4 
    PdfPTableEvent event = new RowBackgroundEvent(3); 
    PdfPTable table = new PdfPTable(7); 
    table.setTableEvent(event); 
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER); 
    for (int i = 0; i < 10; i++) { 
     for (int j = 1; j < 8; j++) { 
      table.addCell(String.valueOf(j)); 
     } 
    } 
    document.add(table); 
    // step 5 
    document.close(); 
} 

あなたが見ることができるように、私たちは第三列の背景をしたいです。結果は以下のようになります。

enter image description here

バックグラウンドバーのサイズを適応したい場合はllxllyurx、およびury値を微調整することができます。

1行の背景を描画できる場合は、コードを拡張して複数の行の背景を描画できます。

+1

ありがとう、ブルーノ。どんなiTextの例でもBTW素晴らしい仕事!私は彼らから多くを学んだ。 –

+0

ありがとうございました。そのようなフィードバックは高く評価されています。 –

関連する問題