Drawing a Grid

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

23rd October 2015
admin-marketing

Switch code for this example

Grid.java
  1. package sandbox.objects;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.PageSize;
  6. import com.itextpdf.text.Rectangle;
  7. import com.itextpdf.text.pdf.PdfContentByte;
  8. import com.itextpdf.text.pdf.PdfWriter;
  9.  
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import sandbox.WrapToTest;
  15.  
  16. @WrapToTest
  17. public class Grid {
  18.  
  19.     public static final String DEST = "results/objects/grid.pdf";
  20.  
  21.     public static void main(String[] args) throws IOException, DocumentException {
  22.         File file = new File(DEST);
  23.         file.getParentFile().mkdirs();
  24.         new Grid().createPdf(DEST);
  25.     }
  26.    
  27.     public void createPdf(String dest) throws FileNotFoundException, DocumentException {
  28.         Rectangle pagesize = PageSize.LETTER;
  29.         Document document = new Document(pagesize);
  30.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  31.         document.open();
  32.         PdfContentByte canvas = writer.getDirectContent();
  33.         for (float x = 0; x < pagesize.getWidth(); ) {
  34.             for (float y = 0; y < pagesize.getHeight(); ) {
  35.                 canvas.circle(x, y, 1f);
  36.                 y += 72f;
  37.             }
  38.             x += 72f;
  39.         }
  40.         canvas.fill();
  41.         document.close();
  42.     }
  43.  
  44. }

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