2017-10-25 43 views
0

iText 7を使用して、内部にテーブルを含むPDFを生成しています。iText 7 - セルの幅を設定する

私は自分の細胞のサイズを定義したいと思います。 enter image description here

私は数字(07、08 ...)の列を削減し、他の人を増やしたい:

たとえば私は、これがしました。ここで

は、私は私のテーブルを作成する方法である:

int nbColumns = 7 + planning.size() *4; 
Table table = new Table(nbColumns); 
table.setWidthPercent(100); 
table.addCell(createCell("")); 
table.addCell(createCell("Col1")); 
table.addCell(createCell("Col2")); 
DateFormat hourFormat = new SimpleDateFormat("HH", Locale.FRENCH); 
for(Date hourDate : planning){ 
    table.addCell(new Cell(1,4).setTextAlignment(TextAlignment.CENTER).add(hourFormat.format(hourDate)).setFont(regular).setFontSize(10)); 
} 

table.addCell(createCell("Long")); 
table.addCell(createCell("A")); 
table.addCell(createCell("B")); 
table.addCell(createCell("")); 

私は方法セルのsetWidthを使用してみましたが、それは何もしません。

Iは、テーブルの作成時に列の幅を規定しようとした:

float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2}; 
Table table = new Table(colWidths); 

しかし、ヘッダーセルつの多重線で大きな混乱を作成します。

私に助けてもらえますか?

+1

うーむ、iText7テーブルのは、ちょっと複雑な獣ですHTMLテーブルに追加し、セルの内容に応じてセルを拡大することができます。彼らの行動やさまざまなオプションを正しく示すためにいくつかの例を挙げることができるかどうかがわかります。 –

答えて

3

float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2};は、各列にしたい相対的な幅を含む配列である場合は、UnitValueパーセント配列にそれらを変換し、それを使用して、テーブルをcosntruct:

Table table = new Table(UnitValue.createPercentArray(colWidths)); 

あなたは、テーブルの絶対幅をしたい場合列に渡される幅を超えて尊重するには、Table#setFixedLayout()を使用します。これは、宣言のそれぞれ異なる種類の効果を確認するために実行するのに役立ち、および/または以下のコードの実験かもしれない

:彼らは同様に動作するので

public void createPdf(String dest) throws IOException, FileNotFoundException{ 
    PdfWriter writer = new PdfWriter(dest); 
    PdfDocument pdfDoc = new PdfDocument(writer); 
    Document doc = new Document(pdfDoc); 
    float tableWidth = 100f; 
    //Using defaults 
    doc.add(new Paragraph("default/auto-layout")); 
    doc.add(new Paragraph("Using a float[]")); 
    float[] colWidths = {1,2,3,4}; 
    Table table = new Table(colWidths); 
    table.setWidthPercent(tableWidth); 
    fillTable(table); 
    doc.add(table); 

    doc.add(new Paragraph("Using a UnitValue[] point array")); 
    colWidths=new float[]{20f,40f,80f,160f}; 
    table = new Table(UnitValue.createPointArray(colWidths)); 
    table.setWidth(tableWidth); 
    fillTable(table); 
    doc.add(table); 

    doc.add(new Paragraph("Using a UnitValue[] percent array")); 
    colWidths=new float[]{1,2,3,4}; 
    table = new Table(UnitValue.createPercentArray(colWidths)); 
    table.setWidthPercent(tableWidth); 
    fillTable(table); 
    doc.add(table); 

    doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE)); 
    //Using fixedLayout 
    doc.add(new Paragraph("Using fixedLayout")); 
    doc.add(new Paragraph("Using a float[]")); 
    colWidths =new float[]{1,2,3,4}; 
    table = new Table(colWidths); 
    table.setFixedLayout(); 
    table.setWidthPercent(tableWidth); 
    fillTable(table); 
    doc.add(table); 

    doc.add(new Paragraph("Using a UnitValue[] point array")); 
    colWidths=new float[]{20f,40f,80f,160f}; 
    table = new Table(UnitValue.createPointArray(colWidths)); 
    table.setWidthPercent(tableWidth); 
    table.setFixedLayout(); 
    fillTable(table); 
    doc.add(table); 

    doc.add(new Paragraph("Using a UnitValue[] percent array")); 
    colWidths=new float[]{1,2,3,4}; 
    table = new Table(UnitValue.createPercentArray(colWidths)); 
    table.setWidthPercent(tableWidth); 
    table.setFixedLayout(); 
    fillTable(table); 
    doc.add(table); 

    doc.close(); 
} 

public void fillTable(Table table){ 
    table.addCell("Water"); 
    table.addCell("Fire"); 
    table.addCell("Heaven"); 
    table.addCell("As I'm grounded here on"); 
    table.addCell("It's waving oceans are calling"); 
    table.addCell("is burning me deep inside"); 
    table.addCell("can wait for me"); 
    table.addCell("Earth"); 
} 
関連する問題