How to create a PDF with a Cartesian grid?

Tags: graphics statePDF syntaxiText 5

Could anyone please provide me a sample program that can dynamically create a grid or even dots starting at (0,0) at bottom left corner on a PDF of page size "Letter"? (max X = 8.5 Inches; max Y = 11 Inches)

Posted on StackOverflow on Jun 10, 2014 by user3727496

Please take a look at the Grid example. In this example, I define the pagesize variable like this:

Rectangle pagesize = PageSize.LETTER;

I use this variable to create the Document instance, and I also use it in the loops that draw the grid:

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;
}
canvas.fill();

In this case, I increment x and y with 72 user units. This means that the distance between the dots will be 1 inch.