Monday 14, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Monday, 5 June 2017

iText 5 - display background color when using rowspan

To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository

Maven Dependency

2.<dependency>
3.    <groupid>com.itextpdf</groupid>
4.    <artifactid>itextpdf</artifactid>
5.    <version>5.5.11</version>
6.</dependency>

SimpleTable10.java
01.package com.javatutorialcorner.itextpdf;
02. 
03.import com.itextpdf.text.BaseColor;
04.import com.itextpdf.text.Document;
05.import com.itextpdf.text.DocumentException;
06.import com.itextpdf.text.Phrase;
07.import com.itextpdf.text.pdf.PdfPCell;
08.import com.itextpdf.text.pdf.PdfPTable;
09.import com.itextpdf.text.pdf.PdfWriter;
10.  
11. 
12.import java.io.File;
13.import java.io.FileOutputStream;
14.import java.io.IOException;
15. 
16.public class SimpleTable10 {
17.    public static final String DEST = "C:/JTC/SimpleTable10.pdf";
18.  
19.    public static void main(String[] args) throws IOException,
20.            DocumentException {
21.        File file = new File(DEST);
22.        file.getParentFile().mkdirs();
23.        new SimpleTable10().createPdf(DEST);
24.    }
25.    public void createPdf(String dest) throws IOException, DocumentException {
26.        Document document = new Document();
27.        PdfWriter.getInstance(document, new FileOutputStream(dest));
28.        document.open();
29.        PdfPTable table = new PdfPTable(5);
30.        PdfPCell sn = new PdfPCell(new Phrase("S/N"));
31.        sn.setRowspan(2);
32.        sn.setBackgroundColor(BaseColor.YELLOW);
33.        table.addCell(sn);
34.        PdfPCell name = new PdfPCell(new Phrase("Name"));
35.        name.setColspan(3);
36.        name.setBackgroundColor(BaseColor.CYAN);
37.        table.addCell(name);
38.        PdfPCell age = new PdfPCell(new Phrase("Age"));
39.        age.setRowspan(2);
40.        age.setBackgroundColor(BaseColor.GRAY);
41.        table.addCell(age);
42.        PdfPCell surname = new PdfPCell(new Phrase("SURNAME"));
43.        surname.setBackgroundColor(BaseColor.BLUE);
44.        table.addCell(surname);
45.        PdfPCell firstname = new PdfPCell(new Phrase("FIRST NAME"));
46.        firstname.setBackgroundColor(BaseColor.RED);
47.        table.addCell(firstname);
48.        PdfPCell middlename = new PdfPCell(new Phrase("MIDDLE NAME"));
49.        middlename.setBackgroundColor(BaseColor.GREEN);
50.        table.addCell(middlename);
51.        PdfPCell f1 = new PdfPCell(new Phrase("1"));
52.        f1.setBackgroundColor(BaseColor.PINK);
53.        table.addCell(f1);
54.        PdfPCell f2 = new PdfPCell(new Phrase("James"));
55.        f2.setBackgroundColor(BaseColor.MAGENTA);
56.        table.addCell(f2);
57.        PdfPCell f3 = new PdfPCell(new Phrase("Fish"));
58.        f3.setBackgroundColor(BaseColor.ORANGE);
59.        table.addCell(f3);
60.        PdfPCell f4 = new PdfPCell(new Phrase("Stone"));
61.        f4.setBackgroundColor(BaseColor.DARK_GRAY);
62.        table.addCell(f4);
63.        PdfPCell f5 = new PdfPCell(new Phrase("17"));
64.        f5.setBackgroundColor(BaseColor.LIGHT_GRAY);
65.        table.addCell(f5);
66.        document.add(table);
67.        document.close();
68.    }
69.  
70.}

Output
Reference : iText Website

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: iText 5 - display background color when using rowspan Rating: 5 Reviewed By: eHowToNow