Java: Create a SmartArt in PowerPoint

2022-02-24 08:00:00 Written by Koohji

A SmartArt graphic is a visual representation of information and ideas. It can turn ordinary text into the predefined graphic, which makes the text information easier to understand. In this article, you will learn how to create a SmartArt in PowerPoint and custom its layout programmatically using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

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

Create a SmartArt in PowerPoint

The detailed steps are as follows.

  • Create a Presentation object.
  • Get a specified slide using Presentation.getSlides().get() method.
  • Insert a SmartArt into the specified slide using ISlide.getShapes().appendSmartArt() method.
  • Set the style and color of the SmartArt using ISmartArt.setStyle() method and ISmartArt.setColorStyle() method.
  • Loop through the nodes in SmartArt and remove all default nodes using ISmartArt.getNodes().removeNode() method.
  • Add a parent node to the SmartArt using ISmartArt.getNodes().addNode() method, and then add 4 child nodes using ISmartArtNode.getChildNodes().addNode() method.
  • Add text to each node using ISmartArtNode.getTextFrame().setText() method.
  • Get the text range of the above added text using ISmartArtNode.getTextFrame().getTextRange() method, and then set the text font size using PortionEx.setFontHeight() method.
  • Save the document to file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.diagrams.*;

public class AddSmartArt {

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

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

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

        //Insert a SmartArt (Organization Chart) into the slide
        ISmartArt smartArt = slide.getShapes().appendSmartArt(60, 60, 500, 400, SmartArtLayoutType.ORGANIZATION_CHART);

        //Set the style and color of SmartArt
        smartArt.setStyle(SmartArtStyleType.MODERATE_EFFECT);
        smartArt.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_4_TO_5);

        //Remove all default nodes
        for (Object a : smartArt.getNodes()) {
            smartArt.getNodes().removeNode(0);
        }

        //Add a parent node
        ISmartArtNode node1 = smartArt.getNodes().addNode();

        //Add 4 child nodes
        ISmartArtNode node1_1 = node1.getChildNodes().addNode();
        ISmartArtNode node1_2 = node1.getChildNodes().addNode();
        ISmartArtNode node1_3 = node1.getChildNodes().addNode();
        ISmartArtNode node1_4 = node1.getChildNodes().addNode();

        //Add text to each node and set its font size
        node1.getTextFrame().setText("General Manager");
        node1.getTextFrame().getTextRange().setFontHeight(14f);
        node1_1.getTextFrame().setText("Marketing Manager");
        node1_1.getTextFrame().getTextRange().setFontHeight(12f);
        node1_2.getTextFrame().setText("Operation Manager");
        node1_2.getTextFrame().getTextRange().setFontHeight(12f);
        node1_3.getTextFrame().setText("Human Resource Manager");
        node1_3.getTextFrame().getTextRange().setFontHeight(12f);
        node1_4.getTextFrame().setText("Account Manager");
        node1_4.getTextFrame().getTextRange().setFontHeight(12f);

        //Save the document
        presentation.saveToFile("SmartArt.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }
}

Java: Create a SmartArt in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

In comparison with PowerPoint documents, image files are easier to view because they can be opened on almost any device without the need for specific software. If you want to make your PowerPoint documents accessible on a wide range of devices, you can convert them to images. In this article, we will explain how to convert PowerPoint documents to various image formats in Java using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

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

Convert PowerPoint Documents to JPG or PNG Images in Java

The following are the steps to convert a PowerPoint document to JPG or PNG image:

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Iterate through all slides in the PowerPoint document.
  • Save each slide as a BufferedImage object using ISlide.saveAsImage() method.
  • Save the BufferedImage object to PNG or JPG file using ImageIO.write() method.
  • 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 ConvertPowerPointToPngOrJpg {
    public static void main(String []args) throws Exception {
        //Create a Presentation instance
        Presentation presentation = new Presentation();
        //Load a PowerPoint document
        presentation.loadFromFile("Sample.pptx");

        //Iterate through all slides in the PowerPoint document
        for(int i = 0; i < presentation.getSlides().getCount(); i++)
        {
            ISlide slide = presentation.getSlides().get(i);
            //Save each slide as PNG image
            BufferedImage image = slide.saveAsImage();
            String fileName = String.format("ToImage-%1$s.png", i);
            ImageIO.write(image, "PNG",new File(fileName));
        }
    }
}

Java: Convert PowerPoint to Images (PNG, JPG, TIFF, SVG)

Convert PowerPoint Documents to TIFF Images in Java

The following are the steps to convert a PowerPoint document to TIFF image:

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Convert the PowerPoint document to TIFF image using Presentation.saveToFile(String, FileFormat) method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

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

        //Convert the PowerPoint document to TIFF image
        presentation.saveToFile("toTIFF.tiff", FileFormat.TIFF);
    }
}

Java: Convert PowerPoint to Images (PNG, JPG, TIFF, SVG)

Convert PowerPoint Documents to SVG Images in Java

The following are the steps to convert a PowerPoint document to SVG images:

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Convert the PowerPoint document to SVG and save the results into an ArrayList of byte arrays using Presentation.saveToSVG() method.
  • Iterate through the byte arrays in the ArrayList.
  • Get the current byte array using ArrayList.get(int) method.
  • Initialize an instance of FileOutputStream class and save the byte array to an SVG file using FileOutputStream.write() method.
  • Java
import com.spire.presentation.Presentation;

import java.io.FileOutputStream;
import java.util.ArrayList;

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

        //Convert the PowerPoint document to SVG and save the results into an ArrayList of byte arrays
        ArrayList<byte[]> svgBytes =(ArrayList) presentation.saveToSVG();
        int len = svgBytes.size();
        //Iterate through the byte arrays in the ArrayList
        for (int i = 0; i < len; i++)
        {
            //Get the current byte array
            byte[] bytes = svgBytes.get(i);
            //Specify the output file name
            String fileName= String.format("ToSVG-%d.svg", i);
            //Create a FileOutputStream instance
            FileOutputStream stream = new FileOutputStream(fileName);
            //Save the byte array to an SVG file
            stream.write(bytes);
        }
    }
}

Java: Convert PowerPoint to Images (PNG, JPG, TIFF, SVG)

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Create Chart in PowerPoint in Java

2019-01-04 08:11:22 Written by Koohji

This article demonstrates how to create a chart in a PowerPoint document using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.pdf.tables.table.*;
import com.spire.presentation.charts.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
import java.lang.Object;


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

        //Add a column clustered chart
        Rectangle2D.Double rect = new Rectangle2D.Double(40, 100, 550, 320);
        IChart chart = null;
        chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);

        //Set chart title
        chart.getChartTitle().getTextProperties().setText("Sales Report");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //Create a dataTable
        DataTable dataTable = new DataTable();
        dataTable.getColumns().add(new DataColumn("SalesPers", DataTypes.DATATABLE_STRING));
        dataTable.getColumns().add(new DataColumn("SaleAmt", DataTypes.DATATABLE_INT));
        dataTable.getColumns().add(new DataColumn("ComPct", DataTypes.DATATABLE_INT));
        dataTable.getColumns().add(new DataColumn("ComAmt", DataTypes.DATATABLE_INT));
        DataRow row1 = dataTable.newRow();
        row1.setString("SalesPers", "Joe");
        row1.setInt("SaleAmt", 250);
        row1.setInt("ComPct", 150);
        row1.setInt("ComAmt", 99);
        DataRow row2 = dataTable.newRow();
        row2.setString("SalesPers", "Robert");
        row2.setInt("SaleAmt", 270);
        row2.setInt("ComPct", 150);
        row2.setInt("ComAmt", 99);
        DataRow row3 = dataTable.newRow();
        row3.setString("SalesPers", "Michelle");
        row3.setInt("SaleAmt", 310);
        row3.setInt("ComPct", 120);
        row3.setInt("ComAmt", 49);
        DataRow row4 = dataTable.newRow();
        row4.setString("SalesPers", "Erich");
        row4.setInt("SaleAmt", 330);
        row4.setInt("ComPct", 120);
        row4.setInt("ComAmt", 49);
        DataRow row5 = dataTable.newRow();
        row5.setString("SalesPers", "Dafna");
        row5.setInt("SaleAmt", 360);
        row5.setInt("ComPct", 150);
        row5.setInt("ComAmt", 141);
        DataRow row6 = dataTable.newRow();
        row6.setString("SalesPers", "Rob");
        row6.setInt("SaleAmt", 380);
        row6.setInt("ComPct", 150);
        row6.setInt("ComAmt", 135);
        dataTable.getRows().add(row1);
        dataTable.getRows().add(row2);
        dataTable.getRows().add(row3);
        dataTable.getRows().add(row4);
        dataTable.getRows().add(row5);
        dataTable.getRows().add(row6);

        //Import data from dataTable to chart data
        for (int c = 0; c < dataTable.getColumns().size(); c++) {
            chart.getChartData().get(0, c).setText(dataTable.getColumns().get(c).getColumnName());
        }
        for (int r = 0; r < dataTable.getRows().size(); r++) {
            Object[] datas = dataTable.getRows().get(r).getArrayList();
            for (int c = 0; c < datas.length; c++) {
                chart.getChartData().get(r + 1, c).setValue(datas[c]);

            }
        }

        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));

        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));

        chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));
        chart.getSeries().get(2).getFill().setFillType(FillFormatType.SOLID);                    
      chart.getSeries().get(2).getFill().getSolidColor().setKnownColor(KnownColors.LIGHT_BLUE);

        //Set overlap
        chart.setOverLap(-50);

        //Set gap width
        chart.setGapDepth(200);

        //Save the document
        presentation.saveToFile("output/CreateChart.pptx", FileFormat.PPTX_2010);
    }
}

Create Chart in PowerPoint in Java

page 74