Mega Code Archive

 
Categories / Java Tutorial / PDF
 

Add paragraph to table cell with alignment setting

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;     table.addCell("default alignment");     table.addCell("paragraph alignment");     Paragraph p1 = new Paragraph("Quick brown fox");     Paragraph p2 = new Paragraph("jumps over");     p2.setAlignment(Element.ALIGN_CENTER);     Paragraph p3 = new Paragraph("the lazy dog.");     p3.setAlignment(Element.ALIGN_RIGHT);     cell = new PdfPCell();     cell.addElement(p1);     cell.addElement(p2);     cell.addElement(p3);     table.addCell(cell);     table.addCell("extra indentation (cell)");     document.add(table);     document.close();   } }