0

I am using iText for generating PDF file from a table data.Actually I am able to generate the PDF file with single table. But I don’t know how to generate second table in that same PDF file. When I tried to do it, second table is coming as part of the first table.

String date = DateUtils.getTimestamp().toString();
date = date.replace(" ", "_");
date = date.replace(":", "_");
date = "d:/CompareDeal_" + date + ".pdf" ;

Document my_pdf_report = new Document();
PdfWriter.getInstance(my_pdf_report, new FileOutputStream(date));
my_pdf_report.open();  

PdfPTable my_report_table = new PdfPTable(5); // CREATING TABLE rt
PdfPCell table_cell;
String blank_column = "";
table_cell=new PdfPCell(new Phrase(blank_column));
my_report_table.addCell(table_cell); // rt row 1 column 1

String deal1_plan="Deal1:Plan";
table_cell=new PdfPCell(new Phrase(deal1_plan));
my_report_table.addCell(table_cell); // rt row 1 column 2

String deal1_actual="Deal1:Actual";
table_cell=new PdfPCell(new Phrase(deal1_actual));
my_report_table.addCell(table_cell); // rt row 1 column 3

String deal2_plan="Deal2:Plan";
table_cell=new PdfPCell(new Phrase(deal2_plan));
my_report_table.addCell(table_cell); // rt row 1 column 4

String deal2_actual="Deal2:Actual";
table_cell=new PdfPCell(new Phrase(deal2_actual));
my_report_table.addCell(table_cell);  // rt row 1 column 5

table_cell=new PdfPCell(new Phrase("Revenue(M$)"));
my_report_table.addCell(table_cell); // rt row 2 column 1

String planRevenue1 = StringUtils.convertToString(StringUtils.formatForNull(model.getPlanRevenue1(), "-", ""));
table_cell=new PdfPCell(new Phrase(planRevenue1));
my_report_table.addCell(table_cell); // rt row 2 column 2

String actualRevenue1 = StringUtils.convertToString(StringUtils.formatForNull(model.getActualRevenue1(), "-", ""));
table_cell=new PdfPCell(new Phrase(actualRevenue1));
my_report_table.addCell(table_cell); // rt row 2 column 3

String planRevenue2 = StringUtils.convertToString(StringUtils.formatForNull(model.getPlanRevenue2(), "-", ""));
table_cell=new PdfPCell(new Phrase(planRevenue2));
my_report_table.addCell(table_cell); // rt row 2 column 4

String actualRevenue2 = StringUtils.convertToString(StringUtils.formatForNull(model.getActualRevenue2(), "-", ""));
table_cell=new PdfPCell(new Phrase(actualRevenue2));
my_report_table.addCell(table_cell); // rt row 2 column 5

table_cell=new PdfPCell(new Phrase("PPM"));
my_report_table.addCell(table_cell); // rt row 3 column 1

String planPpm1 = StringUtils.convertToString(StringUtils.convertToUSFormat(model.getPlanPpm1(),StringUtils.DEFAULT_EMPTY_STRING),StringUtils.DEFAULT_EMPTY_STRING);
table_cell=new PdfPCell(new Phrase(planPpm1));
my_report_table.addCell(table_cell); // rt row 3 column 2

String actualPpm1 = StringUtils.convertToString(StringUtils.convertToUSFormat(model.getActualPpm1(),StringUtils.DEFAULT_EMPTY_STRING),StringUtils.DEFAULT_EMPTY_STRING);
table_cell=new PdfPCell(new Phrase(actualPpm1));
my_report_table.addCell(table_cell); // rt row 3 column 3

String planPpm2 = StringUtils.convertToString(StringUtils.convertToUSFormat(model.getPlanPpm2(),StringUtils.DEFAULT_EMPTY_STRING),StringUtils.DEFAULT_EMPTY_STRING);
table_cell=new PdfPCell(new Phrase(planPpm2));
my_report_table.addCell(table_cell); // rt row 3 column 4

String actualPpm2 = StringUtils.convertToString(StringUtils.convertToUSFormat(model.getActualPpm2(),StringUtils.DEFAULT_EMPTY_STRING),StringUtils.DEFAULT_EMPTY_STRING);
table_cell=new PdfPCell(new Phrase(actualPpm2));
my_report_table.addCell(table_cell);// rt row 3 column 5

table_cell=new PdfPCell(new Phrase("Rev per FTE($)"));
my_report_table.addCell(table_cell); // rt row 4 column 1
String planFte1 = StringUtils.convertToString(StringUtils.formatForNull(model.getPlanFte1(), "-", ""));
table_cell=new PdfPCell(new Phrase(planFte1));
my_report_table.addCell(table_cell); // rt row 4 column 2

String actualFte1 = StringUtils.convertToString(StringUtils.formatForNull(model.getActualFte1(), "-", ""));
table_cell=new PdfPCell(new Phrase(actualFte1));
my_report_table.addCell(table_cell); // rt row 4 column 3

String planFte2 = StringUtils.convertToString(StringUtils.formatForNull(model.getPlanFte2(), "-", ""));
table_cell=new PdfPCell(new Phrase(planFte2));
my_report_table.addCell(table_cell); // rt row 4 column 4

String actualFte2 = StringUtils.convertToString(StringUtils.formatForNull(model.getActualFte2(), "-", ""));
table_cell=new PdfPCell(new Phrase(actualFte2));
my_report_table.addCell(table_cell); // rt row 4 column 5

my_pdf_report.add(my_report_table); // ADDING TABLE rt
PdfPTable my_report_table1 = new PdfPTable(5); // CREATING TABLE t1
PdfPCell table_cell1;
String blank_column1 = "";
table_cell1=new PdfPCell(new Phrase(blank_column1));
my_report_table1.addCell(table_cell1); // t2 row 1 column 1

String deal1_plan1="Deal1:Plan";
table_cell1=new PdfPCell(new Phrase(deal1_plan1));
my_report_table1.addCell(table_cell1); // t2 row 1 column 2

String deal1_actual1="Deal1:Actual";
table_cell1=new PdfPCell(new Phrase(deal1_actual1));
my_report_table1.addCell(table_cell1); // t2 row 1 column 3

String deal2_plan1="Deal2:Plan";
table_cell1=new PdfPCell(new Phrase(deal2_plan1));
my_report_table1.addCell(table_cell1); // t2 row 1 column 4

String deal2_actual1="Deal2:Actual";
table_cell1=new PdfPCell(new Phrase(deal2_actual1));
my_report_table1.addCell(table_cell1); // t2 row 1 column 5

my_pdf_report.add(my_report_table1); // ADDING TABLE r1
my_pdf_report.close();

So far this is what I have done.

Is there anyway we can add styles to the table generated like Font size and font color?

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
Mahesh Narayanan
  • 123
  • 5
  • 21

1 Answers1

1

At first I didn't understand your question. I updated your code with some comments so that I could have a better understanding of where you create, populate and add the table. I could clearly see that you add two different tables and I didn't understand why you'd claim that the second table is coming as part of the first table. You are adding two different tables.

However, maybe your eyes are fooling you. Maybe you only see one table because there is no extra space between the table I marked as rt and the table I marked as t1. This can easily be fixed like this:

PdfPTable my_report_table = new PdfPTable(5);
my_report_table.setSpacingAfter(10);

This will add some extra space after the table I marked with rt, more specifically, the distance between rt and t1 will be 10 user units (which is by default about 10 pt).

Another option is to add some extra spacing before the second table:

PdfPTable my_report_table1 = new PdfPTable(5);
my_report_table1.setSpacingBefore(10);

This adds 10 user units of space before the second table.

As for your other doubt: I also have a doubt. I doubt that you have consulted the free ebook The Best iText Questions on StackOverflow. In this book, you'll find a chapter entitled "Tables" and another chapter entitled "Table events". In these chapters, you'll find the answer to questions such as:

Your second question is too broad for StackOverflow. Please consult the documentation and it you don't find the answer there, post a more specific question.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks Bruno. Space did the job for me. Is there anyway i can add a paragraph between those 2 tables ? – Mahesh Narayanan May 29 '15 at 09:45
  • Of course you can add a paragraph between those 2 tables! Why don't you try this before asking the question. Also: please read the [documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html). I have spent *many hours* writing the answers bundled in that book. It would be a shame if you said *I am not interested in learning more about the tool I am using.* – Bruno Lowagie May 29 '15 at 09:48