Creating the Table and set the Column Width And Spacing In Pdf in java


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 second 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) {
          try {
                 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();
                 fos.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 >


Summary -

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.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java


Creating first pdf and setting attributes using itext in java - iText tutorial >

Create the first Pdf Example Using iText library in java - iText java tutorial example

Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java



Creating TABLES in pdf in java - iText tutorial >

How to Create the Table In Pdf in java - iText java tutorial

How to do the Cell Alignment In Pdf generated table in Java program

How to Set Cell Background Colour In Pdf Table in java example

How to Set Cell Background Colour In Pdf Table in java - iText java tutorial

How to Create Images In Pdf generated Table in java itext

How to Create Nested Tables in Pdf in java - iText java tutorial

How to Create the Table With Multiple Rows In Pdf in java program


Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >

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


Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >

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

How To Set SuperScript And SubScript In Pdf in java - iText java tutorial

UnderLine Text In Pdf in java - iText java tutorial example

Strikethrough Text In Pdf in java - iText java tutorial program


HTML to PDF in java - iText tutorial >

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


Create new page, set page height and width in java - iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program




No comments:

Post a Comment