Java drawing graphics in PPT

Keywords: Java Programming Maven

Microsoft PowerPoint can support the insertion of various types of graphics in slides and can set graphic fill, line color, graphic size, location, etc. The following will demonstrate the method of drawing graphics in PPT through Java programming.

Tools: free flame.presentation for Java V 2.2.3

Jar file import method 1: through Download official website And import

Step 1: create the directory file lib, and import the spirit.presentation.jar file (you can directly copy the file to Lib)

Step 2: select Spire.Presentation.jar, right-click and select Add as library. Complete reference

Jar file import method 2: install through Maven warehouse. See this article for details Article Example

Java code example (for reference)

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class CreateShapes_PPT {

    public static void main(String[] args) throws Exception {

        //Establish PowerPoint File
        Presentation presentation = new Presentation();

        //Add a triangle and set the monochrome fill
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(115, 130, 100, 100));
        shape.getFill().setFillType(FillFormatType.SOLID);
        shape.getFill().getSolidColor().setColor(Color.orange);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //Add an ellipse and set the picture fill
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.ELLIPSE, new Rectangle2D.Double(290, 130, 150, 100));
        shape.getFill().setFillType(FillFormatType.PICTURE);
        shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
        BufferedImage image = ImageIO.read(new File("logo.png"));
        shape.getFill().getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(image));
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //Add a cardioid and set up a hatch
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.HEART, new Rectangle2D.Double(515, 130, 130, 100));
        shape.getFill().setFillType(FillFormatType.PATTERN);
        shape.getFill().getPattern().setPatternType(PatternFillType.LARGE_GRID);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //Add a pentagram star and set the gradient fill
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.FIVE_POINTED_STAR, new Rectangle2D.Double(115, 300, 115, 115));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.RED);
        shape.getFill().getGradient().getGradientStops().append(1, KnownColors.LIGHT_SALMON);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //Add a rectangle and set the gradient fill
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.HEXAGON, new Rectangle2D.Double(290, 300, 140, 125));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.LIGHT_PINK);
        shape.getFill().getGradient().getGradientStops().append(1, KnownColors.LIGHT_SKY_BLUE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //Add an up arrow and set the gradient fill
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.DOUBLE_WAVE, new Rectangle2D.Double(515, 300, 130, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(1f, KnownColors.OLIVE);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.POWDER_BLUE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);
        
        //Save document
        presentation.saveToFile("AddShapes.pptx", FileFormat.PPTX_2010);
    }
}

Drawing effect:

 

Note:

1. There are many kinds of graphics supported by this library, such as the following figure:

 

2. The above libraries can be used in an environment without Microsoft PowerPoint

 

(end of this paper)

Posted by The.Pr0fess0r on Tue, 03 Dec 2019 13:22:58 -0800