10

I cannot find a solution for a very simple question, how can I set a custom color for a text/line/etc. using iText7 in java code?

I found this reply for iText5 but in version 7 there is no BaseColor class...

Community
  • 1
  • 1
Balagex
  • 121
  • 1
  • 1
  • 5

4 Answers4

21

I use this code to customize the text color:

com.itextpdf.kernel.color.Color myColor = new DeviceRgb(255, 100, 20);
Paragraph colorPara = new Paragraph("text with color").setFontColor(myColor);
cao
  • 259
  • 2
  • 11
  • Nice, it is better than what I figured out. (new DeviceRgb has an another constructor with float parameters, and that one is using the 0-1 interval, but the constructor using int parameters is more developer friendly) – Balagex Sep 22 '16 at 08:07
5

One option is to use the ColorConstants. It is located in the kernel dependency.

PdfCanvas canvas = new PdfCanvas(pdfPage);
canvas.setColor(ColorConstants.DARK_GRAY, true);
Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
3
Cell hcell = new Cell();   
Paragraph paragraph = new Paragraph("Your Text").setTextAlignment(TextAlignment.CENTER).setFontSize(8);
hcell.add(paragraph);
Color color = WebColors.getRGBColor("red"); // Color name to RGB
hcell.setBackgroundColor(color);
Prashant
  • 616
  • 6
  • 10
  • 3
    **C#** version here, _CamelCase alert_. `WebColors.GetRGBColor("red")` or `WebColors.GetRGBColor("#FF0000")` works fine. For some reason `Color.MakeColor(new PdfDeviceCs.Rgb(), new [] {31f,141f,71f})` doesn't work. – Henryk Budzinski Jun 14 '22 at 18:23
2

I found the following solution after some try-and-fail loop:

        float[] col = new float[]{0,0.5f,0};
        Color szin = Color.makeColor(Color.GREEN.getColorSpace(), col);
        Canvas canvas = new Canvas(pdfCanvas, pdfDoc, page.getPageSize());
        canvas.setProperty(Property.FONT_COLOR, szin);

At first, I had no idea about how can I get/set that color space, what was required as first parameter of the makeColor method. After logging out the following

LOGGER.info(Color.GREEN.getColorSpace().getPdfObject());

I saw, it is an RGB related info, so maybe I should specify the second float[] with 3 elements (not 4, like cmyk).

Info: 2464035 [http-listener-1(3)] INFO fornax.hu.pdf.generate.PdfCreator2 - /DeviceRGB

The other big problem was, how should I set the float values. Logical tip was for a dark green is 62,172,62, but I saw nothing. I had to realize, 0 acting as 0, but any number greater than 1 act as 255 in the result color, so tried to set values between 0 and 1, and I got the JACKPOT!

test color 1 with {1,0.5f,0} test color 2 with {0,0.5f,0}

Special thanks for iText7 documentation writers, who were unable to insert any example for this very very basic stuff for noobs like me.

Balagex
  • 121
  • 1
  • 1
  • 5
  • 1
    Feel free to submit a pull request that improves the javadocs: https://github.com/itext/itext7/pulls. As for the documentation on http://developers.itextpdf.com/examples-itext7, this is an ongoing process and the documentation continues to grow almost daily, very often based on Stack Overflow questions like yours. – Amedee Van Gasse Sep 19 '16 at 09:20