Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw graphs in PPT by Java

Shulou Source: shulou.com Published: 2022-06-03 09:48:15 10月01日 Update

This article is about how Java draws graphics in PPT. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

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

Tool: Free Spire.Presentation for Java

Jar file import method 1: download through the official website.

Step1: create a new Directory directory in the program and name it lib; and copy the Spire.Presentation.jar in the control package (available in the lib file under the unzipped control package) directly under that directory.

Step2: select the copied Spire.Presentation.jar file, right-click, and select "Add as library". Complete the reference.

Jar file import method 2: install through Maven warehouse.

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 {/ / create PowerPoint document 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 to be filled with 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 heart and set the 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 and set a 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,140125)) 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 upward 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 the document presentation.saveToFile ("AddShapes.pptx", FileFormat.PPTX_2010);}} Thank you for reading! This is the end of this article on "how to draw graphics in Java in PPT". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Graphics files methods content controls documents more directories articles references good practical triangles pentagram warehouse code location monochrome available in pattern Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology Redmi Apple Xiaomi