簡體   English   中英

如何使用iText設置表格單元格的背景顏色?

[英]How to set a background color of a Table Cell using iText?

雖然當然可以使用BaseColor ,但默認情況下它提供的選擇非常有限。

我想知道如何將自己的自定義顏色添加到文檔中?

...
        PdfPTable table = new PdfPTable(3);

        PdfPCell cell = new PdfPCell(new Phrase("some clever text"));
        cell.setBackgroundColor(BaseColor.GREEN);
        table.addCell(cell);
...

發布,希望其他人會發現此回復有用。

似乎可以從WebColor創建一個新的BaseColor

BaseColor myColor = WebColors.GetRGBColor("#A00000");

然后可以將其添加為背景:

cell.setBackgroundColor(myColor);

很多選擇。

BaseColor color = new BaseColor(red, green, blue); // or red, green, blue, alpha
CYMKColor cmyk = new CMYKColor(cyan, yellow, magenta, black); // no alpha
GrayColor gray = new GrayColor(someFloatBetweenZeroAndOneInclusive); // no alpha

還有圖案顏色和陰影顏色,但這些顏色要簡單得多。

試試這個:
cell.setBackgroundColor(new BaseColor(226, 226, 226));
要么:
cell.setBackgroundColor(WebColors.getRGBColor("#E2E2E2")); 棄用

另一個解決方案是:

public static String mColor = "#aa8cc5";
int aa = Integer.parseInt(mColor,16); // base 16
int colorArr = Color.rgb(Color.red(aa),Color.green(aa),Color.blue(aa));
cell1.setBackgroundColor(new BaseColor(colorArr));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM