2017-11-29 14 views
0

Bruno Lowagie의 코드를 참조하여 테이블을 세로 및 가로로 분할합니다. 하지만 헤더를 설정하는 것과 같은 추가 요구 사항이 있습니다. 아래 코드를 찾으십시오.iText java - 세로 및 가로로 테이블 분할 및 머리글 행 추가

public void createPdf(String dest) throws IOException, DocumentException { 
Document document = new Document(PageSize.A4.rotate()); 
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); 
document.open(); 
PdfPTable tbl = new PdfPTable(new float[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}); 
//loop through 100 rows 
for (int r = 1; r <= 100; r++) { 
    for (int c = 1; c <= 24; c++) { 
     tbl.addCell(String.format("r%sc%s", r, c)); 
    } 
} 
PdfContentByte canvas = writer.getDirectContent(); 
tbl.setTotalWidth(1500);//set table width 
float top = document.top() - document.topMargin() - 30; 
float yPos = top; 
int start = 0; 
int stop = 0; 
for (int i = 0; i < 100; i++) { 
    yPos -= tbl.getRowHeight(i); 
    if (yPos < 0) { 
     stop = --i; 
     table = getHeaderRow(table); 
     tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas); 
     document.newPage(); 
     tbl.writeSelectedRows(12, -1, start, stop, 5, top, canvas); 
     start = stop; 
     document.newPage(); 
     yPos = top; 
    } 
} 
tbl.writeSelectedRows(0, 12, stop, -1, document.leftMargin(), top, canvas); 
document.newPage(); 
tbl.writeSelectedRows(12, -1, stop, -1, 5, top, canvas); 
document.close(); 
} 

//Method to create header row 
private static PdfPTable getHeaderRow(PdfPTable table) { 
    BaseColor myColor = WebColors.getRGBColor("#475a6d"); 
    table.setWidthPercentage(100); 
    Font f = new Font(); 
    f.setFamily("sans-serif"); 
    f.setSize(10); 
    f.setColor(BaseColor.WHITE); 
    PdfPCell c1 = new PdfPCell(new Phrase("Id", f)); 
    c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
    c1.setNoWrap(true); 
    c1.setBackgroundColor(myColor); 
    table.addCell(c1); 
    //... and so on 
} 

이 경우 테이블 열 생성이 끝날 때 헤더 행이 추가됩니다. 아무도 나를 도울 수 있습니까?

답변

0

혼자서 대답을 찾았습니다. 문서를 쓰는 동안 헤더 행을 추가하는 대신 테이블 생성 자체에 추가하고 아래 코드를 다시 작성하십시오.

public void createPdf(String dest) throws IOException, DocumentException { 
Document document = new Document(PageSize.A4.rotate()); 
PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream(dest)); 
document.open(); 
PdfPTable tbl = new PdfPTable(new float[] 
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}); 
//loop through 100 rows 
for (int r = 1; r <= 100; r++) { 
for (int c = 1; c <= 24; c++) { 
    tbl.addCell(String.format("r%sc%s", r, c)); 
} 
} 
PdfContentByte canvas = writer.getDirectContent(); 
tbl.setTotalWidth(1500);//set table width 
float top = document.top() - document.topMargin() - 30; 
float yPos = top; 
int start = 0; 
int stop = 0; 
for (int i = 0; i < 100; i++) { 
yPos -= tbl.getRowHeight(i); 
if (yPos < 0) { 
    stop = --i; 
    table = getHeaderRow(table); 
if(start==0){ 
//If it is first row, header is already added 
     tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas); 
} else { 
    //Adding first row explicitly 
tbl.writeSelectedRows(0, 12, 0, 1, document.leftMargin(), top+table.getRowHeight(0), canvas); 
tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas); 
} 
    document.newPage(); 
if(start==0){ 
//If it is first row, header is already added 
    tbl.writeSelectedRows(12, -1, start, stop, 5, top, canvas); 
} else { 
//Adding first row explicitly 
tbl.writeSelectedRows(12, -1, 0, 1, document.leftMargin(), top+table.getRowHeight(0), canvas); 
tbl.writeSelectedRows(12, -1, start, stop, document.leftMargin(), top, canvas); 
} 
    start = stop; 
    document.newPage(); 
    yPos = top; 
    } 
} 
document.close(); 
}