Cannot display background color when using rowspan

2019-08-13 22:28发布

问题:

I am trying to create a table like the one shown here:

I want to use a rowspan of 2. The problem is that when I use rowspan, the background color of the top row shows fine but the 2nd row shows white.

I am facing the same issue shown here: http://itext.2136553.n4.nabble.com/Rowspan-and-background-color-td4659361.html

I am trying to do this in Java. Am I missing something silly here ?

回答1:

Please take a look at the SimpleTable10 example. In this example, I tried to reproduce your problem by creating a table that looks exactly like yours, except that I gave a different background color to each cell:

You claim that the background color of the top row shows fine, but that the second row shows white. The screen shot of the simple_table10.pdf example contradicts this allegation.

Please provide some sample code that reproduces the problem, or inspect my code to see how I was able to create colored backgrounds for cells with a rowspan or colspan greater than 1:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(5);
    PdfPCell sn = new PdfPCell(new Phrase("S/N"));
    sn.setRowspan(2);
    sn.setBackgroundColor(BaseColor.YELLOW);
    table.addCell(sn);
    PdfPCell name = new PdfPCell(new Phrase("Name"));
    name.setColspan(3);
    name.setBackgroundColor(BaseColor.CYAN);
    table.addCell(name);
    PdfPCell age = new PdfPCell(new Phrase("Age"));
    age.setRowspan(2);
    age.setBackgroundColor(BaseColor.GRAY);
    table.addCell(age);
    PdfPCell surname = new PdfPCell(new Phrase("SURNAME"));
    surname.setBackgroundColor(BaseColor.BLUE);
    table.addCell(surname);
    PdfPCell firstname = new PdfPCell(new Phrase("FIRST NAME"));
    firstname.setBackgroundColor(BaseColor.RED);
    table.addCell(firstname);
    PdfPCell middlename = new PdfPCell(new Phrase("MIDDLE NAME"));
    middlename.setBackgroundColor(BaseColor.GREEN);
    table.addCell(middlename);
    PdfPCell f1 = new PdfPCell(new Phrase("1"));
    f1.setBackgroundColor(BaseColor.PINK);
    table.addCell(f1);
    PdfPCell f2 = new PdfPCell(new Phrase("James"));
    f2.setBackgroundColor(BaseColor.MAGENTA);
    table.addCell(f2);
    PdfPCell f3 = new PdfPCell(new Phrase("Fish"));
    f3.setBackgroundColor(BaseColor.ORANGE);
    table.addCell(f3);
    PdfPCell f4 = new PdfPCell(new Phrase("Stone"));
    f4.setBackgroundColor(BaseColor.DARK_GRAY);
    table.addCell(f4);
    PdfPCell f5 = new PdfPCell(new Phrase("17"));
    f5.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(f5);
    document.add(table);
    document.close();
}

Make sure that you use a recent, official version of iText(Sharp). We know of people who have been distributing obsolete (and defective) versions of iText(Sharp).