2016-08-25 4 views

답변

0

사실, 당신은 당신의 질문에 답을 포함했다 : 당신이 테이블 이벤트를 사용해야 나는 아래의 이미지처럼 달력 테이블을 만들려하기 때문에

이유입니다. RowBackground 예제를 살펴보십시오. 여기에는 단일 이벤트의 배경을 그리는 테이블 이벤트를 만들 수있는 테이블 이벤트 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

당신이 배경 바의 크기를 적용하려는 경우가 llx, lly, urxury 값을 조정할 수 있습니다.

단일 행의 배경을 그릴 수있는 경우 코드를 확장하여 둘 이상의 행 배경을 그릴 수 있습니다.

+1

감사합니다. Bruno. 모든 iText 예제에서 BTW 훌륭한 직업! 나는 그들에게서 많은 것을 배웠다. –

+0

감사합니다. 이러한 의견은 높이 평가됩니다. –

관련 문제