Paragraph and Text

Paragraph and Text (5)

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