Home Java Itext Phrase
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Phrase

Advertisement
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.

Phrase   

     

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:

Phrase:
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 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.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class Phrases {
  public static void main(String[] args)throws Exception 
 {
  System.out.println("Phrases");
  Document document = new Document();
  PdfWriter.getInstance(document, 
new 
FileOutputStream("Phrasespdf.pdf"));
  document.open();
  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 
Courier20"
,  FontFactory.getFont(
FontFactory.COURIER, 
20,
 Font.NORMAL,new Color
(25500)));
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.getFont
(
FontFactory.HELVETICA, 16,Font.BOLD, 
new Color(25500))));
  Chunk chunk = new Chunk("
 This is a font: "
);
Phrase phrase6=new Phrase();
phrase6.add(chunk);
  phrase6.add(new Chunk("Helvetica"
FontFactory.getFont
(
FontFactory.HELVETICA, 12)));
  phrase6.add(chunk);
  phrase6.add(new Chunk("ZapfDingBats",
 FontFactory.getFont
(
FontFactory.ZAPFDINGBATS, 12)));
  Phrase phrase7 = new Phrase
(
"If you don't add a newline !");
  document.add(phrase1);
  document.add(phrase2);
  document.add(phrase3);
  document.add(phrase4);
  document.add(phrase5);
  document.add(phrase6);
  document.add(phrase7);  
  document.close();
  }
}

The output of the program is given below:

Download this example.

 

Advertisement

Liked it!  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.

Posted on: March 13, 2008

Ask Questions?    Discuss: Phrase  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com