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


In this core java tutorial we will learn How to do the Cell Alignment In Pdf generated table in Java -  using iText library - 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 setHorizontalAlignment and setVerticalAlignment to do cell alignment in pdf in itext in java.



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 How to do the Cell Alignment In Pdf generated table - Using iText library - core java tutorial in Java

package i1_table;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How to do the Cell Alignment In Pdf Table Example Using
* iText library - core java tutorial
*
*/
public class CellAlignmentInPdfTableExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "e:/Cell Alignment In Pdf Table Example.pdf";
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                 PdfPTable table = new PdfPTable(2); // Create 2 columns in table.
                 PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1 - Left"));
                 cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
                 cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                 PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2 - Center"));
                 cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
                 cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                 table.addCell(cell1);
                 table.addCell(cell2);
                 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:/Cell Alignment In Pdf Table Example.pdf
*/


PDF formed after executing above java program(How to do the Cell Alignment In Pdf generated table in Java) will look like this >



Summary -

So in this core java tutorial we will learn How to do the Cell Alignment In Pdf generated table in Java. dWe created PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then created cells ( com.itextpdf.text.pdf.PdfPCell) and then use setHorizontalAlignment and setVerticalAlignment to do cell alignment in pdf in itext in java.




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

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



HEADER and FOOTER in pdf in java - iText tutorial >

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



BAR AND PIE CHARTS in pdf in java - iText tutorial >

How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial



MODIFY/ EDIT pdf in java - iText tutorial >

How to Modify - Add Text To Existing PDF in java - iText java tutorial example

How To Read And Add Image To Existing Pdf in java Example - iText java tutorial



Solve common Exceptions in itext in java - iText tutorial >

How to solve the Document Has No Pages IOException in iText in java


Alternates to itext in java - iText tutorial >

What are alternatives to iText library for creating Pdf in java?




eEdit
Must read for you :