0

How can i prevent splitting a row? Is there in iText some method on the table or cell where can i set that i dont want it? I need to keep all my rows unsplittable. I dont have every row as a new table, all data i have in one table.

I tried several things, for example this: table.setSplitRows(false); but nothing helped me.

enter image description here My WHOLE code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class Pdf5form {

public static final String DEST = "c:/radek-folder/pdf5form.pdf";
protected int horizontalAlignmentLeft = Element.ALIGN_LEFT;
protected int horizontalAlignmentCenter = Element.ALIGN_CENTER;
protected int verticalAlignmentMiddle = Element.ALIGN_MIDDLE;
protected int verticalAlignmentTop = Element.ALIGN_TOP;
protected String fontTypeRegular = "c:/radek-folder/font_sitebook.ttf";
protected String fontTypeBold = "c:/radek-folder/font_sitebook_bold.ttf";
protected float fontSizeRegular = 10f;
protected float fontSizeLarger = 14f;
protected float fontSizeLarge = 16f;
float tableWidth;
PdfPCell cell;

public static void main(String[] args) throws IOException, DocumentException 
{
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new Pdf5form().createPdf(DEST);
    System.out.println("done");
}

public void createPdf(String dest) throws IOException, DocumentException {
    float tiniestColumnWidth = 40;
    float tinyColumnWidth = 50;
    float smallColumnWidth = 55;
    float mediumColumnWidth = 120;
    float largeColumnWidth = 170; // celé číslo jinak háže čáry navíc
    float[] columns = { largeColumnWidth, tiniestColumnWidth, tinyColumnWidth,
            mediumColumnWidth, smallColumnWidth, smallColumnWidth, tinyColumnWidth,
            tinyColumnWidth, tinyColumnWidth, tinyColumnWidth, tinyColumnWidth,
            tinyColumnWidth };
    int numberOfColumns = columns.length;
    Document document = new Document(PageSize.A4.rotate(), 36, 36, 36, 64);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    writer.setPageEvent(new PageCounter());

    document.open();

    PdfPTable table = new PdfPTable(numberOfColumns);
    table.getDefaultCell().setPadding(0);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.setTotalWidth(columns); // 500
    table.setLockedWidth(true);
    cell = createAndReturnCell("Přenosové trasy GTS", horizontalAlignmentCenter,
            verticalAlignmentMiddle, numberOfColumns, 1, fontTypeBold, fontSizeLarge,
            BaseColor.BLACK);
    cell.setPaddingTop(10);
    cell.setPaddingBottom(15);
    table.addCell(cell);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Trasa [-]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Pásmo [GHz]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Kap a konf [Mbit/s]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Typ zařízení Index [-]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Zeměpis.délka WGS84[°-'-\"]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Zeměpis.šířka WGS84[°-'-\"]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Nadm.výška [m]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Výška ant.[m]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Průměr ant./zisk [m]/[dB]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Útlum vlnovod [dB]", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Výkon vysílač dBm", 1, 1, fontTypeBold, fontSizeRegular);
    addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
            "Délka trasy [m]", 1, 1, fontTypeBold, fontSizeRegular);

    for (int i = 0; i < 500; i++) {
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "CZ5142", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "38", 1, 3, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "1+0", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "FibeAir 1500p 38GHz", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "18?16'52''", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "49?49'52''", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "220", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "50", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "0.30", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "0", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "10", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "1431.55", 1, 3, fontTypeRegular, fontSizeRegular);

        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "Ostrava Hornopolní 3308/40", 1, 1, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "100M", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "N/A", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "18?16'11''", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "49?50'30''", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "211", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "20", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "0.30", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "0", 1, 2, fontTypeRegular, fontSizeRegular);
        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "10", 1, 2, fontTypeRegular, fontSizeRegular);

        addCellToTableCzech(table, horizontalAlignmentCenter, verticalAlignmentMiddle,
                "CZ5305", 1, 1, fontTypeRegular, fontSizeRegular);
    }

    document.add(table);
    document.close();
}

private static void addCellToTableCzech(PdfPTable datatable, int horizontalAlignment,
        int verticalAlignment, String value, int colspan, int rowspan,
        String fontType, float fontSize) {
    BaseFont base = null;
    try {
        base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Font font = new Font(base, fontSize);
    PdfPCell cell = new PdfPCell(new Phrase(value, font));
    cell.setColspan(colspan);
    cell.setRowspan(rowspan);
    cell.setHorizontalAlignment(horizontalAlignment);
    cell.setVerticalAlignment(verticalAlignment);
    cell.setBorder(PdfPCell.BOX);
    datatable.addCell(cell);
}

private static PdfPCell createAndReturnCell(String value, int horizontalAlignment,
        int verticalAlignment, int colspan, int rowspan, String fontType,
        float fontSize, BaseColor color) {
    BaseFont base = null;
    try {
        base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Font font = new Font(base, fontSize);
    font.setColor(color);
    PdfPCell cell = new PdfPCell(new Phrase(value, font));
    cell.setColspan(colspan);
    cell.setRowspan(rowspan);
    cell.setHorizontalAlignment(horizontalAlignment);
    cell.setVerticalAlignment(verticalAlignment);
    cell.setBorder(PdfPCell.NO_BORDER);
    return cell;
}

class PageCounter extends PdfPageEventHelper {

    PdfTemplate t;
    Image total;

    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        t = writer.getDirectContent().createTemplate(30, 16);
        try {
            total = Image.getInstance(t);
            total.setRole(PdfName.ARTIFACT);
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfPTable table = new PdfPTable(3);
        try {
            table.setWidths(new int[] { 24, 24, 2 });
            table.setTotalWidth(tableWidth);

            DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy / HH:mm");
            PdfPCell cell1 = new PdfPCell(new Phrase(dateFormat.format(new Date())));
            cell1.setVerticalAlignment(Element.ALIGN_TOP);
            cell1.setBorder(Rectangle.NO_BORDER);
            cell1.setFixedHeight(30);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(
                    new Phrase(String.format("Page %d of", writer.getPageNumber())));
            cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell2.setBorder(Rectangle.NO_BORDER);
            cell2.setFixedHeight(30);
            cell2.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(total);
            cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell3.setBorder(Rectangle.NO_BORDER);
            cell3.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell3);

            PdfContentByte canvas = writer.getDirectContent();
            canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
            table.writeSelectedRows(0, -1, 210, 40, canvas);
            canvas.endMarkedContentSequence();
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        ColumnText.showTextAligned(t, Element.ALIGN_LEFT,
                new Phrase(String.valueOf(writer.getPageNumber())), 2, 2, 0);
    }
}
}
user7968180
  • 145
  • 1
  • 15
  • Possible duplicate. Check this answer >> http://stackoverflow.com/questions/16619914/itext-avoid-last-row-not-to-cut-tables-on-page-split-to-next-page – Joe Hackerberg May 17 '17 at 15:33
  • Maybe the second answer of the link above could help you. – Joe Hackerberg May 17 '17 at 15:35
  • Unfortunatelly, none of those options from the second answer works: NO - table.setKeepRowsTogather(true); NO - table.setHeaderRows(1) And the first answer solution is in very manually way, i would like to have something more elegant, i hope there must be something better than setting manually on the page coordinates for THIS problem. – user7968180 May 17 '17 at 15:45
  • That one from Bruno does not help me too... So that question is not duplicate. – user7968180 May 18 '17 at 07:43
  • I have noticed that the second answer I recommended you yesterday has a syntactic error in the method `table.setKeepRowsTogather(true);`. Obviously it's not "...Togather". I hope you tried in the correct way: `table.setKeepRowsTogether(true);` - For the lastest versions it is: `setKeepTogether(true)` – Joe Hackerberg May 18 '17 at 07:58
  • Well... I cannot help you anymore with this... I hope this links helps you: http://developers.itextpdf.com/question/how-prevent-splitting-table – Joe Hackerberg May 18 '17 at 08:05
  • table.setKeepRowsTogether does not exist any more :-) i used table.setKeepTogether instead (it is in your example) and it does not work, because it concerns whole table, now rows. – user7968180 May 18 '17 at 08:05
  • Please, can you edit your question adding the section of your code where you trying to fix this? Its quite complex to help without seeing the code – Joe Hackerberg May 18 '17 at 08:10
  • done, my whole code is attached – user7968180 May 18 '17 at 08:14
  • Probably the problem is that the "split row" actually is a single row only thanks to a row span property; I would not be surprised if some or all of the solutions in the duplicate candidate answer work with real atomic rows. Thus, you might fare better with subtables instead of row spans. – mkl May 18 '17 at 08:38

1 Answers1

2

From the link I posted in the comments, http://developers.itextpdf.com/question/how-prevent-splitting-table, you can keep your table structure intact by nesting your table into another:

PdfPTable nesting = new PdfPTable(1);
PdfPCell cell = new PdfPCell(table);
cell.setBorder(PdfPCell.NO_BORDER);
nesting.addCell(cell);
document.add(nesting);
Joe Hackerberg
  • 457
  • 7
  • 20
  • I hope this finally fix that – Joe Hackerberg May 18 '17 at 08:27
  • yeeees, it works!! So it makes necessary to add every row as a new table, and then care about not splitting table. So we did not solve prevention of splitting rows, but you helped me with this problem, this is more important :-) Thanks for your patient, bro :-) – user7968180 May 18 '17 at 09:02
  • Oh thanks god!! haha I'm glad I could finally help you. You're welcome :) – Joe Hackerberg May 18 '17 at 09:08