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 java.io.*;
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"));
            iText_Create_Table.open();            
            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 */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 2 We will specify some cell positions in this example. Top Align the Contents"));
            table_cell.setFixedHeight(widthval);
            table_cell.setVerticalAlignment(Element.ALIGN_TOP); /* Top Align */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 3 iText cell alignment.setting both horizontal and vertical alignment"));
            table_cell.setFixedHeight(widthval);
            table_cell.setVerticalAlignment(Element.ALIGN_CENTER); /* Center align vertically */
            table_cell.setHorizontalAlignment(Element.ALIGN_LEFT); /* Left align horizontally */
            my_first_table.addCell(table_cell);            
            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.setFixedHeight(widthval);
            table_cell.setHorizontalAlignment(Element.ALIGN_RIGHT); /* Align Right */
            my_first_table.addCell(table_cell);          
            table_cell=new PdfPCell(new Phrase("Cell 5 Center align contents of the Cell using iText API"));
            table_cell.setFixedHeight(widthval);
            table_cell.setHorizontalAlignment(Element.ALIGN_CENTER); /* Align Center */
            my_first_table.addCell(table_cell);       
            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.setFixedHeight(widthval);
            table_cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED); /* Align Justified */
            my_first_table.addCell(table_cell);                  
            iText_Create_Table.add(my_first_table);                       
            iText_Create_Table.close();
        }
        catch (Exception i)
        {
            System.out.println(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)
iText Cell ALIGN_JUSTIFIED_ALL option
ALIGN_JUSTIFIED_ALL option

1 comment:

  1. thanks helps more the site documentation

    ReplyDelete