indra
indra

Reputation: 832

set the table cell width in iText java pdf

I am using the opensource http://itextpdf.com/ , to create some pdf .

I could create a table , but width of all column are same , I need to change the width of the particular column .

PdfPTable table = new PdfPTable(6);
Phrase tablePhrse = new Phrase("Sl n0", normalFontBold);
    PdfPCell c1 = new PdfPCell(tablePhrse);
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

I could not find any method to set the width of the column , pls suggest some way to do achieve this ..

Upvotes: 13

Views: 52430

Answers (5)

Rodney Dunn
Rodney Dunn

Reputation: 21

Make sure to set table total width

// Set Column Number
PdfPTable table = new PdfPTable(2);

// Set Table Total Width
table.setTotalWidth(500);

// Set Each Column Width - Make Sure Array is the same number specified in constructor
table.setWidths(new int[]{50, 450});

Upvotes: 1

Sujeet Kumar
Sujeet Kumar

Reputation: 1942

Try this.

float[] columnWidths = new float[]{10f, 20f, 30f, 10f};
table.setWidths(columnWidths);

Upvotes: 5

krishna ragav
krishna ragav

Reputation: 192

float[] columnWidths = {1, 5, 5};

// columnWidths = {column1, column2, column3...}

PdfPTable table = new PdfPTable(columnWidths)

Upvotes: 1

SivaRamaKrishnan T
SivaRamaKrishnan T

Reputation: 31

Here is the corrected one if table.setWidths(new int[]{200,50}); does not work.

innertable.setWidthPercentage(50);

Upvotes: 1

Smit
Smit

Reputation: 4715

You can make use of setWidths() method.

table.setWidths(new int[]{200,50});

public void setWidths(int[] relativeWidths)

Upvotes: 23

Related Questions