Drawing a Grid

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

31st May 2016
admin-marketing

Switch code for this example

Grid.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.objects;
  9.  
  10. import com.itextpdf.kernel.geom.PageSize;
  11. import com.itextpdf.kernel.pdf.PdfDocument;
  12. import com.itextpdf.kernel.pdf.PdfWriter;
  13. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  14. import com.itextpdf.samples.GenericTest;
  15. import com.itextpdf.test.annotations.type.SampleTest;
  16.  
  17. import org.junit.experimental.categories.Category;
  18.  
  19. import java.io.File;
  20. import java.io.IOException;
  21.  
  22. @Category(SampleTest.class)
  23. public class Grid extends GenericTest {
  24.     public static final String DEST = "./target/test/resources/sandbox/objects/grid.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new FullDottedLine().manipulatePdf(DEST);
  30.     }
  31.  
  32.     public void manipulatePdf(String dest) throws IOException {
  33.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  34.         PageSize pageSize = new PageSize(612, 792);
  35.         pdfDoc.setDefaultPageSize(pageSize);
  36.  
  37.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  38.         for (float x = 0; x < pageSize.getWidth(); ) {
  39.             for (float y = 0; y < pageSize.getHeight(); ) {
  40.                 canvas.circle(x, y, 1f);
  41.                 y += 72f;
  42.             }
  43.             x += 72f;
  44.         }
  45.         canvas.fill();
  46.  
  47.         pdfDoc.close();
  48.     }
  49. }

results

Contact

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