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.

4 comments:

The Java Monkey said...

This is an old blog, but your example is only correct for writing to a document. If you subsequently try and load that document into a web page (iframe, object, embed etc) on a lot of browsers it will be rendered in portrait sideways.

If you check the meta-data of the PDF you will see that it still has a pixel size of portrait A4, because the rectangle you gave it had a 90 degree rotate set on it.

DomFilk said...

I'm not a developer, i always use this free online pdf to word converter to convert pdf to word online.

AdeleB said...

Advanced PDF to text API for programming
Extract text from one page or several pages of PDF document

Anna Schafer said...

Do you have any recommendations?best virtual assistant program