· 

Creating a PowerPoint Document in Java Application

It is not recommended to create a new PowerPoint document directly by programming, because designing a beautiful layout through code may take more time than to operate visually. However, if you do have the requirement to do it programmatically, this article might be of help.

 

This scenario requires Free Spire.Presentation for Java, which is a class library supporting creation and manipulation of PowerPoint documents in Java applications. The following code snippets will show you -

  • How to create a PowerPoint slide
  • How to set the background image of a slide
  • How to insert text
  • How to insert an image
  •  How to insert a bullet list with customized bullet point 

Installing Spire.Presentation.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</
repositories>
<
dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>2.6.1</version>
    </dependency>
</
dependencies>

Using the code

import com.spire.presentation.*;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

public class CreatePowerPoint {

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

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

       
//Set the slide size type
       
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

       
//Get the height and width of slide
       
double slideHeight = presentation.getSlideSize().getSize().getHeight();
       
double slideWidth = presentation.getSlideSize().getSize().getWidth();

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

       
//Set the background image
       
String bgImage = "C:\\Users\\Administrator\\Desktop\\bg.jpg";
        setBackgroundImage(slide, bgImage);

       
//Insert a rectangle
       
Rectangle2D titleRect = new Rectangle2D.Float(100, 100, 450, 50);
        IAutoShape titleShape = appendShape(slide, ShapeType.
RECTANGLE, titleRect, new Color(225, 225, 225, 0), new Color(225, 225, 225, 0));

       
//Add text to rectangle
       
String title = "Top 6 Web Design Tips";
        TextFont titleFont =
new TextFont("Bodoni MT Black");
        insertTextToShape(titleShape, title, titleFont,
36f, Color.BLACK);

       
//Insert a picture bullet
       
String iconPath = "C:\\Users\\Administrator\\Desktop\\icon.png";
        Rectangle2D listBounds =
new Rectangle2D.Float(120, (float) (slideHeight - 200) / 2, 400, 200);
        String[] listContent =
new String[]{

               
" Provide a clear, concise navigation method",
               
" Make it intuitively easy to use",
               
" Keep it consistent",
               
" Degrade gracefully",
               
" Write it with your target audience in mind",
               
" Make sure its compliant"
       
};
        appendPictureList(slide, iconPath, listContent, listBounds);

       
//Insert an image
       
String feateredImage = "C:\\Users\\Administrator\\Desktop\\Website-Design-in-Parramatta.jpg";
        Rectangle2D imageBounds =
new Rectangle2D.Double(slideWidth / 2 + 30, (slideHeight - 290) / 2 + 40, 350, 290);
        appendImage(slide, feateredImage, imageBounds);

       
//Save to file
       
presentation.saveToFile("CreatePowerPoint.pptx", FileFormat.PPTX_2013);
    }

   
//Set background image of slide
   
public static void setBackgroundImage(ISlide slide, String imagePath) throws Exception {

        BufferedImage image = ImageIO.read(
new FileInputStream(imagePath));
        IImageData imageData = slide.getPresentation().getImages().append(image);
        slide.getSlideBackground().setType(BackgroundType.
CUSTOM);
        slide.getSlideBackground().getFill().setFillType(FillFormatType.
PICTURE);
        slide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.
STRETCH);
        slide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
    }

   
//Append shape to slide
   
public static IAutoShape appendShape(ISlide slide, ShapeType shapeType, Rectangle2D shapeBounds, Color lineColor, Color fillColor) throws Exception {

        IAutoShape shape = slide.getShapes().appendShape(shapeType, shapeBounds);
        shape.getShapeStyle().getLineColor().setColor(lineColor);
        shape.getFill().setFillType(FillFormatType.
SOLID);
        shape.getFill().getSolidColor().setColor(fillColor);
       
return shape;
    }

   
//Insert text to shape
   
public static void insertTextToShape(IAutoShape shape, String text, TextFont font, float fontHeight, Color fontColor) throws DocumentEditException {

        shape.getTextFrame().setText(text);
        PortionEx portionEx = shape.getTextFrame().getTextRange().getParagraph().getFirstTextRange();
        portionEx.getFill().setFillType(FillFormatType.
SOLID);
        portionEx.getFill().getSolidColor().setColor(fontColor);
        portionEx.setLatinFont(font);
        portionEx.setFontHeight(fontHeight);
    }

   
//Append image in slide
   
public static IEmbedImage appendImage(ISlide slide, String imagePath, Rectangle2D imageBounds) throws Exception {

        ShapeType shapeType = ShapeType.
RECTANGLE;
        BufferedImage image = ImageIO.read(
new FileInputStream(imagePath));
        IImageData imageData = slide.getPresentation().getImages().append(image);
        IEmbedImage iEmbedImage = slide.getShapes().appendEmbedImage(shapeType, imageData, imageBounds);
        iEmbedImage.getLine().setFillType(FillFormatType.
NONE);
       
return iEmbedImage;
    }

   
//Append picture list in slide
   
public static void appendPictureList(ISlide slide, String iconPath, String[] listContent, Rectangle2D listBounds) throws Exception {

        IAutoShape autoShape = slide.getShapes().appendShape(ShapeType.
RECTANGLE, listBounds);
        autoShape.getTextFrame().getParagraphs().clear();
        autoShape.getFill().setFillType(FillFormatType.
NONE);
        autoShape.getLine().setFillType(FillFormatType.
NONE);

       
for (int i = 0; i < listContent.length; i++) {

            ParagraphEx paragraph =
new ParagraphEx();
            autoShape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(listContent[i]);
            paragraph.getTextRanges().get(
0).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(
0).getFill().getSolidColor().setColor(Color.black);
            paragraph.setBulletType(TextBulletType.
PICTURE);
            BufferedImage image = ImageIO.read(
new
File(iconPath));
            paragraph.getBulletPicture().setEmbedImage(slide.getPresentation().getImages().append(image));
        }

    }

}

Output

Write a comment

Comments: 0