2017-11-29 5 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(); 
} 
関連する問題