How to Set Cell Background Colour In Pdf Table in java


In this core java tutorial we will learn How to Set Cell Background Colour In Pdf generated Table using iText library - core java tutorial in Java with program and examples.

We will create PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then create cells ( com.itextpdf.text.pdf.PdfPCell) and then use setBackgroundColor to set the Cell Background Colour In Pdf generated Table in java in pdf in itext in java.





Download jars required to execute program >

Or, you may download jars from here.


Program/Example - How to Set Cell Background Colour In Pdf generated Table in java  - Using iText library - core java tutorial in Java

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
*  How to set the Cell Background Colour In Pdf generated Table Example
*  Using iText library - core java tutorial
*
*/
public class CellBackgroundColourInPdfTableExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "e:/Cell Background Colour In Pdf Table Example.pdf";
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                 PdfPTable table = new PdfPTable(2); // Create 2 columns in table
                 PdfPCell cell1 = new PdfPCell(new Paragraph(
                              "Cell 1 - GREEN Background"));

                 /* Set Background colour */
                 cell1.setBackgroundColor(BaseColor.GREEN);
                 PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
                 table.addCell(cell1);
                 table.addCell(cell2);
                 document.add(table);
                 document.close();
                 fos.close();
                 System.out.println("PDF created in >> " + pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/* Output of above program
PDF created in >> e:/Cell Background Colour In Pdf Table Example.pdf
*/


PDF formed after executing above java program(How to Set Cell Background Colour In Pdf Table in java) will look like this >

Summary -
So in this core java tutorial we learned how to Set Cell Background Colour In Pdf generated Table using iText library - core java tutorial in Java with program and examples. We created PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then created cells ( com.itextpdf.text.pdf.PdfPCell) and then used setBackgroundColor to set the Cell Background Colour In Pdf generated Table in java in pdf in itext in java.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java


Creating first pdf and setting attributes using itext in java - iText tutorial >

Create the first Pdf Example Using iText library in java - iText java tutorial example

Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java




eEdit
Must read for you :