Saturday 24 September 2016

itext: Paragraph object

A paragraph is a series of chunks (or) Phrase object. Whenever you add a paragraph, a new line is added by default.

Paragraph paragraph = new Paragraph("This is a paragraph",FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
document.add(paragraph);

Paragraph class provides following constructors to define Paragraph instance.

public Paragraph()
public Paragraph(float leading)
public Paragraph(Chunk chunk)
public Paragraph(float leading, Chunk chunk)
public Paragraph(String string)
public Paragraph(String string, Font font)
public Paragraph(float leading, String string)
public Paragraph(float leading, String string, Font font)
public Paragraph(Phrase phrase)

When you see the Paragraph class, it extends the functionality of Phrase class.

public class Paragraph extends Phrase implements Indentable, Spaceable, IAccessibleElement{
         .....
         .....
}

In simple terms, Paragraph has same behavior as Phrase with some extra features like the indentation, the alignment of the text.


Following application uses Paragraph instances to add content to a document.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class ParagraphEx {
 private static Font fontNormal = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL, BaseColor.BLACK);
 private static Font fontHeading = FontFactory.getFont(FontFactory.HELVETICA, 25, Font.BOLD,
   new BaseColor(255, 0, 0));

 private static String data = "We Are Living In An Environment, Where Multiple Hardware Architectures And Multiple Platforms Presents. So It Is Very Difficult To Write, Compile And Link The Same Application, For Each Platform And Each Architecture Separately. The Java Programming Language Solves All The Above Problems. The Java Programming Language Platform Provides A Portable, Interpreted, High-Performance, Simple, Object-Oriented Programming Language And Supporting Run-Time Environment. Java Design And Architecture Decisions Drew From A Variety Of Languages Such As Eiffel, SmallTalk, Objective C, And Cedar/Mesa. Closely Observed The Problems In The Other Languages Like Platform Dependent, Pointers Complexity, Manual Garbage De Allocation Etc., Java Removes The Basic Problems In Other Languages. ";

 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  Document document = new Document();

  PdfWriter.getInstance(document, new FileOutputStream("java_features.pdf"));

  Paragraph heading = new Paragraph("Features Of Java", fontHeading);
  Paragraph features = new Paragraph(data, fontNormal);
  document.open();

  document.add(heading);
  document.add(features);

  document.close();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment