Skip to main content
Skip table of contents

Drawing a Grid

Example written in the context of the question Click How to create a PDF with a Cartesian grid?


grid

##GITHUB:https://github.com/itext/i7js-examples/blob/develop/src/main/java/com/itextpdf/samples/sandbox/objects/Grid.java##

C#

CPP
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;

namespace iText.Samples.Sandbox.Objects
{
    public class Grid
    {
        public static readonly string DEST = "results/sandbox/objects/grid.pdf";

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new Grid().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            PageSize pageSize = new PageSize(612, 792);
            pdfDoc.SetDefaultPageSize(pageSize);

            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            for (float x = 0; x < pageSize.GetWidth(); x += 72f)
            {
                for (float y = 0; y < pageSize.GetHeight(); y += 72f)
                {
                    canvas.Circle(x, y, 1f);
                }
            }

            canvas.Fill();

            pdfDoc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.