0

I want to draw a visual distinction between rows based on their content in my iText table.

I found a tutorial here:, but it's in Java instead of C#, and I don't know what version of iText it is for.

I am using iText 7 and it is apparently radically different than previous versions.

I tried using this code from that article:

if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
    Cell cell = new Cell();
    cell.SetBackgroundColor(Color.DARK_GRAY);
}

...but it won't even compile (the Color class has no definition for DARK_GRAY).

I would much prefer to work with a row at a time, if that's possible, but I don't think it is.

Does anybody know how to change the background color of a row, either all at once, or a cell at a time, using iText 7?

UPDATE

I tried to convert this Java example to C#, but so far no go. This is what I have:

Color headerBg = new DeviceRgb(0xA6, 0xCB, 0x0B);
Color lineColor = new DeviceRgb(0xCC, 0xCC, 0xCC);
. . .
foreach (PairODocs pod in lstPairODocs.OrderByDescending(a => a.Doc1Prcntg).ThenByDescending(a => a.Doc2Prcntg))
{
    if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
    {
        Cell cell = new Cell();
        cell.Add(new Paragraph(pod.Whirred));
        cell.SetBackgroundColor(Color.headerBg);
        table.AddCell(cell);
        // TODO: If get it to work, add for the other four vals below, too...
    }
    else // these work, but are plain jane
    {
        table.AddCell(pod.Whirred);
        table.AddCell(pod.Doc1Count.ToString());
        table.AddCell(pod.Doc1Prcntg.ToString());
        table.AddCell(pod.Doc2Count.ToString());
        table.AddCell(pod.Doc2Prcntg.ToString());
    }          
}

...and the compiler says, "Error CS0117 'Color' does not contain a definition for 'headerBg'" on the cell.SetBackgroundColor(Color.headerBg); line.

UPDATE 2

I had an extraneous "Color." preceding "headerBg" which was causing it to not compile. After removing it, it compiles with this code:

if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
    Cell cell = new Cell();
    cell.Add(new Paragraph(pod.Whirred));
    cell.SetBackgroundColor(headerBg);
    table.AddCell(cell);

    cell = new Cell();
    cell.Add(new Paragraph(pod.Doc1Count.ToString()));
    cell.SetBackgroundColor(headerBg);
    table.AddCell(cell);

    ... // etc.
}
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    Does this answer your question? [How to create / set custom color of table cells and borders in iText 7?](https://stackoverflow.com/questions/47038011/how-to-create-set-custom-color-of-table-cells-and-borders-in-itext-7) – mathis1337 Jun 13 '20 at 21:01
  • @mathis1337: No, it's for Java and doesn't work in C#. I'll post what I tried with it... – B. Clay Shannon-B. Crow Raven Jun 13 '20 at 23:36
  • 1
    I linked that because it links to API docs for iText. Where it is clear as day on how to handle this in .net. I'd give them a read: https://api.itextpdf.com/iText7/dotnet/7.1.11/classi_text_1_1_kernel_1_1_colors_1_1_color.html – mathis1337 Jun 13 '20 at 23:39
  • @mathis1337: "clear as day" is "muddy as a swamp" to me; it would have been nice if they would have given an example of how to apply all that whizbang gobbledygook. – B. Clay Shannon-B. Crow Raven Jun 13 '20 at 23:51
  • Reading documents is core for any level of developer. It would help with the question and what all you have already tried friend. If I have time I will get you an example as this seems rather straight forward to me. I will see what I can whip up once back home. Is using iText a must by the way? – mathis1337 Jun 13 '20 at 23:52
  • `cell.SetBackgroundColor(headerBg);` shouldn't this be working? – Chetan Jun 14 '20 at 00:04
  • @mathis1337: Thanks; iText isn't necessarily a must, if there's a better way. – B. Clay Shannon-B. Crow Raven Jun 14 '20 at 10:15
  • @ChetanRanpariya: Yes, it finally worked; I had an extraneous "Color" prepending the "headerBg" – B. Clay Shannon-B. Crow Raven Jun 14 '20 at 10:23

2 Answers2

1

Actually, if you're willing to stick with some basic colors, it's far simpler to use the iText.Kernel.Color class' ColorConstants class like so:

cell.SetBackgroundColor(ColorConstants.YELLOW);
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
1
iText.Kernel.Colors.Color bgColour = new DeviceRgb(169, 169, 169);  //Create Darkgray color
cell.SetBackgroundColor(bgColour);  // Set the color we created to the cell

https://www.rapidtables.com/web/color/gray-color.html Here you can find multiple colors, just choose the one you like and edit the '169, 169, 169' in the example above.