com.lowagie.text.Chunk

com.lowagie.text.Phrase

com.lowagie.text.Paragraph


com.lowagie.text.Chunk

You need iText library to do this tutorial. Please read Setup iText for project & Hello World for a tutorial on download and add iText library to your eclipse project.

Chunk is the smallest significant part of text that can be added to a document. See the following code snipplet

 

 

package com.geek.tutorial.itext.text;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import java.awt.Color;
import java.io.FileOutputStream;

public class Chunk_Example {
	
	public Chunk_Example() throws Exception{
		
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream("chunk_example.pdf"));
		document.open();
		
		Font font = new Font(Font.COURIER, 10, Font.BOLD); // 1
		font.setColor(new Color(0x92, 0x90, 0x83));
		
		Chunk chunk = 
			new Chunk("testing text element", font); // 2
		
		chunk.setBackground(new Color(0xff, 0xe4, 0x00)); // 3

		document.add(chunk); // 4
		
		document.close();
		
	}

	public static void main(String[] args) {
		try{
			Chunk_Example textExample = new Chunk_Example();
		}catch(Exception e){
			System.out.println(e);
		}
	}
}
		

1. Set the font type, size,style and color.
2. Create the Chunk instance and initialize it with testing String and the Font instance.
3. Set the chunk background colour.
4. Add the chunk to the document.

Run the example above and your will see the following PDF.

However, normally we don't use chunk for rendering text on PDF. Because chunk do not auto insert a new line when the string reach the edge of the page. You can use com.lowagie.text.Phrase to render text on the PDF and have it automatic insert a new line when it reach the edge of the page.

com.lowagie.text.Phrase


The Phrase can itself contain a default String, and allow to add series of Chunks after its construction.

A Phrase has its default Font, but the later added Chunks can have their own Font. See the following code example:

package com.geek.tutorial.itext.text;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;


import java.awt.Color;
import java.io.FileOutputStream;

public class Phrase_Example {

	
	public Phrase_Example() throws Exception{
		
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream("phrase_example.pdf"));
		document.open();
		
		Font font = new Font(Font.COURIER, 10, Font.BOLD);
		font.setColor(new Color(0x92, 0x90, 0x83));
		Chunk chunk = new Chunk("testing text element ", font);
		chunk.setBackground(new Color(0xff, 0xe4, 0x00));
		
		Phrase phrase = 
			new Phrase(20, "This is initial text. "); // 1		
		
		for(int i=0; i < 10; i++)
		{
			phrase.add(chunk); // 2
		}
		
		document.add(phrase); // 3
		
		document.close();		
	}
	

	public static void main(String[] args) {
		try{
			Phrase_Example textExample = new Phrase_Example();
		}catch(Exception e){
			System.out.println(e);
		}
	}

}
		
		

1. Create a Phrase instance with 20 leading (The space between each line) and initial String content.
2. Add 10 chunk to the Phrase.
3. Add the Phrase to the document.

Run the example above and see the result phrase_example.pdf.

Now all the text are automatic feed to next line when it reach the edge of the page. But Phrase object handle all text in one huge paragraph, when you want to start a new paragraph you can do the following in the above code example:


	phrase.add("\n");

It will add a new line to the phrase without needed to reach the edge of page. However, there is a proper way to handle a paragraph, and the Class to handle this tasks is... com.lowagie.text.Paragraph Class!

 

com.lowagie.text.Paragraph


The Paragraph can itself contain a default String, and allow to add series of Chunks and/or Phrases after its construction. Each Paragraph that added to the document will automatic start in a new line.

com.lowagie.text.Paragraph also allow you to specify other layout-parameters such as indentation and alignment of the text.

package com.geek.tutorial.itext.text;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;


import java.awt.Color;
import java.io.FileOutputStream;

public class Paragraph_Example {

	
	public Paragraph_Example() throws Exception{
		
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream("paragraph_example.pdf"));
		document.open();
		
		Font font = new Font(Font.COURIER, 10, Font.BOLD);
		font.setColor(new Color(0x92, 0x90, 0x83));
		Chunk chunk = new Chunk("testing text element ", font);
		chunk.setBackground(new Color(0xff, 0xe4, 0x00));
		
		Phrase phrase = new Phrase(20, "This is initial text. "); 
		
		for(int i=0; i < 10; i++)
		{
			phrase.add(chunk); 
		}
		
		Paragraph paragraph = new Paragraph(); // 1
		paragraph.add(phrase); // 2
		
		document.add(paragraph); // 3		
		document.add(paragraph); // 4	
		
		document.close();		
	}
	

	public static void main(String[] args) {
		try{
			Paragraph_Example textExample = 
				new Paragraph_Example();
		}catch(Exception e){
			System.out.println(e);
		}
	}

}
		
		

1. Create an instance of paragraph object.
2. Add phrase to the paragraph.
3. Add first paragraph to the document.
4. Add second paragraph to the document.

Now the Paragraph that added to the document will automatic start in a new line. See figure below:

Note that before add paragraph to the document, you can specify the alignment as follow:


	// set alignment to left
	paragraph.setAlignment(Element.ALIGN_LEFT);

	// set alignment to center
	paragraph.setAlignment(Element.ALIGN_CENTER);

	// set alignment to right
	paragraph.setAlignment(Element.ALIGN_RIGHT);

You can set the indentation(the amount of space between the paragraph and the edge of page) of paragraph by following code:


	// set left indentation to 30
	paragraph.setIndentationLeft(30);
		
	// set right indentation to 50
	paragraph.setIndentationRight(50);
		

Your donation will be use for this project's site maintainance and further development of the content. Your support can help us provide higher quality of free tutorials and services to everyone in future Support This Project. Download latest source code for this project.

 

This is the project of
SourceForge.net Logo