Create Tables In PDF Using Java iText

Chapter: iText Last Updated: 10-08-2016 18:24:32 UTC

Program:

            /* ............... START ............... */
                
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
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;

public class JavaiTextTablesInPDF {

	public static void main(String[] args) {

		Document document = new Document();
		try {
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddTableExample.pdf"));
			document.open();

			PdfPTable table = new PdfPTable(3); // 3 columns.
			table.setWidthPercentage(100); // Width 100%
			table.setSpacingBefore(10f); // Space before table
			table.setSpacingAfter(10f); // Space after table

			// Set Column widths
			float[] columnWidths = { 1f, 1f, 1f };
			table.setWidths(columnWidths);

			PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
			cell1.setBorderColor(BaseColor.BLUE);
			cell1.setPaddingLeft(10);
			cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);

			PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
			cell2.setBorderColor(BaseColor.GREEN);
			cell2.setPaddingLeft(10);
			cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);

			PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
			cell3.setBorderColor(BaseColor.RED);
			cell3.setPaddingLeft(10);
			cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);

			table.addCell(cell1);
			table.addCell(cell2);
			table.addCell(cell3);

			document.add(table);
			document.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

                /* ............... END ............... */
        

Output


Create Tables In PDF Using Java iText

Tags

Create Tables In PDF , iText, Java

Similar Programs Chapter Last Updated
Java Set Background To Table Example iText 22-07-2017
Java PDF Hello World Example iText 14-08-2016
Java PDF Formatting Using iText iText 14-08-2016
Read Existing PDF In Java iText 14-08-2016
Read OR Modify PDF File In Java Using iText iText 12-08-2016
PDF With Limited Permissions In Java Using iText iText 12-08-2016
Password Protected PDF Files Using Java iText iText 12-08-2016
Styling Or Formatting PDF In Java Using iText iText 11-08-2016
Create Lists In PDF Using Java iText iText 10-08-2016
Add Images To PDF In Java Using iText iText 10-08-2016
File Attributes To PDF In Java Using iText iText 10-08-2016
PDF Creation In Java Using iText iText 10-08-2016

1