Wednesday, 11 November 2020

Create SmartArt graphics in PowerPoint and extract text from them in Java

SmartArt is a useful tool in PowerPoint which allows converting textual data into predefined graphics. In this article, I’ll show you how to create SmartArt graphics in PowerPoint and extract text from them in Java programmatically.

The Java API I used in this tutorial is FreeSpire.Presentation for Java. It is a professional API that enables developers to create, read, convert and print PowerPoint documents in Java Applications. Meanwhile, as an independent Java library, it doesn't need Microsoft PowerPoint to be installed on system.

BEFORE CODING

Before coding, please import Spire.Presentation.jar as reference in your project. If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.

<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>3.9.0</version>
    </dependency>

</dependencies>

The screenshot below is showed what it finally looks like:


USING THE CODE

Create SmartArt graphics

In the following code snippets, I create two SmartArt graphics: one is a default SmartArt, and the other

is a customized SmartArt. In the process of creating a SmartArt, we can set its location, color type, style

type and insert text in it.

import com.spire.presentation.*;

import com.spire.presentation.diagrams.*;

public class AddSmartArt {
   
public static void main(String[] args) throws Exception {
       
//Create a PowerPoint file, and get the first slide
       
Presentation ppt = new Presentation();
        ISlide slide =ppt.getSlides().get(
0);

       
//Insert the first default SmartArt
       
ISmartArt smartArt1 = slide.getShapes().appendSmartArt(50,50,350,350, SmartArtLayoutType.BASIC_CYCLE);//Insert the SmartArt into the specific location
       
smartArt1.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_4_TO_5);//Set the SmartArt color type
       
smartArt1.setStyle(SmartArtStyleType.INTENCE_EFFECT);//Set the SmartArt style type
       
ISmartArtNode smartArtNode1 = smartArt1.getNodes().get(0);         smartArtNode1.getTextFrame().setText("Information");//Gain the default nodes, and add relevant contents
       
smartArt1.getNodes().get(1).getTextFrame().setText("Communication");
        smartArt1.getNodes().get(
2).getTextFrame().setText("Analysis");
        smartArt1.getNodes().get(
3).getTextFrame().setText("Optimization");
        smartArt1.getNodes().get(
4).getTextFrame().setText("Execution");
       
//Insert the second SmartArt, and customize the node contents
       
ISmartArt smartArt2 = slide.getShapes().appendSmartArt(400,200,200,200,SmartArtLayoutType.BASIC_RADIAL);
        smartArt2.setColorStyle(SmartArtColorType.
DARK_2_OUTLINE);//set the style of the SmartArt
       
smartArt2.setStyle(SmartArtStyleType.MODERATE_EFFECT);//set the color of the SmartArt
        //Remove all default nodes
       
for (Object a : smartArt2.getNodes()) {
           smartArt2.getNodes().removeNode((ISmartArtNode) a);
        }
       
//Add a parent node
       
ISmartArtNode node2 = smartArt2.getNodes().addNode();
       
//Add three child nodes
       
ISmartArtNode node2_1 = node2.getChildNodes().addNode();
        ISmartArtNode node2_2 =node2.getChildNodes().addNode();
        ISmartArtNode node2_3 =node2.getChildNodes().addNode();
       
//add text to each node and set the font size
       
node2.getTextFrame().setText("Living Creature");
        node2.getTextFrame().getTextRange().setFontHeight(
16f);
        node2_1.getTextFrame().setText(
"Animal");
        node2_1.getTextFrame().getTextRange().setFontHeight(
14f);
        node2_2.getTextFrame().setText(
"Plant");
        node2_2.getTextFrame().getTextRange().setFontHeight(
14f);
        node2_3.getTextFrame().setText(
"Microbe");
        node2_3.getTextFrame().getTextRange().setFontHeight(
14f);
       
//Save the file
       
ppt.saveToFile("output/AddSmartArt.pptx",FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

Output


Extract text from SmartArt graphics

Here are some steps to extract text from SmartArt graphics.

Step 1: Load the PowerPoint document which needs to be extracted text

Step 2: Traverse through all the slides of the PPT file and find the SmartArt shapes.

Step 3: Create a new .txt file and insert the extracted text from SmartArt into it.

import com.spire.presentation.*;
import com.spire.presentation.diagrams.ISmartArt;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter; public class ExtractTextFromSmartArt {
   
public static void main(String[] args) throws Exception {
       
//Create a Presentation instance, and load a sample
       
Presentation presentation = new Presentation();
        presentation.loadFromFile(
"C:\\Users\\Test1\\Desktop\\AddSmartArt.pptx");
       
//Create a new .txt file to store the extracted text
       
String result = "output/extractTextOfSmartArt.txt";
        File file=
new File(result);
       
if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        FileWriter fw =
new FileWriter(file,true);
        BufferedWriter bw =
new BufferedWriter(fw);
        bw.write(
"Below is extracted text from SmartArt:" + "\r\n");
       
//Traverse through all the slides of the PPT file and find the SmartArt shapes.
       
for (int i = 0; i < presentation.getSlides().getCount(); i++)
        {
            
for (int j = 0; j < presentation.getSlides().get(i).getShapes().getCount(); j++)
            {
               
if (presentation.getSlides().get(i).getShapes().get(j) instanceof ISmartArt)
                { ISmartArt smartArt = (ISmartArt)presentation.getSlides().get(i).getShapes().get(j);

                   
//Extract text from SmartArt and append to the StringBuilder object
                   
for (int k = 0; k < smartArt.getNodes().getCount(); k++)
                    {
                        bw.write(smartArt.getNodes().get(k).getTextFrame().getText() +
"\r\n");
                    }
                }
            }
        }
        bw.flush();
        bw.close();
        fw.close();
    }
}

Output



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...