使用带有itext pdf的rowspan时无法显示背景颜色 [英] Cannot display background color when using rowspan with itext pdf

查看:255
本文介绍了使用带有itext pdf的rowspan时无法显示背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个如下所示的表:



我想使用的行数为2.问题是当我使用rowspan时,顶行的背景颜色显示正常,但第二行显示白色。



我面临的问题是:示例的屏幕截图与此指控相矛盾。



请提供一些重现问题的示例代码,或检查我的代码,看看我是如何为行距或大于1的colspan创建彩色背景:

  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(年龄);
PdfPCell surname = new PdfPCell(new Phrase(SURNAME));
surname.setBackgroundColor(BaseColor.BLUE);
table.addCell(姓);
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 =新的PdfPCell(新短语(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 =新的PdfPCell(新词组(17));
f5.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(f5);
document.add(table);
document.close();
}

确保使用最新的官方版iText(夏普)。我们知道有人分发了过时(有缺陷)的iText(夏普)版本。


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 ?

解决方案

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).

这篇关于使用带有itext pdf的rowspan时无法显示背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆