Drawing a Grid

This is a code example of iText PDF, discover more.

23rd October 2015

Switch code for this example

package sandbox.objects;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

public class Grid {

    public static final String DEST = "results/objects/grid.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        new Grid().createPdf(DEST);
    public void createPdf(String dest) throws FileNotFoundException, DocumentException {
        Rectangle pagesize = PageSize.LETTER;
        Document document = new Document(pagesize);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        PdfContentByte canvas = writer.getDirectContent();
        for (float x = 0; x < pagesize.getWidth(); ) {
            for (float y = 0; y < pagesize.getHeight(); ) {
                canvas.circle(x, y, 1f);
                y += 72f;
            x += 72f;




Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now