Search Here

Custom Search

Thursday, February 7, 2008

How to show a pdf in landscape mode using iText API in Java

This post may help the pdf developers who use iText. I mean who use the com.lowagie.text and its sister packages.

I guess you (iText users) are familiar with the class
com.lowagie.text.Document

We have to tell this class the page size of the pdf in the following way.

document.setPageSize(PageSize.A4));   // suppose document is the instance of Document

But you will not find the word ‘Landscape’ anywhere in the context. So, how can you tell it to generate a pdf of landscape page size? Yes there is a simple but uncommon way to specify this.
See the solution below:

document.setPageSize(PageSize.A4.rotate());

Actually PageSize.A4 is a static variable of type com.lowagie.text.Rectangle

So by rotating it you are getting the landscape size of A4 page.