Paragraph and Text

Paragraph and Text (9)

We have already introduced how to add animation effect to shape in PowerPoint, in this article, we will introduce how to add animation effect to paragraph in PowerPoint using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.animation.*;

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

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

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

        //Add a shape to the slide
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(150, 150, 450, 100));
        shape.getFill().setFillType(FillFormatType.SOLID);
        shape.getFill().getSolidColor().setColor(Color.gray);
        shape.getShapeStyle().getLineColor().setColor(Color.white);
        shape.appendTextFrame("This demo shows how to apply animation on paragraph in PPT document.");

        //Add animation effect to the first paragraph in the shape
        AnimationEffect animation = shape.getSlide().getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FLOAT);
        animation.setStartEndParagraphs(0, 0);

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

Output:

Add Animation Effect to Paragraph in PowerPoint in Java

Tuesday, 13 October 2020 08:34

Insert HTML with images into PowerPoint in Java

Written by support iceblue

We have already demonstrated how to insert HTML formatted text to a Presentation slide by using Spire.Presentation for Java. This article will introduce the way to insert HTML with images to PowerPoint and each html tag will be added to the slide as a separate shape.

import com.spire.presentation.*;
import com.spire.presentation.collections.*;

public class AddHTMLWithImage {
    public static void main(String[] args) throws Exception {
        //Create an instance of presentation document
        Presentation ppt = new Presentation();
        //Get the shapes on the first slide.
        ShapeList shapes = ppt.getSlides().get(0).getShapes();
        //Add contents to shapes from HTML codes, which includes text and image.
        shapes.addFromHtml("<html><div><p>E-iceblue</p>" +
                "<p><img src='https://cdn.e-iceblue.com/C:\\Users\\Test1\\Desktop\\logo.png'/></p>" +
                "<p>Spire.Presentation for Java</p></html>");

        //Save the document
        String result = "output/insertHtmlWithImage.pptx";
        ppt.saveToFile(result, FileFormat.PPTX_2013);
    }
}

Insert HTML with images into PowerPoint in Java

This article demonstrates how to search and highlight specific text in a PowerPoint document using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.Presentation;
import com.spire.presentation.TextHighLightingOptions;

import java.awt.*;

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

        //Get the first shape on the first slide
        IAutoShape shape = (IAutoShape)presentation.getSlides().get(0).getShapes().get(0);

        //Set highlight options
        TextHighLightingOptions options = new TextHighLightingOptions();
        options.setWholeWordsOnly(true);
        options.setCaseSensitive(true);

        //Highlight text
        shape.getTextFrame().highLightText("Spire", Color.yellow, options);

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

Output:

Search and Highlight Specific Text in PowerPoint in Java

There are two kinds of special indents styles for the paragraph on the presentation slides, first line and hanging. This article will demonstrate how to set the indents and spacing for the paragraph on presentation slide in Java applications.

import com.spire.presentation.*;

public class IndentStyle {
    public static void main(String[] args)  throws Exception{
        //Load the sample document
        Presentation presentation = new Presentation();
        presentation.loadFromFile("Indent.pptx");

        //get the shapes
        IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);

        //set the indent, margin and space for the first paragraph
        shape.getTextFrame().getParagraphs().get(0).setIndent(20);
        shape.getTextFrame().getParagraphs().get(0).setLeftMargin(10);
        shape.getTextFrame().getParagraphs().get(0).setSpaceAfter(10);

        //set the indent, margin and space for the third paragraph
        shape.getTextFrame().getParagraphs().get(2).setIndent(-100);
        shape.getTextFrame().getParagraphs().get(2).setLeftMargin(40);
        shape.getTextFrame().getParagraphs().get(2).setSpaceBefore(0);
        shape.getTextFrame().getParagraphs().get(2).setSpaceAfter(0);

        //save the document to file
        String output = "output/result.pptx";
        presentation.saveToFile(output, FileFormat.PPTX_2010);
    }
}

Effective screenshot after setting the indent style of paragraphs on presentation slide:

Java set the intents and spacing for paragraph on the presentation slide

Saturday, 12 October 2019 03:45

Auto Fit Text or Shape in PowerPoint in Java

Written by support iceblue

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

Tuesday, 10 September 2019 07:51

Replace Text in PowerPoint in Java

Written by support iceblue

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

Tuesday, 27 August 2019 06:07

Insert HTML String in PowerPoint in Java

Written by support iceblue

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

Spire.Presentation for Java supports to add numbered lists as well as bulleted lists to PowerPoint document. This article demonstrates how to add a numbered list, a symbol bulleted list and a custom picture bulleted list using Spire.Presentation for 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 AddBullets {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();

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

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

        //Clear the default paragragh
        shape.getTextFrame().getParagraphs().clear();

        String[] str = new String[] {"Item 1", "Item 2", "Item 3" ,"Item 4", "Item 5", "Item 6","Item 7", "Item 8", "Item 9" };

        //Add a numbered bulleted list
        for(int i = 0; i < 3; i ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(str[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_ROMAN_LC_PERIOD);
        }

        //Add a symbol bulleted list
        for(int j = 3; j < 6; j ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(str[j]);
            paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
            paragraph.setBulletType(TextBulletType.SYMBOL);
        }

        //Add a custom bulleted list with picture
        for(int k = 6; k < str.length; k ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(str[k]);
            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("timg.jpg"));
            paragraph.getBulletPicture().setEmbedImage(ppt.getImages().append(image));
        }

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

Create Numbered and Bulleted Lists in PowerPoint in Java

In this article, we will show you how to read/extract text from a PowerPoint document using Spire.Presentation for Java.

The sample PowerPoint document:

Read/Extract Text from a PowerPoint Document in Java

import com.spire.presentation.*;

import java.io.FileWriter;

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

        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the PowerPoint document
        ppt.loadFromFile("Input.pptx");

        StringBuilder buffer = new StringBuilder();

        //Loop through the slides in the document and extract text
        for (Object slide : ppt.getSlides()) {
            for (Object shape : ((ISlide) slide).getShapes()) {
                if (shape instanceof IAutoShape) {
                    for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
                        buffer.append(((ParagraphEx) tp).getText());
                    }
                }
            }
        }
        //Save text
        FileWriter writer = new FileWriter("ExtractText.txt");
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
    }
}

Output:

Read/Extract Text from a PowerPoint Document in Java