2
votes

I'm creating a PDF with images and text. Text can be of varying color. I convert the color from the HTML color code to get me a System.Drawing.Color object but the color turns out differently in the generated PDF. In one particular instance, the html code is 3C3C3C and it comes out as 3C403E. I check the color by using a color picker to get the color in the PDF.

var color = System.Drawing.ColorTranslator.FromHtml("#3C3C3C);
iTextSharp.text.Font font = font = FontFactory.GetFont(FontFactory.HELVETICA);
font.Color = new BaseColor(color);

// boxValue is a string
Phrase phrase = new Phrase(boxValue, font);

ColumnText columnText = new ColumnText(canvas);
columnText.SetSimpleColumn(boxRectangle);
columnText.Leading = lineHeight;
columnText.SetLeading(lineHeight, 0);
columnText.SetText(phrase);
columnText.Alignment = alignment;
columnText.Go();
2
Is the color model of the text RGB, or is it something else (CMYK, for example)? Conversions from one color model to another can cause differences. "A color picker" merely samples it from the screen. - Jongware
@Jongware I left it as device RGB. I also tried converting it to CMYK but I must be missing something (like the correct color profile) because it was way off. I understand the color picker samples it from the screen but I don't know of another way to check the actual color in the PDF. I tried PDFGears but I don't see the color anywhere. Any suggestions to check the color in the PDF? - Victor P
Can you upload a sample somewhere and add a link in your post? Include a couple of different colors, with the hex codes they are supposed to be. - Jongware
It's also strange that you would use the ColorTranslator class when you can create a BaseColor directly using the WebColors.getRGBColor method. - Bruno Lowagie
@BrunoLowagie Thanks, didn't know about that - Victor P

2 Answers

0
votes

It turns out that it does save the actual color in the PDF. I discovered this by using a PDF inspector and do see the correct values used.

0
votes
   public static BaseColor stringToBaseColor(string code)
    {


        Color color = ColorFromString(code);
        BaseColor b = new BaseColor(color);
        return ColorToBaseColor(color);

    }


    public static BaseColor ColorToBaseColor(Color color)
    {

       return new BaseColor(color);

    }

    public static Color ColorFromString(string code)
    {

        string[] colors = code.Split(',');
        List<int> myInts = Array.ConvertAll(colors, s => int.Parse(s)).ToList();
        return  Color.FromArgb(myInts[0], myInts[1], myInts[2]);

    }