简体   繁体   中英

iText 7 - set width of cells

I'm using iText 7 to generate a PDF with a table inside.

I would like to define the size of my cells.

For example I've this : 在此处输入图片说明

I would like to reduce columns with numbers (07, 08 ...) and increase the others.

Here is how I create my table :

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(""));

I tried using the method setWidth of Cell but it doesn't do anything.

I tried defining width of columns at creation of table :

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);

But it creates a big mess with header cells one multiple lines.

Can you help me pls ?

If float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2}; is an array containing the relative widths you want for each column, transform them to a UnitValue percent array, and cosntruct your table using that:

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

If you want the absolute width of a table to be respected above the widths passed for the columns, use Table#setFixedLayout() .

It might help to run and/or experiment on the following piece of code to see the effect of each different kind of declaration:

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");
}

I don't have enough reputation to comment, so I've added the C# version as an additional answer. Also updated for the later version of IText7, as SetWidthPercentage is deprecated.

public static void CreatePdf(String dest)
{
    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.SetWidth(UnitValue.CreatePercentValue(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.SetWidth(UnitValue.CreatePercentValue(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.SetWidth(UnitValue.CreatePercentValue(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(UnitValue.CreatePercentValue(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.SetWidth(UnitValue.CreatePercentValue(tableWidth));
    table.SetFixedLayout();
    fillTable(table);
    doc.Add(table);

    doc.Close();
}

public static 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM