You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Jason E. McCollough (JIRA)" <ji...@apache.org> on 2011/05/19 09:02:47 UTC

[jira] [Created] (PDFBOX-1015) Draw Rotated Box

Draw Rotated Box
----------------

                 Key: PDFBOX-1015
                 URL: https://issues.apache.org/jira/browse/PDFBOX-1015
             Project: PDFBox
          Issue Type: Improvement
          Components: PDModel
    Affects Versions: 1.5.0
            Reporter: Jason E. McCollough
            Priority: Minor


While developing an app using PDFBox, I had a need to draw a rotated box.  I did not see a function in the PDPageContentStream, so I built my own.  The code is below.  Perhaps it could be included as a method in the PDPageContentStream class in a future release?  If such a method already exists somewhere, I apologize for creating white noise with this post.

    /**
     * Method to draw a box, rotated counterclockwise about the start point.
     * @param cs ContentStream to which the box should be added.
     * @param rotation Rotation, in degrees.
     * @param startX Bottom-left (pre-rotation) x-coordinate
     * @param startY Bottom-left (pre-rotation) y-coordinate
     * @param width Box width
     * @param height Box height
     * @param lineWidth Line Width
     */
    private void drawRotatedBox(
            PDPageContentStream cs,
            float rotation,
            float startX,
            float startY,
            float width,
            float height,
            float lineWidth) {
        logger.trace("Entered Method: {drawRotatedBox}");
        
        try {
            double rads = Math.toRadians(rotation);
            cs.setLineWidth(lineWidth);
            float[][] coords = new float[4][2];
            //bottom-left
            coords[0][0] = startX;
            coords[0][1] = startY;
            //bottom-right
            coords[1][0] = coords[0][0]+(float)(Math.cos(rads)*width);
            coords[1][1] = coords[0][1]+(float)(Math.sin(rads)*width);
            //top-right
            coords[2][0] = coords[1][0]-(float)(Math.sin(rads)*height);
            coords[2][1] = coords[1][1]+(float)(Math.cos(rads)*height);
            //top-left
            coords[3][0] = coords[2][0]-(float)(Math.cos(rads)*width);
            coords[3][1] = coords[2][1]-(float)(Math.sin(rads)*width);
            
            //draw the box
            cs.moveTo(coords[0][0], coords[0][1]);
            cs.lineTo(coords[1][0], coords[1][1]);
            cs.lineTo(coords[2][0], coords[2][1]);
            cs.lineTo(coords[3][0], coords[3][1]);
            //close the box.  shift +/- 1/2 the line width to ensure complete closure
            cs.lineTo(coords[0][0]+(lineWidth/2), coords[0][1]-(lineWidth/2));
            
//            Display coordinate map during debugging
//            for (int i=0; i<4; i++){
//                int j=0;
//                    logger.trace("x: " + coords[i][j] + "; y: " + coords[i][j+1]);
//            }
            
            coords=null;  //gc
            cs.stroke();
        } catch (IOException ioe) {
            logger.error("Problem occurred while drawing the rotated box.");
        }

        logger.trace("Method Complete: {drawRotatedBox}");
    }



--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira