简体   繁体   中英

itextpdf table number of columns

I have a list of 100 records and i want to make a table in pdf with 3 columns. My problem is once the pdf file has been made it's 33 rows with 3 columns so it's 99 records not 100. How can i show the other one record in pdf table?

here is my code:

Document document = new Document(PageSize.A4, -60f, -60f, 18f, 5f);

PdfPTable table = new PdfPTable(3);
PdfPCell cell;

if (filterPins.size() > 0) {
    for (int i = 0; i < filterPins.size(); i++) {
        Bitmap bmp = getRecyclerViewScreenshot(pinList, i);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        Image image = Image.getInstance(stream.toByteArray());
        cell = new PdfPCell(image, true);
        cell.setPadding(0f);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
    }

    File myDirectory = new File(rootView.getContext().getFilesDir(), String.valueOf(factorNo));
    if (!myDirectory.exists()) {
        myDirectory.mkdirs();
    }
    File pdfFile = new File(myDirectory, fileName);

    PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
    document.open();
    document.add(table);
    document.close(); 
}

filterPins is my arrayList containing 100 records.

How can i show the other one record in pdf table?

First of all that also is a question you have to ask yourself. Do you want the 100th entry fill only a single of the three cells of a row? If yes, which one? Or should it span the whole row? Or would you prefer yet another construct?

Let's assume, though, you simply want the 100th entry to be the first cell in the 34th row and you also want the other cells in that row to be empty. In that case there is a PdfPTable utility method that fills the current row with empty cells (copies of the default cell):

table.completeRow();

if(yourlist.size() % 3 == 0){

int totalRows = yourlist.size() / 3;

create your pdf.

for example list.size() = 99 then you create 33 rows each row have 3 column.

}

else {

int totalRows = yourlist.size() / 3;

for example if yourlist.size() = 100.

then first create 33 row each row have 3 column.

remaining 1 item add in new list after created 33 rows.

remaining 1 or 2 column add in new row.

Note : in any case you have remaining only 1 or 2.

}

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