Set Space Ratio and Alignment

In this example , we are going to explain how we can set space ratio and how we can gives various alignment. You can make pdf with no space between the characters of a word .

Set Space Ratio and Alignment

In this example , we are going to explain how we can set space ratio and how we can gives various alignment. You can make pdf with no space between the characters of a word .

Set Space Ratio and Alignment

Set Space Ratio and Alignment     

     

In this example , we are going to explain how we can set space ratio and how we can gives various alignment. You can make pdf with no space between the characters of a word .

Code Description:

setAlignment(int alignment):

We can set the aligment of a Paragraph with the method setAlignment(int alignment). The alignment can be one of the following values:

  1. Element.ALIGN_LEFT
  2. Element.ALIGN_CENTER
  3. Element.ALIGN_RIGHT
  4. Element.ALIGN_JUSTIFIED

setSpaceCharRatio:
If you want the alignment to be justified, the space between the words can be stretched, as well as the space between the characters of a word. The default ratio of this word-char space is 2.5. You can change this ratio with setSpaceCharRatio. For instance, if you don't want the space between the characters of a word .We can do this by:

writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);

The code of the program is given below:

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class SpaceWordRatio {  
  public static void main(String[] args)throws Exception {
  System.out.println("Example of Space Word Ratio");  
  Document document = new Document(PageSize.A4,
 
503505050); 
  PdfWriter writer = PdfWriter.getInstance(document, 
new 
FileOutputStream("spacewordratio.pdf"));
  document.open();
  String input = "Rose india is a java site which provide
 you online tutorial and online interview question help"
;
  Paragraph paragraph = new Paragraph(input);
  paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
  document.add(paragraph);
  document.newPage();
  writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
  document.add(paragraph);
  document.close();
  }
}

The output of the program is given below:

Download this example.