Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Celll rowspan exceeding total table rows not rendering #1025

Closed
ReinhardJvREx opened this issue Jan 17, 2024 · 7 comments
Closed

Celll rowspan exceeding total table rows not rendering #1025

ReinhardJvREx opened this issue Jan 17, 2024 · 7 comments
Labels

Comments

@ReinhardJvREx
Copy link

Describe the bug
Cells are not rendered if rowspan exceeds available rows in table?

I am trying to add cells to a table that are evenly spaced vertically using rowspan, but I can not seem to get the following to work.
The following works in 1.3.28 but since I have upgraded to version 1.3.36 it no longer renders the cells in the table.

To Reproduce (Kotlin)

val width = document.pageSize.width - 20f
val table = PdfPTable(floatArrayOf(33.3F,33.3F,33.3F))
table.totalWidth = width
table.isLockedWidth = true
table.headerRows = 1

var cell = PdfPCell()
cell.addElement(Phrase("ITEM NO.",bodyFont))
table.addCell(cell)
cell = PdfPCell()
cell.addElement(Phrase("PART",bodyFont))
table.addCell(cell)
cell = PdfPCell()
cell.addElement(Phrase("PROCESS",bodyFont))
table.addCell(cell)

cell = PdfPCell()
cell.rowspan = 6
cell.addElement(Phrase("item 1", bodyFont))
table.addCell(cell)

cell = PdfPCell()
cell.rowspan = 3
cell.minimumHeight = 30f
cell.addElement(Phrase("part 1", bodyFont))
table.addCell(cell)

cell = PdfPCell()
cell.rowspan = 2
cell.minimumHeight = 20f
cell.addElement(Phrase("process 1", bodyFont))
table.addCell(cell)

cell = PdfPCell()
cell.rowspan = 2
cell.minimumHeight = 20f
cell.addElement(Phrase("process 2", bodyFont))
table.addCell(cell)

cell = PdfPCell()
cell.rowspan = 3
cell.minimumHeight = 30f
cell.addElement(Phrase("part 2", bodyFont))
table.addCell(cell)

cell = PdfPCell()
cell.rowspan = 2
cell.minimumHeight = 20f
cell.addElement(Phrase("process 3", bodyFont))
table.addCell(cell)

Current behavior (1.3.36)
Screenshot from 2024-01-17 12-57-19

Expected behavior (1.3.28)
Screenshot from 2024-01-17 13-27-22

@andreasrosdal
Copy link
Contributor

These look related: #693 #865 #1019

@fellmann Can you please assist in solving this?

@andreasrosdal
Copy link
Contributor

@ReinhardJvREx Can you please provide a full test-case to reproduce this bug, preferably in Java?

@ReinhardJvREx
Copy link
Author

ReinhardJvREx commented Jan 17, 2024

Java Test Case

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfStream;
import com.lowagie.text.pdf.PdfWriter;

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

public class Test {

    public void generate() {
        try {
            Document document = new Document(PageSize.A4,10,10,10,10);
            File file = new File("test.pdf");
            FileOutputStream fos = new FileOutputStream(file);
            PdfWriter writer = PdfWriter.getInstance(document, fos);

            writer.open();
            writer.setFullCompression();
            writer.setCompressionLevel(PdfStream.BEST_COMPRESSION);
            document.open();

            float width = document.getPageSize().getWidth() - 20;

            float[] columns = { 33.3F, 33.3F, 33.3F };
            PdfPTable table = new PdfPTable(columns);
            table.setTotalWidth(width);
            table.setLockedWidth(true);
            table.setHeaderRows(1);

            PdfPCell cell = new PdfPCell();
            cell.addElement(new Phrase("ITEM NO."));
            table.addCell(cell);
            cell = new PdfPCell();
            cell.addElement(new Phrase("PART"));
            table.addCell(cell);
            cell = new PdfPCell();
            cell.addElement(new Phrase("PROCESS"));
            table.addCell(cell);

            cell = new PdfPCell();
            cell.setRowspan(6);
            cell.addElement(new Phrase("item 1"));
            table.addCell(cell);

            cell = new PdfPCell();
            cell.setRowspan(3);
            cell.setMinimumHeight(30);
            cell.addElement(new Phrase("part 1"));
            table.addCell(cell);

            cell = new PdfPCell();
            cell.setRowspan(2);
            cell.setMinimumHeight(20);
            cell.addElement(new Phrase("process 1"));
            table.addCell(cell);

            cell = new PdfPCell();
            cell.setRowspan(2);
            cell.setMinimumHeight(20);
            cell.addElement(new Phrase("process 2"));
            table.addCell(cell);

            cell = new PdfPCell();
            cell.setRowspan(3);
            cell.setMinimumHeight(30);
            cell.addElement(new Phrase("part 2"));
            table.addCell(cell);

            cell = new PdfPCell();
            cell.setRowspan(2);
            cell.setMinimumHeight(20);
            cell.addElement(new Phrase("process 3"));
            table.addCell(cell);

            document.add(table);

            document.close();
            writer.close();
            fos.close();

        }catch (DocumentException | IOException e){
            System.err.println(e.getMessage());
        }
    }

}

Pdf generated using: 1.3.28
test-1.3.28.pdf

Pdf generated using: 1.3.36
test-1.3.36.pdf

@fellmann
Copy link
Contributor

I think the problem is not the row height calculation, but the fact that it is not called when using locked width.
@ReinhardJvREx Can you confirm the problem is solved when you remove table.setLockedWidth(true);?

@ReinhardJvREx
Copy link
Author

@fellmann when removing table.setLockedWidth(true); the problem is solved, with the exception that the table does not span the full width of the page.

Result when removing table.setLockedWidth(true);
test.pdf

@fellmann
Copy link
Contributor

@andreasrosdal luckily, there is an easy fix

@andreasrosdal
Copy link
Contributor

andreasrosdal commented Jan 17, 2024

@fellmann Thank you, I am glad you managed to fix this so easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants