Share on Google+Share on Google+


In this section, you will learn how to design some text according to our need by use of Phrase. We can format our pdf by using Phrase() constructor.



In this section, you will learn how to design some text according to our need by use of Phrase. We can format our pdf by using Phrase() constructor.
 We can add the text, Chunk and set the font size , name and style of the font.

Code Description:

All the APIs used in the code of the program are derived here:

The class Phrase is used as render of process. This class comes under the  com.lowagie.text.Phrase. The Phrase   extends ArrayList
 and it implements the TextElementArray . A Phrase is a series of Chunks.  A Phrase has a main Font, but some chunks within the phrase
 can have a Font that differs from the main Font. All the Chunks in a Phrase have the same leading.e.g.

// Passed as a parameter with the default leading = 16
 Phrase phrase0 = new Phrase(25, "this is a phrase with leading 16");
//Default Constructor  
 Phrase phrase1 = new Phrase();
//Passing String
 Phrase phrase2 = new Phrase("this is a phrase");
  // Font is passed (explicitely or embedded in a chunk), the default leading = 1.5 * size of the font
 Phrase phrase3 = new Phrase("this is a phrase with a red, normal font Courier, size 12", 
FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL, new Color(255, 0, 0)));
//Passing Chunk 
 Phrase phrase4 = new Phrase(new Chunk("this is a phrase"));
//Passing Chunk,Font with the default leading = 18
 Phrase phrase5 = new Phrase(18, new Chunk("this is a phrase", FontFactory.getFont(FontFactory.HELVETICA,
 16, Font.BOLD, new Color(255, 0, 0)));

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.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class Phrases {
  public static void main(String[] args)throws Exception 
  Document document = new Document();
  Phrase phrase0 = new Phrase();
  Phrase phrase1 = new Phrase("This
 is a phrase\n"
  Phrase phrase2 = new Phrase(25,"This 
is a phrase with leading 25. \n"
  Phrase phrase3 = new Phrase("This is a 
phrase with a red, normal font 
,  FontFactory.getFont(
 Font.NORMAL,new Color
Phrase phrase4 = new Phrase(new 
Chunk("(4) this is a phrase\n"));
Phrase phrase5 = new Phrase(18,
new Chunk
("This is a phrase in Helvetica, bold,
 red and size 16 with a given leading 
of 18 points.\n"
FontFactory.HELVETICA, 16,Font.BOLD, 
new Color(25500))));
  Chunk chunk = new Chunk("
 This is a font: "
Phrase phrase6=new Phrase();
  phrase6.add(new Chunk("Helvetica"
FontFactory.HELVETICA, 12)));
  phrase6.add(new Chunk("ZapfDingBats",
FontFactory.ZAPFDINGBATS, 12)));
  Phrase phrase7 = new Phrase
"If you don't add a newline !");

The output of the program is given below:

Download this example.




Posted on: March 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Phrase  

Post your Comment

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