Java iText Align Cell Content Example Tutorial - Format Options - Part 3

In our series of Java iText Tutorials on creating table in a PDF document and applying formatting options to it, in this post we will describe how to align your text (various alignment options) in a Cell of a PDF table. Positioning content inside a Cell is one of the common tasks when you create a PDF file and we will provide enough examples in iText for you to understand various alignment options for a PdfCell object. You may also be interested in some of our formatting options tutorials presented earlier, provided for you below;

There are multiple options for you to position your text in a Cell of a Pdf table.These options can be referred from the interface "Element" defined in com.itextpdf.text.We will now look into some methods for aligning text in a cell and then discuss how to feed various values for these alignment options. There are two key methods to specify alignment which are defined in com.itextpdf.text.pdf.PdfPCell. These methods are listed below;
setHorizontalAlignment - To Specify the Horizontal alignment of a Cell
setVerticalAlignment - To Specify the Vertical Alignment of a Cell
Here is a complete Java code that explains cell alignment options;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class iTextAlignCellContents {  
     public static void main(String[] args){
        try {
            Document iText_Create_Table = new Document();
            PdfWriter.getInstance(iText_Create_Table, new FileOutputStream("iText_Cell_Alignment_Example.pdf"));
            PdfPTable my_first_table = new PdfPTable(3);            
            PdfPCell table_cell;
            float widthval=75;
            table_cell=new PdfPCell(new Phrase("Cell 1 Alignment Options for PdfCell in iText. Bottom align the contents in a Cell"));
            table_cell.setFixedHeight(widthval); /* Specify Cell height using setFixedHeight Method */
            table_cell.setVerticalAlignment(Element.ALIGN_BOTTOM); /* Bottom align */
            table_cell=new PdfPCell(new Phrase("Cell 2 We will specify some cell positions in this example. Top Align the Contents"));
            table_cell.setVerticalAlignment(Element.ALIGN_TOP); /* Top Align */
            table_cell=new PdfPCell(new Phrase("Cell 3 iText cell alignment.setting both horizontal and vertical alignment"));
            table_cell.setVerticalAlignment(Element.ALIGN_CENTER); /* Center align vertically */
            table_cell.setHorizontalAlignment(Element.ALIGN_LEFT); /* Left align horizontally */
            table_cell=new PdfPCell(new Phrase("Cell 4 I want to align contents of cell 4 to right. so, we use Element.ALIGN_RIGHT as input"));
            table_cell.setHorizontalAlignment(Element.ALIGN_RIGHT); /* Align Right */
            table_cell=new PdfPCell(new Phrase("Cell 5 Center align contents of the Cell using iText API"));
            table_cell.setHorizontalAlignment(Element.ALIGN_CENTER); /* Align Center */
            table_cell=new PdfPCell(new Phrase("Cell 6 add some more content to see how justification works. We will add a long text for this cell to check this."));
            table_cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED); /* Align Justified */
        catch (Exception i)
The code is commented well enough for you to understand. We add six cells to the table and position them using various options available to us. An output of this code is provided below;
iText Cell Alignment Options
iText Cell Alignment Options
Now, you should also note that there exists a property ALIGN_JUSTIFIED_ALL which aligns the last line of the cell content to justified as well, as per iText documentation. When I tried this for Cell 6, I got a different version of the cell content as shown below. (I liked the original option though)

1 comment:

  1. thanks helps more the site documentation