Mega Code Archive

 
Categories / Java Tutorial / PDF
 

Set alignment for Table cell

import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class MainClass {   public static void main(String[] args) throws Exception {     Document document = new Document();     PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));     document.open();     PdfPTable table = new PdfPTable(2);     PdfPCell cell;     Paragraph p = new Paragraph(".");     table.addCell("default alignment");     cell = new PdfPCell(p);     table.addCell(cell);     table.addCell("centered alignment");     cell = new PdfPCell(p);     cell.setHorizontalAlignment(Element.ALIGN_CENTER);     table.addCell(cell);     table.addCell("right alignment");     cell = new PdfPCell(p);     cell.setHorizontalAlignment(Element.ALIGN_RIGHT);     table.addCell(cell);     table.addCell("justified alignment");     cell = new PdfPCell(p);     cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);     table.addCell(cell);     document.add(table);     document.close();   } }