Saturday, 12 October 2019 03:45

Auto Fit Text or Shape in PowerPoint in Java

This article demonstrates how to automatically shrink text to fit a shape or how to automatically resize a shape to fit text by using Spire.Presentation for Java.

import com.spire.presentation.*;

import java.awt.geom.Rectangle2D;

public class AutoFitTextOrShape {

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

        //create Presentation instance 
        Presentation presentation = new Presentation();

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

        //add a shape to slide 
        IAutoShape textShape1 = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50,50,200,80));

        //add text to shape
        textShape1.getTextFrame().setText("Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape.");

        //set the auto-fit type to normal, which means the text automatically shrinks to fit the shape when text overflows the shape
        textShape1.getTextFrame().setAutofitType(TextAutofitType.NORMAL);

        //add another shape to slide 
        IAutoShape textShape2 = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(350, 50, 200, 80));
        textShape2.getTextFrame().setText("Resize shape to fit text.");

        //set the auto-fit type to shape, which means the shape size automatically decreases or increases to fit text
        textShape2.getTextFrame().setAutofitType(TextAutofitType.SHAPE);

        //save to file 
        presentation.saveToFile("output/AutoFit.pptx", FileFormat.PPTX_2013);
    }
}

Auto Fit Text or Shape in PowerPoint in Java

Published in Paragraph and Text
Tuesday, 10 September 2019 07:51

Replace Text in PowerPoint in Java

This article demonstrates how to replace text in an exising PowerPoint document with new text using Spire.Presentation for Java.

import com.spire.presentation.*;

import java.util.HashMap;
import java.util.Map;

public class ReplaceText {

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

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

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

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

        //create a Map object
        Map map = new HashMap();

        //add several pairs of keys and values to the map
        map.put("#name#","John Smith");
        map.put("#age#","28");
        map.put("#address#","Oklahoma City, United States");
        map.put("#tel#","333 123456");
        map.put("#email#","johnsmith@outlook.com");

        //replace text in the slide
        replaceText(slide,map);

        //save to another file
        presentation.saveToFile("output/ReplaceText.pptx", FileFormat.PPTX_2013);
    }

    /**
     * Replace text within a slide
     * @param slide Specifies the slide where the replacement happens
     * @param map Where keys are existing strings in the document and values are the new strings to replace the old ones
     */
    public static void replaceText(ISlide slide, Map map) {

        for (Object shape : slide.getShapes()
        ) {
            if (shape instanceof IAutoShape) {

                for (Object paragraph : ((IAutoShape) shape).getTextFrame().getParagraphs()
                ) {
                    ParagraphEx paragraphEx = (ParagraphEx)paragraph;
                    for (String key : map.keySet()
                    ) {
                        if (paragraphEx.getText().contains(key)) {

                            paragraphEx.setText(paragraphEx.getText().replace(key, map.get(key)));
                         }
                    }
                }
            }
        }
    }
}

Replace Text in PowerPoint in Java

Published in Paragraph and Text
Tuesday, 27 August 2019 06:07

Insert HTML String in PowerPoint in Java

This article demonstrates how to render text with simple HTML tags to formatted text in a presentation slide by using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.Presentation;
import com.spire.presentation.ShapeType;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

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

        //create a Presentation object
        Presentation ppt = new Presentation();
        
        //add a shape to the first slide
        IAutoShape shape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50, 50, 400, 100));
        shape.getFill().setFillType(FillFormatType.NONE);

        //clear the default paragraph
        shape.getTextFrame().getParagraphs().clear();

        //define html string
        String htmlString = "<ul>" +
                            "<li style=\"color:blue\">Spire.Presentation for Java</li>" +
                            "<li style=\"color:green\">Spire.PDF for Java</li>" +
                            "<li style=\"color:gray\">Spire.Doc for Java</li>" +
                            "<li style=\"color:red\">Spire.Barcode for Java</li>" +
                            "</ul>";

        //insert html string in the shape
        shape.getTextFrame().getParagraphs().addFromHtml(htmlString);

        //save to file
        ppt.saveToFile("output/InsertHtml.pptx", FileFormat.PPTX_2013);
    }
}

Insert HTML String in PowerPoint in Java

Published in Paragraph and Text

Making your slides easy to read is vital to creating an effective PowerPoint presentation. One of the most common ways of doing this is to format the text as a bulleted or numbered list. A list can help organize information in a neat manner as well as attract the reader’s attention. In this article, you will learn how to create numbered lists and bulleted lists in a PowerPoint slide 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>

Create a Numbered List in PowerPoint in Java

Spire.Presentation supports adding numerals or bullet points in front of paragraphs to create a numbered or bulleted list. To specify the bullet type, use ParagraphEx.setBulletType() method. The following are the steps to create a numbered list in a PowerPoint slide using Spire.Presentation for Java.

  • Create a Presentation object.
  • Get the first slide using Presentation.getSlides().get() method.
  • Append a shape to the slide using ISlide.getShapes().appendShape() method.
  • Specify list content inside a String list.
  • Create paragraphs based on the list content, and set the bullet type of these paragraphs to NUMBERED using ParagraphEx.setBulletType() method.
  • Set the numbered bullet style using ParagraphEx.setBulletStyle() method.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

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

public class CreateNumberedList {

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

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

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

        //Append a shape to the slide and set shape style
        Rectangle2D rect = new Rectangle2D.Double(50, 50, 300, 200);
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
        shape.getLine().setFillType(FillFormatType.NONE);
        shape.getFill().setFillType(FillFormatType.NONE);

        //Add text to the default paragraph
        ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
        titleParagraph.setText("Required Web Development Skills:");
        titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
        titleParagraph.setAlignment(TextAlignmentType.LEFT);

        //Specify list content
        String[] listContent = new String[] {
                " Command-line Unix",
                " Vim",
                " HTML",
                " CSS",
                " Python",
                " JavaScript",
                " SQL"
        };

        //Create a numbered list
        for(int i = 0; i < listContent.length; i ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.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.NUMBERED);
            paragraph.setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);
        }

        //Save the document
        presentation.saveToFile("NumberedList.pptx", FileFormat.PPTX_2013);
    }
}

Java: Create Numbered or Bulleted Lists in PowerPoint

Create a Bulleted List with Symbol Bullets in PowerPoint in Java

The process of creating a bulleted list using symbol bullets is very similar to that of creating a numbered list. The only difference is that you need to set the bullet type to SYMBOL. The following are the steps.

  • Create a Presentation object.
  • Get the first slide using Presentation.getSlides().get() method.
  • Append a shape to the slide using ISlide.getShapes().appendShape() method.
  • Specify list content inside a String list.
  • Create paragraphs based on the list content, and set the bullet type of these paragraphs to SYMBOL using ParagraphEx.setBulletType() method.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

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

public class CreateBulletedListWithSymbol {

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

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

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

        //Append a shape to the slide and set shape style
        Rectangle2D rect = new Rectangle2D.Double(50, 50, 350, 200);
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
        shape.getLine().setFillType(FillFormatType.NONE);
        shape.getFill().setFillType(FillFormatType.NONE);

        //Add text to the default paragraph
        ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
        titleParagraph.setText("Computer Science Subjects:");
        titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
        titleParagraph.setAlignment(TextAlignmentType.LEFT);

        //Specify list content
        String[] listContent = new String[] {
                " Data Structure",
                " Algorithm",
                " Computer Networks",
                " Operating System",
                " Theory of Computations",
                " C Programming",
                " Computer Organization and Architecture"
        };

        //Create a bulleted list with symbol bullets
        for(int i = 0; i < listContent.length; i ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.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.SYMBOL);
        }

        //Save the document
        presentation.saveToFile("SymbolBullets.pptx", FileFormat.PPTX_2013);
    }
}

Java: Create Numbered or Bulleted Lists in PowerPoint

Create a Bulleted List with Image Bullets in PowerPoint in Java

To use an image as bullet points, you need to set the bullet type to PICTURE and specify an image for the BulletPicture object. The following are the detailed steps.

  • Create a Presentation object.
  • Get the first slide using Presentation.getSlides().get() method.
  • Append a shape to the slide using ISlide.getShapes().appendShape() method.
  • Specify list content inside a String list.
  • Create paragraphs based on the list content, and set the bullet type of these paragraphs to PICTURE using ParagraphEx.setBulletType() method.
  • Set the image for the bullets using Paragraph.getBulletPicture().setEmbedImage() method.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

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

public class CreateBulletedListWithImage {

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

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

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

        //Append a shape to the slide and set shape style
        Rectangle2D rect = new Rectangle2D.Double(50, 50, 400, 180);
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
        shape.getLine().setFillType(FillFormatType.NONE);
        shape.getFill().setFillType(FillFormatType.NONE);

        //Add text to the default paragraph
        ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
        titleParagraph.setText("Project Task To-Do List:");
        titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
        titleParagraph.setAlignment(TextAlignmentType.LEFT);

        //Specify list content
        String[] listContent = new String[] {
                " Define projects and tasks you're working on",
                " Assign people to tasks",
                " Define the priority levels of your tasks",
                " Keep track of the progress status of your tasks",
                " Mark tasks as done when completed"
        };

        //Create a bulleted list with image bullets
        BufferedImage image = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\R-C25.png"));
        for(int i = 0; i < listContent.length; i ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.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);
            paragraph.getBulletPicture().setEmbedImage(presentation.getImages().append(image));
        }

        //Save the document
        presentation.saveToFile("ImageBullets.pptx", FileFormat.PPTX_2013);
    }
}

Java: Create Numbered or Bulleted Lists 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.

Published in Paragraph and Text
Friday, 11 August 2023 07:12

Java: Extract Text from PowerPoint

A PowerPoint presentation, developed by Microsoft Corporation, is a versatile file format used for creating visually captivating and interactive content. It includes rich features and multiple elements such as text and images, making it a powerful tool for various scenarios, such as business introductions and academic speeches. If you need to edit or manipulate the text of PowerPoint, programmatically extracting it and saving it to a new file is an effective approach. In this article, we will show you how to extract text  from PowerPoint 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>

Extract Text from the Whole PowerPoint File

Spire.Presentation for Java supports looping through all slides and extracting text from the paragraphs on each slide using ParagraphEx.getText() method. The detailed steps are as follows.

  • Create an object of Presentation class.
  • Load a sample presentation using Presentation.loadFromFile() method.
  • Create an object of StringBuilder class.
  • Loop through shapes in each slide and paragraphs in each shape.
  • Extract all text from these slides by calling ParagraphEx.getText() method and append the extracted text to StringBuilder object.
  • Create an object of FileWriter class, and write the extracted text to a new .txt file.
  • Java
import com.spire.presentation.*;

import java.io.*;

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

        //Create an object of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample presentation
        presentation.loadFromFile("sample.pptx");

        //Create a  StringBuilder object
        StringBuilder buffer = new StringBuilder();

        //Loop through each slide and extract text 
        for (Object slide : presentation.getSlides()) {
            for (Object shape : ((ISlide) slide).getShapes()) {
                if (shape instanceof IAutoShape) {
                    for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
                        buffer.append(((ParagraphEx) tp).getText()+"\n");
                    }
                }
            }
        }

        //Write the extracted text to a new .txt file
        FileWriter writer = new FileWriter("output/ExtractAllText.txt");
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
        presentation.dispose();
    }
}

Java: Extract Text from PowerPoint

Extract Text from the Specific Slide

Spire.Presentation for Java also supports users to extract text from the specific slide. Simply get the desired slide by calling Presentation.getSlides().get() method before extracting text from the paragraphs on it. The following are detailed steps.

  • Create an object of Presentation class.
  • Load a sample presentation using Presentation.loadFromFile() method.
  • Create an object of StringBuilder class.
  • Get the first slide of this file by calling Presentation.getSlides().get() method.
  • Loop through each shape and the paragraphs in each shape.
  • Extract the text from the first slide by calling ParagraphEx.getText() method and append the extracted text to StringBuilder object.
  • Create an object of FileWriter class, and write the extracted text to a new .txt file.
  • Java
import com.spire.presentation.*;

import java.io.*;

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

        //Create an object of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample presentation
        presentation.loadFromFile("sample.pptx");

        //Create a StringBuilder object
        StringBuilder buffer = new StringBuilder();

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

        //Loop through each paragraphs in each shape and extract text
        for (Object shape : Slide.getShapes()) {
            if (shape instanceof IAutoShape) {
                for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
                    buffer.append(((ParagraphEx) tp).getText()+"\n");
                }
            }
        }

        //Write the extracted text to a new .txt file
        FileWriter writer = new FileWriter("output/ExtractSlideText.txt");
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
        presentation.dispose();
    }
}

Java: Extract Text from 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.

Published in Paragraph and Text
Page 2 of 2