简体   繁体   English

使用iText在Java中将多个表转换为PDF报告

[英]Multiple Tables to PDF Report in Java using iText

I am using iText for generating PDF file from a table data.Actually I am able to generate the PDF file with single table. 我正在使用iText从表数据生成PDF文件。实际上,我能够使用单个表生成PDF文件。 But I don't know how to generate second table in that same PDF file. 但是我不知道如何在同一PDF文件中生成第二张表。 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? 无论如何,我们可以在生成的表格中添加字体大小和字体颜色等样式吗?

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 . 也许您只看到一个表,因为在标记为rt的表和标记为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). 在我用rt标记的表之后,这将增加一些额外的空间,更具体地说, rtt1之间的距离将是10个用户单位(默认情况下约为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. 这将在第二个表之前增加10个用户单位的空间。

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 . 我怀疑您是否查阅过免费电子书《 StackOverflow上的最佳iText问题》 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. 对于StackOverflow,您的第二个问题过于笼统。 Please consult the documentation and it you don't find the answer there, post a more specific question. 请查阅文档,如果找不到答案,请发布更具体的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM