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

When presenting a slideshow to an audience, you probably want to explain more than what appears on the slides. Adding speaker notes to your presentation is a great way to help you remember what needs to be said during the slideshow. In this article, you will learn how to add, read or delete speaker notes in PowerPoint 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>10.11.4</version>
    </dependency>
</dependencies>

Add Speaker Notes in PowerPoint in Java

The following are the main steps to add speaker notes to a PowerPoint document:

  • Create a Presentation instance and load a PowerPoint document using Presentation.loadFromFile() method.
  • Get the slide that you want to add speaker notes to using Presentation.getSlides().get(slideIndex) method.
  • Add a notes slide to the slide using ISlide.addNotesSlides() method.
  • Create a ParagraphEx instance.
  • Set text for the paragraph through ParagraphEx.setText() method, then append the paragraph to the notes slide using NotesSlide.getNotesTextFrame().getParagraphs().append() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

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

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);
        //Add a notes slide
        NotesSlide notesSlide = slide.addNotesSlide();

        //Add a paragraph to the notes slide
        ParagraphEx paragraph = new ParagraphEx();
        paragraph.setText("Tips for making effective presentations:");
        notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);

        //Add a paragraph to the notes slide
        paragraph = new ParagraphEx();
        paragraph.setText("Use the slide master feature to create a consistent and simple design template.");
        notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);

        //Add a paragraph to the notes slide
        paragraph = new ParagraphEx();
        paragraph.setText("Simplify and limit the number of words on each screen.");
        notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);

        //Add a paragraph to the notes slide
        paragraph = new ParagraphEx();
        paragraph.setText("Use contrasting colors for text and background.");
        notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);

        //Set the bullet type and bullet style for specific paragraphs on the notes slide 
        for (int i = 1; i < notesSlide.getNotesTextFrame().getParagraphs().getCount();i++)
        {
            notesSlide.getNotesTextFrame().getParagraphs().get(i).setBulletType(TextBulletType.NUMBERED);
            notesSlide.getNotesTextFrame().getParagraphs().get(i).setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);
        }

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

Java: Add, Read or Delete Speaker Notes in PowerPoint

Read Speaker Notes in PowerPoint in Java

The following are the steps to read the speaker notes on a PowerPoint slide:

  • Create a Presentation instance and load the PowerPoint document using Presentation.loadFromFile() method.
  • Get the slide that you want to read speaker notes from using Presentation.getSlides().get(slideIndex) method.
  • Get the notes slide from the slide using ISlide.getNotesSlide() method.
  • Get the speaker notes from the notes slide using NotesSlide.getNotesTextFrame().getText() method.
  • Create a StringBuilder instance.
  • Append the speaker notes to the string builder, then write them into a .txt file.
  • Java
import com.spire.presentation.ISlide;
import com.spire.presentation.NotesSlide;
import com.spire.presentation.Presentation;

import java.io.FileWriter;

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

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

        //Get the notes slide from the first slide
        NotesSlide notesSlide = slide.getNotesSlide();
        //Get the speaker notes from the notes slide
        String notes = notesSlide.getNotesTextFrame().getText();

        //Create a StringBuilder instance
        StringBuilder sb = new StringBuilder();
        //Append the speaker notes to the string builder
        sb.append(notes + "\n");

        //Save the speaker notes to a .txt file
        FileWriter writer = new FileWriter("SpeakerNotes.txt");
        writer.write(sb.toString());
        writer.flush();
        writer.close();
    }
}

Java: Add, Read or Delete Speaker Notes in PowerPoint

Delete Speaker Notes in PowerPoint in Java

The following are the steps to delete speaker notes from a PowerPoint slide:

  • Create a Presentation instance and load the PowerPoint document using Presentation.loadFromFile() method.
  • Get the slide that you want to delete speaker notes from using Presentation.getSlides().get(slideIndex) method.
  • Get the notes slide from the slide using ISlide.getNotesSlide() method.
  • Remove a specific speaker note from the notes slide using NotesSlide.getNotesTextFrame().getParagraphs().removeAt(paragraphIndex) method or remove all the speaker notes from the notes slide using NotesSlide.getNotesTextFrame().getParagraphs().clear() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.NotesSlide;
import com.spire.presentation.Presentation;

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

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);
        //Get the notes slide from the slide
        NotesSlide notesSlide = slide.getNotesSlide();

        //Remove a specific speaker note from notes slide
        notesSlide.getNotesTextFrame().getParagraphs().removeAt(1);

        //Remove all the speaker notes from notes slide
        notesSlide.getNotesTextFrame().getParagraphs().clear();

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

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.

Add Shapes to PowerPoint in Java

2019-01-03 06:34:39 Written by Koohji

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

page 74