如何在 iText 的 PDFPTable 中显示动态列表
How to show dynamic List in PDFTable in iText
我需要你的帮助来显示从数据库中检索到的收入列表 PDFTable
在 iText 中。收入将有两列:earnings_description 和 earnings_amount,它们在单独的 class 中定义,称为收入。 java 检索它们的代码是:
List<Earnings> listEarnings = new ArrayList<Earnings>();
try{
Connection con = Database.getConnection();
PreparedStatement ps = con.prepareStatement("select * from Earnings");
List<Earnings> listEarnings = new ArrayList<Earnings>();
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Earnings e = new Earnings();
e.setEarningsDescription(rs.getString("Earning_description"));
e.setEarningsAmount(rs.getString("Earning_amount"));
listEarnings.add(e);
}
catch(Exception e)){
System.out.println("Error");
}
但是我试图创建一个 table 来将值放在 header 下,但我需要一些帮助。下面是代码:
PdfPTable table = new PdfPTable(2);
Font font = new Font(FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
PdfPCell c1 = new PdfPCell(new Phrase("Earning Description"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Earning Amount"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
现在我需要你的助手在每个 header 下添加值。
首先,您没有 post earnings_description
& earnings_amount
,
的 get 方法
因此假设它们是 getEarningsDescription()
和 getEarningsAmount()
,但根据您的收入调整它们 class :
PdfPTable table = new PdfPTable(2);
Font font = new Font(FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
PdfPCell c1 = new PdfPCell(new Phrase("Earning Description"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Earning Amount"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
// Here's how you adding the values
for(int i=0;i<listEarnings.size();i++){
String temp1 = listEarnings.get(i).getEarningsDescription();
String temp2 =listEarnings.get(i).getEarningsAmount();
if(temp.equalsIgnoreCase("")){
temp="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
}
if(temp2.equalsIgnoreCase("")){
temp2="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
}
table.addCell( temp1 ); // rows for first column
table.addCell(temp2); // rows for seconds column
}
注意:不要忘记相应地调整那些 getEarningsDescription()
和 getEarningsAmount()
我需要你的帮助来显示从数据库中检索到的收入列表 PDFTable
在 iText 中。收入将有两列:earnings_description 和 earnings_amount,它们在单独的 class 中定义,称为收入。 java 检索它们的代码是:
List<Earnings> listEarnings = new ArrayList<Earnings>();
try{
Connection con = Database.getConnection();
PreparedStatement ps = con.prepareStatement("select * from Earnings");
List<Earnings> listEarnings = new ArrayList<Earnings>();
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Earnings e = new Earnings();
e.setEarningsDescription(rs.getString("Earning_description"));
e.setEarningsAmount(rs.getString("Earning_amount"));
listEarnings.add(e);
}
catch(Exception e)){
System.out.println("Error");
}
但是我试图创建一个 table 来将值放在 header 下,但我需要一些帮助。下面是代码:
PdfPTable table = new PdfPTable(2);
Font font = new Font(FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
PdfPCell c1 = new PdfPCell(new Phrase("Earning Description"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Earning Amount"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
现在我需要你的助手在每个 header 下添加值。
首先,您没有 post earnings_description
& earnings_amount
,
因此假设它们是 getEarningsDescription()
和 getEarningsAmount()
,但根据您的收入调整它们 class :
PdfPTable table = new PdfPTable(2);
Font font = new Font(FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
PdfPCell c1 = new PdfPCell(new Phrase("Earning Description"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Earning Amount"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
// Here's how you adding the values
for(int i=0;i<listEarnings.size();i++){
String temp1 = listEarnings.get(i).getEarningsDescription();
String temp2 =listEarnings.get(i).getEarningsAmount();
if(temp.equalsIgnoreCase("")){
temp="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
}
if(temp2.equalsIgnoreCase("")){
temp2="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
}
table.addCell( temp1 ); // rows for first column
table.addCell(temp2); // rows for seconds column
}
注意:不要忘记相应地调整那些 getEarningsDescription()
和 getEarningsAmount()