R4R Java SourceCode Set Page Size Of PDF Java

Set Page Size Of PDF Java

In this example we are going to set diffrent size page of a PDF file.To set the size of PDF file we have setPageSize(int pageSize) method .Here pageSize is int value which is the size of page.It may be A4,A1 ,A2 etc. 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("LandscapePortrait.pdf"));

Step3: Open the document

document.open();

Step4: Add a paragraph to the document, set size and create new page

document.add(new Paragraph("The default PageSize is DIN A4."));
document.setPageSize(PageSize.A3);
document.newPage();

Step 5: Close the document

document.close();:

SOURCE CODE


package com.itext.pdf.example.r4r;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

/**
 * Demonstrates the use of PageSize.
 * In this example we are creating a PDF file. Then we 
are creating new pages with different size.
* To create new a page we have a method newPage(). * To set size we have a method setpagesize(int pageSize). * To add paragraph we have add(Paragraph p) methods. */ public class DefaultPDFPageSize { /** * Creates a PDF document with a certain pagesize */ public static void main(String[] args) { System.out .println("The default PageSize and some
other standard sizes available which you can use."
); // Step 1: Creation of a document-object. Document document = new Document(); try { // Step2: // We are creating a writer that listens to
the document and directs a PDF file.
PdfWriter.getInstance(document, new FileOutputStream( "DefaultPageSize.pdf")); // Step3: We are opening the document. document.open(); // Step 4: We are adding here some paragraphs
to the document.
document.add(new Paragraph("The default PageSize
is DIN A4."
)); document.setPageSize(PageSize.A3); document.newPage(); document.add(new Paragraph("This PageSize is DIN A3.")); document.setPageSize(PageSize.A2); document.newPage(); document.add(new Paragraph("This PageSize is DIN A2.")); document.setPageSize(PageSize.A1); document.newPage(); document.add(new Paragraph("This PageSize is DIN A1.")); document.setPageSize(PageSize.A0); document.newPage(); document.add(new Paragraph("This PageSize is DIN A0.")); document.setPageSize(PageSize.A5); document.newPage(); document.add(new Paragraph("This PageSize is DIN A5.")); document.setPageSize(PageSize.A6); document.newPage(); document.add(new Paragraph("This PageSize is DIN A6.")); document.setPageSize(PageSize.A7); document.newPage(); document.add(new Paragraph("This PageSize is DIN A7.")); document.setPageSize(PageSize.A8); document.newPage(); document.add(new Paragraph("This PageSize is DIN A8.")); document.setPageSize(PageSize.LETTER); document.newPage(); document.add(new Paragraph("This PageSize is LETTER.")); document.add(new Paragraph( "A Lot of other Standar
d PageSizes are Available."
)); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // Step5: We are closing the document. document.close(); } }

Download Source File
 

Output of This Example

 


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20