简体   繁体   中英

How to add conditional data into itext table cells

I'm trying to input data from that I've parsed from a JSON file into a table. I'm using Java on Eclipse with itext external jar (since I'm trying to output a PDF). The data to be input is conditional.

                  table.addCell("Annual Leave*"); 
                  if(ltype=="annual"){
                  table.addCell(from1);
                  table.addCell(to1);
                  table.addCell(n);
                  }
                  else{
                     table.addCell("");
                      table.addCell("");
                      table.addCell("");
                  }

                  table.addCell("Sick Leave(certified)**");
                  if(ltype=="sick_leave_cert"){
                      table.addCell(from1);
                      table.addCell(to1);
                      table.addCell(n);
                  }

This is what I've tried to do. However, my end PDF document has an empty table.

The issue is not really itext related, it is about Java String handling: You compare Strings like this:

if(ltype=="annual")
...
if(ltype=="sick_leave_cert")

In Java the String type is not a primitive type. Thus, String comparison using == checks whether both sides resolve top the identical String object, not whether the Strings on both sides represent the same characters in sequence.

To check whether two Strings represent the same character sequences use the equals method instead:

if("annual".equals(ltype))
...
if("sick_leave_cert".equals(ltype))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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