// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java // For complete examples and data files, please go to // https://github.com/aspose-words/Aspose.Words-for-Java // The shape renderer is retrieved using this method. This is made into a // separate object from the shape as it internally // caches the rendered shape. ShapeRenderer r = shape.getShapeRenderer(); // Find the size that the shape will be rendered to at the specified scale and // resolution. Dimension shapeSizeInPixels = r.getSizeInPixels(1.0f, 96.0f); // Rotating the shape may result in clipping as the image canvas is too small. // Find the longest side // and make sure that the graphics canvas is large enough to compensate for // this. int maxSide = Math.max(shapeSizeInPixels.width, shapeSizeInPixels.height); BufferedImage image = new BufferedImage((int) (maxSide * 1.25), (int) (maxSide * 1.25), BufferedImage.TYPE_INT_ARGB); // Rendering to a graphics object means we can specify settings and // transformations to be applied to // the shape that is rendered. In our case we will rotate the rendered shape. Graphics2D gr = (Graphics2D) image.getGraphics(); // Clear the shape with the background color of the document. gr.setBackground(shape.getDocument().getPageColor()); gr.clearRect(0, 0, image.getWidth(), image.getHeight()); // Center the rotation using translation method below gr.translate(image.getWidth() / 8, image.getHeight() / 2); // Rotate the image by 45 degrees. gr.rotate(45 * Math.PI / 180); // Undo the translation. gr.translate(-image.getWidth() / 8, -image.getHeight() / 2); // Render the shape onto the graphics object. r.renderToSize(gr, 0, 0, shapeSizeInPixels.width, shapeSizeInPixels.height); ImageIO.write(image, "png", new File(dataDir + "TestFile.RenderToGraphics_out.png")); gr.dispose();