Animesh Jena Animesh Jena - 2 months ago 23
Android Question

Table Splitting Issue in iText PDF

Through my android App, I am generating a quote pdf, where the proper format of the quote has to be like below image:

enter image description here

But while developing, the item table is not spliting. Instead, the whole table is moving to the next page. Below is the result, I am getting now:

enter image description here

As you can see, the table is not splitting on the first page in case of large number of rows. Below I am posting the code of the Item List table:

// For loop for formatting and adding individual rows for the item list
for(int i = 0; i< quoteEntryFragment.alst_queryDetailList.size(); i++)
{

String str_catalogName = quoteEntryFragment.alst_queryDetailList.get(i).getStr_CatalogName();
String str_catalogDesc = quoteEntryFragment.alst_queryDetailList.get(i).getStr_CatalogDescription();
int int_UPC_Number = quoteEntryFragment.alst_queryDetailList.get(i).getInt_UPCNumber();
Paragraph paragraph_description1 = new Paragraph("",normalFontSmaller);
paragraph_description1.add(str_catalogName+"\n");
Paragraph paragraph_description2 = new Paragraph("",normalFontSmaller);
paragraph_description2.add(str_catalogDesc+"\n");
Paragraph paragraph_description21 = new Paragraph("UPC Number:", boldFontSmaller);
Paragraph paragraph_description22 = new Paragraph("", normalFontSmaller);
if(int_UPC_Number==-1)
{
paragraph_description22.add("");

}
else
{
paragraph_description22.add(int_UPC_Number+"");
}

Paragraph paragraph_description31 = new Paragraph( "\nPackage Quantity:"+quoteEntryFragment.
alst_queryDetailList.get(i).getInt_PackageQuantity()+"\n",normalFontSmaller);
Paragraph paragraph_description32 = new Paragraph( "Min Ord Qty:"+quoteEntryFragment.
alst_queryDetailList.get(i).getInt_MinOrdQty()+"\n",normalFontSmaller );
Paragraph paragraph_description41 = new Paragraph("Quote Lead Time :", boldFontSmaller);
String str_number_of_weeks = "";
if( quoteEntryFragment.alst_queryDetailList.get(i).getInt_QuoteLeadTime()==-1)
{
str_number_of_weeks = "";
}
else
{
str_number_of_weeks = quoteEntryFragment.alst_queryDetailList.get(i).getInt_QuoteLeadTime()+"WEEKS";
}
Paragraph paragraph_desLast = new Paragraph(str_number_of_weeks+"\n" +
"Made to order item. Cancellation or restocking charges will apply", normalFontSmallest);

// Creating the value in the 2nd column of every row

Paragraph combination_item = new Paragraph();
combination_item. add(paragraph_description1);
combination_item. add(paragraph_description2);
combination_item. add(paragraph_description21);
combination_item. add(paragraph_description22);
combination_item. add(paragraph_description31);
combination_item. add(paragraph_description32);
combination_item. add(paragraph_description41);
combination_item. add(paragraph_desLast);
combination_item.setKeepTogether(false);


Paragraph np = new Paragraph((i+1)+"", normalFontSmaller);
Paragraph quantity = new Paragraph(quoteEntryFragment.alst_queryDetailList.get(i).getInt_quantity()+"", normalFontSmaller);
Paragraph UoM = new Paragraph("EA", normalFontSmaller);
Paragraph NUP = new Paragraph(quoteEntryFragment.alst_queryDetailList.get(i).getDbl_price()+"",normalFontSmaller);

double dbl_itemQuantity = (double)quoteEntryFragment.alst_queryDetailList.get(i).getInt_quantity();
double dbl_itemPrice = quoteEntryFragment.alst_queryDetailList.get(i).getDbl_price();

double dbl_totalPrice = dbl_itemPrice*dbl_itemQuantity;
DecimalFormat dformat = new DecimalFormat("#.##");
String str_formatted = dformat.format(dbl_totalPrice);
totalPrice[i] =Double.parseDouble(str_formatted);

Paragraph extPrice = new Paragraph(str_formatted, normalFontSmaller);
PdfPCell cellVal = new PdfPCell(np);
cellVal.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell cellVal3 = new PdfPCell(combination_item);
cellVal3.setHorizontalAlignment(Element.ALIGN_LEFT);
cellVal3.setPaddingBottom(10.0f);
PdfPCell cellVal4 = new PdfPCell(quantity);
cellVal4.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell cellVal5 = new PdfPCell(UoM);
cellVal5.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell cellVal6 = new PdfPCell(NUP);
cellVal6.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell cellVal7 = new PdfPCell(extPrice);
cellVal7.setHorizontalAlignment(Element.ALIGN_CENTER);


itemList.addCell(cellVal).setBorder(Rectangle.NO_BORDER);
itemList.addCell(cellVal3).setBorder(Rectangle.NO_BORDER);
itemList.addCell(cellVal4).setBorder(Rectangle.NO_BORDER);
itemList.addCell(cellVal5).setBorder(Rectangle.NO_BORDER);
itemList.addCell(cellVal6).setBorder(Rectangle.NO_BORDER);
itemList.addCell(cellVal7).setBorder(Rectangle.NO_BORDER);
itemList.setSplitLate(false);
}

double sumValue = 0;

for(int i =0; i< totalPrice.length; i++)
{
sumValue = sumValue+ totalPrice[i];
}
DecimalFormat dformat = new DecimalFormat("#.##");
String str_total_Price_formatted = dformat.format(sumValue);



PdfPCell itemlistCell = new PdfPCell(itemList);
itemlistCell.setBorder(Rectangle.NO_BORDER);
PdfPCell itemTotalCell = new PdfPCell(itemTotal);
itemTotalCell.setBorder(Rectangle.NO_BORDER);
itemInfo.addCell(itemlistCell);//here
itemInfo.addCell(itemTotalCell);


Sorry for the messed up code. I am unable to split the table items in the first page. However, the table is starting from the 2nd page and in case of more number of items, its successfully able to split between 2nd and 3rd page. Need your help. Thanks in advance.

Answer Source

The problem was solved by adding the following line:

itemInfo.setSplitLate(false);

Such a line was present in the original code, but only for the inner table, not for the outer table.