Saturday 17 September 2016

Itext: Specifying margins to page

Document class provide following constructor, where you can specify page size, left, right, top and bottom margins of a page.

public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
pageSize specifies the pageSize
marginLeft specifies the margin on the left
marginRight specifies the margin on the right
marginTop specifies the margin on the top
marginBottom specifies the margin on the bottom

What are the default values for margins?
As per itext documentation, default constructor defined like below.

public Document(Rectangle pageSize) {
         this(pageSize, 36, 36, 36, 36);
}

36/72 = 0.5 inches is the default for margins.

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).

package com.learn.tutorial;

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), 36, 72, 144, 288);
  PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
  document.open();
  document.add(new Paragraph("Hello World!"));
  document.close();
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment