R4R Java SourceCode Set Different Alignment Of PDF Using Java


Set Different Alignment Of PDF Using Java

Here we are creating a PDF document with different pages that have different alignments. To set alignment we are using setAlignment(int arg).

To create a PDF using java you need iText API.You can download from website.This is an open source jar .Then you have to set class path of this iText jar.

There are basic five steps to create PDF:

Step1: Creation of a document-object

Document document = new Document();

Step2: Create a writer that listens to the document and directs a PDF-stream to a file.

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

Step3: Open the document

document.open();

Step4: Create an object of paragraph set alignment ,add contant into paragraph and finaly add the paragraph with document

Paragraph paragraph = new Paragraph();
paragraph.setAlignment(Element.ALIGN_RIGHT);

paragraph.add("RIGHT Alignment");
document.add(paragraph);

Step 5: Close the document

document.close();:

SOURCE CODE


package com.itext.pdf.example.r4r;

//These are package of java.io.*
import java.io.FileOutputStream;
import java.io.IOException;
//These are package of iText API 
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Here in this example we are creating a PDF document with 
different pages that have different alignments.
* To set alignment we are using setAlignment(int arg). */ public class SetAlignmentInPDF { /** * Creates a PDF document with different pages that have
different alignments.
*/ public static void main(String[] args) { System.out.println("Set Alignment in PDF file"); // Step1: Here we are creating an object of a document. Document document = new Document(PageSize.A5, 36
,
72, 108, 180); try { // Step 2:Here we are creating a writer tha
t listens to
the document and directs a PDF-stream to a file.
PdfWriter .getInstance(document, ne
w
FileOutputStream
(
"Alignment.pdf")); // Step3: Here we are opening the document document.open(); // Step4: Here we are adding paragraph, creating new
page and setting alignments.
Paragraph paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.add("RIGHT Alignment"); document.add(paragraph); document.newPage(); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_CENTER); paragraph.add("CENTER Alignment"); document.add(paragraph); document.newPage(); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_BASELINE); paragraph.add("BASELINE Alignment"); document.add(paragraph); document.newPage(); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_BOTTOM); paragraph.add("BOTTOM Alignment"); document.add(paragraph); document.newPage(); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_MIDDLE); paragraph.add("MIDDLE Alignment"); document.add(paragraph); document.newPage(); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_TOP); paragraph.add("TOP Alignment"); document.add(paragraph); document.newPage(); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_LEFT); paragraph.add("LEFT Alignment"); document.add(paragraph); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // Step5:Here we are closing the document. document.close(); } }

Download Source File