Java Programing laungage

iText Projects

iText Project 1

adplus-dvertising
Set Space Ratio and alignment
Previous Home Next

By help of this example, we are trying to explain how we can set space ratio and various alignment in the PDF file Text. You can make PDF with no space between the characters of a word.

For setting to the alignment of the text we are calling the method setAlignment(int alignment) and if We want to set the aligment of a Paragraph then we only call to the method with the method paragraph object.

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

Examples:

package r4r;
import java.io.FileOutputStream;
import javax.swing.GroupLayout.Alignment;
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 setspacetest 
	{
public static void main(String[] args)throws Exception
	{
System.out.println("Example of Space Word Ratio"); 
Document doc = new Document(PageSize.A4,50, 350, 50, 50); 
PdfWriter writer = PdfWriter.getInstance
(doc, new FileOutputStream("setspacetest.pdf"));

doc.open();
String in = "This is the R4R site for the Free Education. 
       This site provide the good tutorials of the java";

Paragraph par = new Paragraph(in);
par.setAlignment(Element.ALIGN_CENTER);
doc.add(par);
doc.newPage();
writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
doc.add(par);
doc.close();
}
}
Previous Home Next