Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Itext pdf default size

Related Tutorials


 
 

Share on Google+Share on Google+

pdf default size

Advertisement
In this section, you will learn about the pdf default size.

pdf default size

     

In this program we are going to tell you how we can automatically set the default size of pdf. Its options are from A0 to A10. These text explains the ISO 216 paper size system and the ideas behind its design. It will adjust the size of a pdf file irrespective of the fact whether it exists or not. If it exists then its fine, otherwise a pdf file will be created. After going this tutorial you will better understand how you can adjust the paper size. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named defaultSizePDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.  Inside the constructor of Document we have encapsulated predefined page size. These page sizes are encapsulated in the class PageSize. In this example we have use many PageSize

Now create a document writer that writes the equivalent syntax for a document's content to a specific OutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously created Document. Inside the constructor of Paragraph pass the String . Set the page size to the document. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class defaultSizePDF{
public static void main(String arg[])throws Exception{
Document document = new Document();
PdfWriter.getInstance(document, 
new FileOutputStream("DefaultPageSize.pdf"));
document.open();
document.add(new Paragraph("The default PageSize==>>RoseIndia"));
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.close();
}
}

The output of the program is given below:

Download this program

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Discuss: pdf default size  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Flock Marc
June 14, 2011
resize and crop page

I have documents with cutting edge (little horizontal and vertical lines in the corners)I must delete these line and the content of the page can not be resized. Is it possible with iText ? and how ? Thanks for your help
DMCA.com