changing Chunk Color

In this section, you will see how to change chunk color in java.

changing Chunk Color

In this section, you will see how to change chunk color in java.

changing Chunk Color

changing Chunk Color

     

In this program we are going to tell you how you can change the color of chunk color .This example tell you how you can change the color of chunk and how you can add fonts and gives the size of the fonts.

Code Description:

In this Example we create the object of the paragraph .In this paragraph we add the chunk and  set the font by using the getFont(fontname,size,coloor ).The getFont() method is a method of FontFactory. We are changing the color of the chunk by passing the color values into the getFont() method.

FontFactory:
The class FontFactory is used to create a Font. It is easiest way to get a new Font object. FontFactory has an API with methods that can construct a Font object of any kind. The constructed font is in a uniform way. There are some CID fonts which are not in it.
 
getFont:

The getFont() method is used to gets the current font for the window.The getFont method is used to get Font-Object from FontFactory class.

Font.DEFAULTSIZE:
This is used to set the default size.

Color(0xFF, 0x00, 0x00):
This constructor is used to create the object of the color. We pass three hexadecimal values to which gives  the combination of colors. 

Font.BOLD:
This is used to set text BOLD. 

Font.ITALIC:
This is used to set text ITALIC.

FontFactory.HELVETICA:
This is used to set text style HELVETICA.

The code of the program is given below:

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class changeChunkColor {
  public static void main(String[] argsthrows Exception
  {
  System.out.println("FontColor");
  Document document = new Document();
  PdfWriter writer = PdfWriter.getInstance(document,new 
FileOutputStream
("changeChunkColor.pdf"));
  document.open();
  Paragraph paragraph;
  paragraph = new Paragraph("Roses");
  paragraph.add(new Chunk("india", FontFactory.getFont
(
FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLD, new 
Color
(0xFF0x000x00))));
  document.add(paragraph);
  paragraph = new Paragraph("India");
  paragraph.add(new Chunk(".net", FontFactory.getFont
(
FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC, new
 
Color(0x000x000xFF))));
  document.add(paragraph);  
  document.close();
  }
}

The output of the program is given below:

Download this example.