Search code examples
pdfclown

Is there an option to display grid, for programming positions of visible material on screen?


When doing PDF programming, while adding many visible material on screen (such as text, polygon drawing, picture, colors, border, etc.).

Is there a way to turn on or display grid (can be dots) for measuring the x & y axis positioning? If so, what objects should I be looking for in PDFClown?

I find it easier to measure the position locations and width/height of objects than spending time calculating the points and making mistake.

Thanks.

P.S. - Also, we don't have to print out the paper and put the plastic grid on it to do the measurement. Save papers and go green. ;-)


Solution

  • How about drawing a grid? Simply add operations for drawing it to the page content during development.

    E.g. you can do it like in this sample based on the PDF Clown sample HelloWorldSample.java:

    // 1. Instantiate a new PDF file!
    /*
     * NOTE: a File object is the low-level (syntactic) representation of a
     * PDF file.
     */
    org.pdfclown.files.File file = new org.pdfclown.files.File();
    
    // 2. Get its corresponding document!
    /*
     * NOTE: a Document object is the high-level (semantic) representation
     * of a PDF file.
     */
    Document document = file.getDocument();
    
    // 3. Insert the contents into the document!
    populate(document);
    
    // 3.5 Add a grid to the content
    addGrid(document);
    
    // 4. Serialize the PDF file!
    file.save(new File(RESULT_FOLDER, "helloWorld-grid.pdf"), SerializationModeEnum.Standard);
    
    file.close();
    

    using the helper method addGrid:

    void addGrid(Document document)
    {
        for (Page page: document.getPages())
        {
            Dimension2D pageSize = page.getSize();
            PrimitiveComposer composer = new PrimitiveComposer(page);
            composer.beginLocalState();
    
            composer.setStrokeColor(new DeviceRGBColor(1, 0, 0));
            for (int x = 0; x < pageSize.getWidth(); x+=20)
            {
                composer.startPath(new Point2D.Float(x, 0));
                composer.drawLine(new Point2D.Double(x, pageSize.getHeight()));
            }
    
            for (int y = 0; y < pageSize.getHeight(); y+=20)
            {
                composer.startPath(new Point2D.Float(0, y));
                composer.drawLine(new Point2D.Double(pageSize.getWidth(), y));
            }
    
            composer.stroke();
    
            composer.end();
            composer.flush();
        }
    }
    

    This results in something like this:

    Sample result