JavaMadeSoEasy.com (JMSE)

In this core java tutorial we will learn Creating the Table and set the Column Width And Spacing In Pdf in java using iText library - core java tutorial in Java with program and examples.

We will create PdfPTable ( com.itextpdf.text.pdf.PdfPTable ) and then create cells ( com.itextpdf.text.pdf.PdfPCell ) and then use -

setWidthPercentage method on table to set the table width in pdf in java. setSpacingBefore method on table to set the space before table in pdf in java. setSpacingAfter method on table to set the space after table in pdf in java. Create table with 3 columns ( new PdfPTable(3)).

Set width of columns ( float [] columnWidths = < 1f, 2f, 1f >), here s econd column will be twice as first and third and then use table .setWidths( columnWidths ) to set the width of columns in table.

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

Convert Html To Pdf in java using iText - iText java tutorial

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial

Download jars required to execute program > Or, you may download jars from here .

Program/Example to create the Table and set the Column Width And Spacing In Pdf in java - Using iText library - core java tutorial

import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; * Creating the Table In Pdf and set the Column Width And Spacing * Example in iText library - core java tutorial public class CreatingTableInPdfSetColumnWidthAndSpacingExample < public static void main(String[] args ) < String pdfFilePath = "e:/Creating Table In Pdf Set Column Width And Spacing Example.pdf" ; OutputStream fos = new FileOutputStream( new File( pdfFilePath )); Document document = new Document(); PdfWriter. getInstance ( document , fos ); document .open(); * Now, we will create table in PDF file PdfPTable table = new PdfPTable(3); // Create 3 columns in table. // Set table Width as 100% table .setWidthPercentage(100f); // Space before and after table table .setSpacingBefore(20f); table .setSpacingAfter(20f); // Set Column widths of table float [] columnWidths = < 1f, 2f, 1f >; // Second column will be // twice as first and third table .setWidths( columnWidths ); PdfPCell cell1 = new PdfPCell( new Paragraph( "Cell 1" )); PdfPCell cell2 = new PdfPCell( new Paragraph( "Cell 2" )); PdfPCell cell3 = new PdfPCell( new Paragraph( "Cell 3" )); table .addCell( cell1 ); table .addCell( cell2 ); table .addCell( cell3 ); document .add( table ); document .close(); System. out .println( "PDF created in >> " + pdfFilePath ); > catch (Exception e ) < e .printStackTrace(); /* Output of above program PDF created in >> e:/Creating Table In Pdf Set Column Width And Spacing Example.pdf

PDF formed after executing above java program(Creating the Table and set the Column Width And Spacing In Pdf in java) will look like this >

In this core java tutorial we learned how to Create the Table and set the Column Width And Spacing In Pdf in java using iText library.

We created PdfPTable ( com.itextpdf.text.pdf.PdfPTable ) and then created cells ( com.itextpdf.text.pdf.PdfPCell ) and then used -

setWidthPercentage method on table to set the table width in pdf.

setSpacingBefore and setSpacingAfter method on table to set the space before and after table in pdf in java.

Created table with 3 columns, Setted the width of columns and then used table .setWidths( columnWidths ) to set the width of columns in table.

Having any doubt? or you you liked the tutorial! Please comment in below section.

Please express your love by liking JavaMadeSoEasy.com ( JMSE ) on facebook , following on google + or Twitter .