Image and Shapes

Image and Shapes (7)

Wednesday, 11 September 2019 09:38

Group Shapes in PowerPoint in Java

Written by support iceblue

This article demonstrates how to group shapes in a PowerPoint document using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class GroupShapes {
    public static void main(String[] args) throws Exception {
        //create a PowerPoint document
        Presentation ppt = new Presentation();
        //get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //add a rectangle shape to the slide
        IShape rectangle = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(20,100,200,40));
        rectangle.getFill().setFillType(FillFormatType.SOLID);
        rectangle.getFill().getSolidColor().setKnownColor(KnownColors.GOLD);
        rectangle.getLine().setWidth(0.1f);

        //add a ribbon shape to the slide
        IShape ribbon = slide.getShapes().appendShape(ShapeType.RIBBON_2, new Rectangle2D.Double(60, 75, 120, 80));
        ribbon.getFill().setFillType(FillFormatType.SOLID);
        ribbon.getFill().getSolidColor().setKnownColor(KnownColors.PURPLE);
        ribbon.getLine().setWidth(0.1f);

        //add the shapes to a list
        ArrayList list = new ArrayList();
        list.add((Shape)rectangle);
        list.add((Shape)ribbon);

        //group the shapes
        ppt.getSlides().get(0).groupShapes(list);

        //save the resultant document
        ppt.saveToFile("GroupShapes.pptx", FileFormat.PPTX_2013);
    }
}

Group Shapes in PowerPoint in Java

Alternative text (Alt Text) can help people with vision or cognitive impairments understand shapes, pictures or other graphical content. This article demonstrates how to set and get the alternative text of a shape in a PowerPoint document using Spire.Presentation for Java.

Set alternative text

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class SetAltText {
    public static void main(String[] args) throws Exception {
        //instantiate a Presentation object
        Presentation ppt = new Presentation();throws Exception 

        //add a shape to the first slide
        IAutoShape shape = ppt.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);

        //set alt text (title and description) for the shape
        shape.setAlternativeTitle("Triangle");
        shape.setAlternativeText("This is a triangle.");

        //save the resultant document
        ppt.saveToFile("Output.pptx", FileFormat.PPTX_2013);
    }
}

Set and Get Alternative Text (Alt Text) of PowerPoint Shapes in Java

Get alternative text

import com.spire.presentation.*;

public class GetAltText {
    public static void main(String[] args) throws Exception {
        //load PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Output.pptx");

        //get the first shape in the first slide
        IShape shape = ppt.getSlides().get(0).getShapes().get(0);

        //get the alt text (title and description) of the shape
        String altTitle = shape.getAlternativeTitle();
        String altDescription = shape.getAlternativeText();

        System.out.println("Title: " + altTitle);
        System.out.println("Description: " + altDescription);
    }
}

Set and Get Alternative Text (Alt Text) of PowerPoint Shapes in Java

Tuesday, 03 September 2019 06:59

Replace an Image in PowerPoint in Java

Written by support iceblue

This article demonstrates how to replace an existing image in a PowerPoint document with a new image by using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.IImageData;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class ReplaceImage {

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

        //create a Presentation object
        Presentation presentation= new Presentation();

        //load the sample PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //add an image to the image collection
        String imagePath = "C:\\Users\\Administrator\\Desktop\\Microsoft-PowerPoint-logo.jpg";
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath));
        IImageData image = presentation.getImages().append(bufferedImage);

        //get the shape collection from the first slide
        ShapeCollection shapes = presentation.getSlides().get(0).getShapes();

        //loop through the shape collection
        for (int i = 0; i < shapes.getCount(); i++) {

            //determine if a shape is a picture
            if (shapes.get(i) instanceof SlidePicture) {

                //fill the shape with a new image
               ((SlidePicture) shapes.get(i)).getPictureFill().getPicture().setEmbedImage(image);
            }
        }
        
        //save to file
        presentation.saveToFile("output/ReplaceImage.pptx", FileFormat.PPTX_2013);
    }
}

Replace an Image in PowerPoint in Java

Spire.Presentation for Java supports to save the whole presentation slides with table, chart and shape to image. And it also supports to save the single element in the presentation slides, such as chart, table and shape into image. This article will show you how to save shape and chart on Presentation Slide as image in Java.

import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ShapeAsImage {
    public static void main(String[] args) throws Exception {
        String inputFile = "Sample.pptx";
        String outputFile = "output/";

        //Create a Presentation instance and load sample file
        Presentation presentation = new Presentation();
        presentation.loadFromFile(inputFile);

        //Traverse every slides and every shapes on the slides
        int k = 0;
        for (int i = 0; i < presentation.getSlides().getCount(); i++) {
            ISlide slide = presentation.getSlides().get(i);
            for (int j = 0; j < slide.getShapes().getCount(); j++) {
                String fileName = outputFile + String.format("shapeToImage-%1$s.png", k);
                //Save every single shape as image
                BufferedImage image = slide.getShapes().saveAsImage(j);
                ImageIO.write(image, "PNG", new File(fileName));
                k++;
            }
        }
    }
}

Save Chart, Table and Shape on Presentation slides as Image in Java

Save Chart, Table and Shape on Presentation slides as Image in Java

Save Chart, Table and Shape on Presentation slides as Image in Java

Animation can make a PowerPoint document more dynamic. In this article, we'll introduce how to add animations to shapes in a PowerPoint document using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.animation.AnimationEffectType;

import java.awt.*;

import java.awt.geom.Rectangle2D;

public class SetAnimation {
    public static void main(String[] args) throws Exception {
        //create a PowrePoint document 
        Presentation ppt = new Presentation();
        //add a slide
        ISlide slide = ppt.getSlides().get(0);

        //Add a shape to slide
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.CUBE, new Rectangle2D.Double(50, 150, 150, 150));
        shape.getFill().setFillType(FillFormatType.SOLID);
        shape.getFill().getSolidColor().setColor(Color.orange);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //Add animation to the shape
        slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FADED_SWIVEL);

        //save the document
        ppt.saveToFile("AddAnimationToShape.pptx", FileFormat.PPTX_2013);
    }
}

Add Animations to Shapes in PowerPoint in Java

Thursday, 03 January 2019 06:34

Add Shapes to PowerPoint in Java

Written by support iceblue

This article demonstrates how to add a variety of shapes to a PowerPoint slide and how to fill the shapes with a solid color, a picture, a pattern or a gradient.

Entire Code

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 AddShapes {

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

        //create a PowerPoint document
        Presentation presentation = new Presentation();

        //append a Triangle and fill the shape with a solid color
        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);

        //append an Ellipse and fill the shape with a picture
        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("C:\\Users\\Administrator\\Desktop\\logo.png"));
        shape.getFill().getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(image));
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Heart and fill the shape with a pattern
        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.CROSS);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Five-Pointed Star and fill the shape with a gradient color
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.FIVE_POINTED_STAR, new Rectangle2D.Double(115, 300, 100, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.BLACK);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Rectangle and fill the shape with gradient colors
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(290, 300, 150, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.LIGHT_SKY_BLUE);
        shape.getFill().getGradient().getGradientStops().append(1, KnownColors.ROYAL_BLUE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Bent Up Arrow and fill the shape with gradient colors
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.BENT_UP_ARROW, 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("output/AddShapes.pptx", FileFormat.PPTX_2010);
    }
}

Output

Add Shapes to PowerPoint in Java

In this article, we're going to demonstrate how to insert and extract images from a PowerPoint document using Spire.Presentation for Java.

Insert images into a PowerPoint document

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class InsertImages {
    public static void main(String[] args) throws Exception {
            //Create a Presentation instance
            Presentation ppt = new Presentation();

            Rectangle2D rect = new Rectangle2D.Double(ppt.getSlideSize().getSize().getWidth() / 2 - 280, 140, 120, 120);

            //Get the first slide
            ISlide slide = ppt.getSlides().get(0);
            //Insert an image into the slide
            IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "a.jpg", rect);
            image.getLine().setFillType(FillFormatType.NONE);

            //Append a new slide
            slide = ppt.getSlides().append();
            //Insert an image into the slide
            image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "b.jpg", rect);
            image.getLine().setFillType(FillFormatType.NONE);

            //Save the document
            ppt.saveToFile("InsertImages.pptx", FileFormat.PPTX_2013);
    }
}

Insert and Extract Images from a PowerPoint Document in Java

Extract images from a PowerPoint document

Spire.Presentation for Java supports extracting all of the images in a PowerPoint document along with extracting images from a specific slide in a PowerPoint document.

The following code example shows how to extract all of the images in a PowerPoint document:

import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractImages {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the PowerPoint document
        ppt.loadFromFile("InsertImages.pptx");

        for (int i = 0; i < ppt.getImages().getCount(); i++) {
            //Extract images from the PowerPoint document
            BufferedImage image = ppt.getImages().get(i).getImage();
            ImageIO.write(image, "PNG",  new File(String.format("presentation/" + "extractImage-%1$s.png", i)));
        }
    }
}

Insert and Extract Images from a PowerPoint Document in Java

The following code example shows how to extract images from a specific slide in a PowerPoint document:

import com.spire.presentation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractImages {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the PowerPoint document
        ppt.loadFromFile("InsertImages.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        for(int i = 0; i< slide.getShapes().getCount(); i++)
        {
            IShape shape = slide.getShapes().get(i);
            //Extract images from the slide
            if(shape instanceof SlidePicture)
            {
                SlidePicture pic = (SlidePicture) shape;
                BufferedImage image = pic.getPictureFill().getPicture().getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
            if(shape instanceof PictureShape)
            {
                PictureShape ps = (PictureShape) shape;
                BufferedImage image = ps.getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
        }
    }
}

Insert and Extract Images from a PowerPoint Document in Java