changing Background Color

This section helps you for changing background color in java.

changing Background Color


In this program, we are going to tell you how you can change background color of a pdf file. You can change the background color of any chunk with the help of setBackground(Color color)

In this example, we are creating a chunk then we use setBackground(Color color) to set the background color of that chunk. Here we pass the object of Color by using
new Color(0xFF, 0xFF, 0x00). We are passing three hexadecimal values into the Color constructor. Which is used to create the object of the Color. 

Code Description:

setBackground(Color color):
If you want to change the background color  then we use this method. This method is used to change the color of  chunk of text into the chunk object. We use this for set the background color of any text.

There are some methods that can be invoked on a Chunk. The method setTextRise(float f)is used to write the chunks  in super or subscript.

The code of the program is given below:

import java.awt.Color;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class changeBackgroundColor {
 public static void main(String[] args)throws Exception {
  System.out.println("Background color");
 Document document = new Document();
  PdfWriter.getInstance(document,new FileOutputStream
  Chunk chunk =new Chunk("")
  chunk.setBackground(new Color(0xFF0xFF0x00));
  Paragraph paragraph = new Paragraph("The following chunk is ");
  Chunk chunk2,chunk3;
  chunk2 = new Chunk("");
  chunk2.setBackground(new Color(0xb00xb00xb0));
  chunk3 = new Chunk("");
  chunk3.setBackground(new Color(0xFF0xDE0xAD));

The output of the program is given below:

Download this example.


Share on Google+Share on Google+

changing Background Color

Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: changing Background Color   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image