Saturday 17 September 2016

Itext: Specifying page size

itextpdf API provides PageSize class to specify page size of the pdf document. PageSize class provides number of built-in constants to support A1, A2, A3, A4...A10 and B1, B2...B10 formats.

What is the default page size in itext?
A4 (8.3 X  11.7 inches).

Following application create A4 size pdf document.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

public class HelloWorld {
 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  Document document = new Document(PageSize.A4);
  PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
  document.open();
  document.add(new Paragraph("Hello World!"));
  document.close();
 }
}
By default page size is measured in inches. I copied following content from ISO 32000 specification.

In PDF versions earlier than PDF 1.6, the size of the default user space unit is fixed at 1 / 72 inch. In Acrobat viewers earlier than version 4.0, the minimum allowed page size is 72 by 72 units in default
user space (1 by 1 inch); the maximum is 3240 by 3240 units (45 by 45 inches).

 In Acrobat versions 5.0 and later, the minimum allowed page size is 3 by 3 units (approximately 0.04 by 0.04 inch); the maximum is 14,400 by 14,400 units (200 by 200 inches).

Beginning with PDF 1.6, the size of the default user space unit may be set with the UserUnit entry of the page dictionary. Acrobat 7.0 supports a maximum UserUnit value of 75,000, which gives a
maximum page dimension of 15,000,000 inches (14,400 * 75,000 * 1 / 72). The minimum UserUnit value is 1.0 (the default).

You can also specify customized page size using Rectangle instance. Rectangle class provide following constructors to get an instance of this class.

public Rectangle(final float llx, final float lly, final float urx, final float ury)
public Rectangle(final float llx, final float lly, final float urx, final float ury, final int rotation)
public Rectangle(final float urx, final float ury, final int rotation)

llx     specifies lower left x
lly     specifies lower left y
urx    specifies upper right x
ury    specifies upper right y
rotation specifies the rotation

How to create 4 X 10 inches document.
Create a rectangle instance with arguments 4*72, 10*72.

Document document = new Document(new Rectangle(288, 720));
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;

public class HelloWorld {
 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  // 288/72 = 4 and 720/72 = 10 inches
  Document document = new Document(new Rectangle(288, 720));
  PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
  document.open();
  document.add(new Paragraph("Hello World!"));
  document.close();
 }
}

Run above program, it creates a document with 4X10 inches. To reconfirm, open PDF document, go to file -> properties -> go to description tab, you can able to see the page size.



Previous                                                 Next                                                 Home

No comments:

Post a Comment