Monday 19 September 2016

Itext: Setting pagesize and margins

In my previous posts, I explained how to set pagesize and margins to a Document. Document class provides setPageSize and setMargins methods to set page size and margins.

public boolean setPageSize(Rectangle pageSize)
public boolean setMargins(float marginLeft, float marginRight,float marginTop, float marginBottom)

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();
  int marginLeft = 36, marginRight = 72, marginTop = 144, marginBottom = 288;

  document.setPageSize(PageSize.A5);
  document.setMargins(marginLeft, marginRight, marginTop, marginBottom);

  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