2

I have string like below, and i can't split the string.

String result="Developed By : Mr.XXXXX";

i can create a paragraph in itext and set font with color like below,

Font dataGreenFont = FontFactory.getFont("Garamond", 10,Color.GREEN);
preface.add(new Paragraph(result, dataGreenFont));

it set the green color to entire text result but i want to set color only for Mr.XXXXX part. How do i do this?

Ramakrishna
  • 426
  • 7
  • 26

1 Answers1

4

First this: you are using an obsolete version of iText. Please upgrade!

As for your question: a Paragraph consists of a series of Chunk objects. A Chunk is an atomic part of text in which all the glyphs are in the same font, have the same font size, color, etc...

Hence you need to split your String in two parts:

Font dataGreenFont = FontFactory.getFont("Garamond", 10, BaseColor.GREEN);
Font dataBlackFont = FontFactory.getFont("Garamond", 10, BaseColor.BLACK);
Paragraph p = new Paragraph();
p.Add(new Chunk("Developed By : ", dataGreenFont));
p.Add(new Chunk("Mr.XXXXX", dataBlackFont));
document.add(p);
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165