How to keep rows together in ITETXPDF 5

-1

I am putting together a PDF for a quota list and it works as follows, if the amount of installments is equal to 36, those quotas are shown on the first sheet plus a description of totals below row 36. But if is 37 or more, those rows should go on the next page plus the description of totals. But if they cover more than two or more sheets the description of totals should never be left alone on a sheet.

What I did was read the list and use a counter internally so that when it is equal to 36 setKeepTogether (true) , and go all on one sheet. But the second part I still have not been able to develop it. Any suggestions to achieve this ?. Thanks

    
asked by Rahmen 27.08.2018 в 22:49
source

1 answer

1

See the iText website example:

/**
 * Este ejemplo fue escrito por Bruno Lowagie en respuesta a las siguientes preguntas:
 * http://stackoverflow.com/questions/28503491/large-table-in-table-cell-invoke-page-break
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class NestedTables2 {
    public static final String DEST = "results/tables/nested_tables2.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables2().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setSplitLate(false);
        table.setWidths(new int[]{1, 15});
        for (int i = 1; i <= 20; i++) {
            table.addCell(String.valueOf(i));
            table.addCell("¡No es inteligente usar iText 2.1.7!");
        }
        PdfPTable innertable = new PdfPTable(2);
        innertable.setWidths(new int[]{1, 15});
        for (int i = 0; i < 90; i++) {
            innertable.addCell(String.valueOf(i + 1));
            innertable.addCell("¡Actualiza si eres un desarrollador profesional!");
        }
        table.addCell("21");
        table.addCell(innertable);
        for (int i = 22; i <= 40; i++) {
            table.addCell(String.valueOf(i));
            table.addCell("¡No es inteligente usar iText 2.1.7!");
        }
        document.add(table);
        document.close();
    }
}

This is creating nested tables, since setKeepTogether(True) does not work in the cells, but in the table. When you reach 36, you can create the external table.

    
answered by 27.09.2018 в 22:02