Java Set Background To Table Example

Chapter: iText Last Updated: 22-07-2017 14:25:56 UTC

Program:

            /* ............... START ............... */
                
import com.itextpdf.kernel.color.Color; 
import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 
import com.itextpdf.layout.Document; 
import com.itextpdf.layout.border.Border; 
import com.itextpdf.layout.element.Cell; 
import com.itextpdf.layout.element.Table; 
import com.itextpdf.layout.property.TextAlignment; 

public class JavaBackgroundTable {
	
	public static void main(String args[]) throws Exception {
	      String file = "C:/EXAMPLES/itextExamples/backgroundToTable.pdf"; 

	      //Creating a PdfDocument object 
	      PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));     

	      //Creating a Document object 
	      Document doc = new Document(pdfDoc);        

	      //Creating a table 
	      Table table = new Table(2);     

	      //Adding row 1 to the table          
	      Cell c1 = new Cell(); 
	      
	      c1.add("Name"); 
	      c1.setBackgroundColor(Color.DARK_GRAY); 
	      c1.setBorder(Border.NO_BORDER); 
	      c1.setTextAlignment(TextAlignment.CENTER); 
	      table.addCell(c1);          

	      Cell c2 = new Cell(); 
	      c2.add("Raju"); 
	      c2.setBackgroundColor(Color.GRAY); 
	      c2.setBorder(Border.NO_BORDER); 
	      c2.setTextAlignment(TextAlignment.CENTER); 
	      table.addCell(c2); 

	      //Adding row 2 to the table          
	      Cell c3 = new Cell(); 
	      
	      c3.add("Id"); 
	      c3.setBackgroundColor(Color.WHITE); 
	      c3.setBorder(Border.NO_BORDER); 
	      c3.setTextAlignment(TextAlignment.CENTER); 
	      table.addCell(c3);          

	      Cell c4 = new Cell(); 
	      c4.add("001");
	      c4.setBackgroundColor(Color.WHITE); 
	      c4.setBorder(Border.NO_BORDER); 
	      c4.setTextAlignment(TextAlignment.CENTER); 
	      table.addCell(c4);          

	      //Adding row 3 to the table 
	      Cell c5 = new Cell(); 
	      
	      c5.add("Designation"); 
	      c5.setBackgroundColor(Color.DARK_GRAY); 
	      c5.setBorder(Border.NO_BORDER); 
	      c5.setTextAlignment(TextAlignment.CENTER); 
	      table.addCell(c5); 

	      Cell c6 = new Cell(); 
	      c6.add("Programmer"); 
	      c6.setBackgroundColor(Color.GRAY); 
	      c6.setBorder(Border.NO_BORDER); 
	      c6.setTextAlignment(TextAlignment.CENTER); 
	      table.addCell(c6);          

	      //Adding Table to document  
	      doc.add(table);  

	      //Closing the document 
	      doc.close();  
	      System.out.println("Background added successfully.."); 
	   } 

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

Output


Java Set Background To Table Example

Tags

Set Background To Table Example, Java, Apache POI

Similar Programs Chapter Last Updated
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
Create Tables 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