Manage PowerPoint Paragraph

Aspose.Slides provides all the interfaces and classes you need to work with PowerPoint texts, paragraphs, and portions .

  • Aspose.Slides provides the ITextFrame interface to allow you to add objects that represent a paragraph. An ITextFame object can have one or multiple paragraphs (each paragraph is created through a carriage return).
  • Aspose.Slides provides the IParagraph interface to allow you to add objects that represent portions. An IParagraph object can have one or multiple portions (collection of iPortions objects).
  • Aspose.Slides provides IPortion interface to allow you to add objects that represent texts and their formatting properties.

An IParagraph object is capable of handling texts with different formatting properties through its underlying IPortion objects.

Add Multiple Paragraph Containing Multiple Portions

These steps show you how to add a text frame containing 3 paragraphs and each paragraph containing 3 portions:

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add a Rectangle IAutoShape to the slide.
  4. Get the ITextFrame associated with the IAutoShape.
  5. Create two IParagraph objects and add them to the IParagraphs collection of the ITextFrame.
  6. Create three IPortion objects for each new IParagraph (two Portion objects for default Paragraph) and add each IPortion object to the IPortion collection of each IParagraph.
  7. Set some text for each portion.
  8. Apply your preferred formatting features to each portion using the formatting properties exposed by the IPortion object.
  9. Save the modified presentation.

This PHP code is an implementation of the steps for adding paragraphs containing portions:

# Instantiate a Presentation class that represents a PPTX file
$pres = new Presentation();
try {
    # Accessing first slide
    $slide = $pres->getSlides()->get_Item(0);
    # Add an AutoShape of Rectangle type
    $ashp = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 150, 300, 150);
    # Access TextFrame of the AutoShape
    $tf = $ashp->getTextFrame();
    # Create Paragraphs and Portions with different text formats
    $para0 = $tf->getParagraphs()->get_Item(0);
    $port01 = new Portion();
    $port02 = new Portion();
    $para0->getPortions()->add($port01);
    $para0->getPortions()->add($port02);
    $para1 = new Paragraph();
    $tf->getParagraphs()->add($para1);
    $port10 = new Portion();
    $port11 = new Portion();
    $port12 = new Portion();
    $para1->getPortions()->add($port10);
    $para1->getPortions()->add($port11);
    $para1->getPortions()->add($port12);
    $para2 = new Paragraph();
    $tf->getParagraphs()->add($para2);
    $port20 = new Portion();
    $port21 = new Portion();
    $port22 = new Portion();
    $para2->getPortions()->add($port20);
    $para2->getPortions()->add($port21);
    $para2->getPortions()->add($port22);
    for($i = 0; $i < 3; $i++) {
        for($j = 0; $j < 3; $j++) {
            $portion = $tf->getParagraphs()->get_Item($i)->getPortions()->get_Item($j);
            $portion->setText("Portion0" . $j);
            if ($j == 0) {
                $portion->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
                $portion->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->RED);
                $portion->getPortionFormat()->setFontBold(NullableBool::True);
                $portion->getPortionFormat()->setFontHeight(15);
            } else if ($j == 1) {
                $portion->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
                $portion->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
                $portion->getPortionFormat()->setFontItalic(NullableBool::True);
                $portion->getPortionFormat()->setFontHeight(18);
            }
        }
    }
    # Write PPTX to Disk
    $pres->save("multiParaPort_out.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($pres)) {
        $pres->dispose();
    }
}

Manage Paragraph Bullets

Bullet lists help you to organize and present information quickly and efficiently. Bulleted paragraphs are always easier to read and understand.

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add an autoshape to the selected slide.
  4. Access the autoshape’s TextFrame.
  5. Remove the default paragraph in the TextFrame.
  6. Create the first paragraph instance using the Paragraph class.
  7. Set the bullet Type for the paragraph to Symbol and set the bullet character.
  8. Set the paragraph Text.
  9. Set the paragraph Indent for the bullet.
  10. Set a color for the bullet.
  11. Set a height of the bullet.
  12. Add the new paragraph to the TextFrame paragraph collection.
  13. Add the second paragraph and repeat the process given in steps 7 to 13.
  14. Save the presentation.

This PHP code shows you how to add a paragraph bullet:

# Instantiates a Presentation class that represents a PPTX file
$pres = new Presentation();
try {
    # Accesses the first slide
    $slide = $pres->getSlides()->get_Item(0);
    # Adds and accesses Autoshape
    $aShp = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
    # Accesses the autoshape text frame
    $txtFrm = $aShp->getTextFrame();
    # Removes the default paragraph
    $txtFrm->getParagraphs()->removeAt(0);
    # Creates a paragraph
    $para = new Paragraph();
    # Sets a paragraph bullet style and symbol
    $para->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
    $para->getParagraphFormat()->getBullet()->setChar(8226);
    # Sets a paragraph text
    $para->setText("Welcome to Aspose.Slides");
    # Sets bullet indent
    $para->getParagraphFormat()->setIndent(25);
    # Sets bullet color
    $para->getParagraphFormat()->getBullet()->getColor()->setColorType(ColorType::RGB);
    $para->getParagraphFormat()->getBullet()->getColor()->setColor(java("java.awt.Color")->BLACK);
    $para->getParagraphFormat()->getBullet()->setBulletHardColor(NullableBool::True);// set IsBulletHardColor to true to use own bullet color

    # Sets Bullet Height
    $para->getParagraphFormat()->getBullet()->setHeight(100);
    # Adds Paragraph to text frame
    $txtFrm->getParagraphs()->add($para);
    # Creates second paragraph
    $para2 = new Paragraph();
    # Sets paragraph bullet type and style
    $para2->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
    $para2->getParagraphFormat()->getBullet()->setNumberedBulletStyle(NumberedBulletStyle->BulletCircleNumWDBlackPlain);
    # Adds paragraph text
    $para2->setText("This is numbered bullet");
    # Sets bullet indent
    $para2->getParagraphFormat()->setIndent(25);
    $para2->getParagraphFormat()->getBullet()->getColor()->setColorType(ColorType::RGB);
    $para2->getParagraphFormat()->getBullet()->getColor()->setColor(java("java.awt.Color")->BLACK);
    $para2->getParagraphFormat()->getBullet()->setBulletHardColor(NullableBool::True);// set IsBulletHardColor to true to use own bullet color

    # Sets Bullet Height
    $para2->getParagraphFormat()->getBullet()->setHeight(100);
    # Adds Paragraph to text frame
    $txtFrm->getParagraphs()->add($para2);
    # Saves the modified presentation
    $pres->save("Bullet_out.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($pres)) {
        $pres->dispose();
    }
}

Manage Picture Bullets

Bullet lists help you to organize and present information quickly and efficiently. Picture paragraphs are easy to read and understand.

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add an autoshape to the slide.
  4. Access the autoshape’s TextFrame.
  5. Remove the default paragraph in the TextFrame.
  6. Create the first paragraph instance using the Paragraph class.
  7. Load the image in IPPImage.
  8. Set the bullet type to Picture and set the image.
  9. Set the Paragraph Text.
  10. Set the Paragraph Indent for the bullet.
  11. Set a color for the bullet.
  12. Set a height for the bullet.
  13. Add the new paragraph to the TextFrame paragraph collection.
  14. Add the second paragraph and repeat the process based on the previous steps.
  15. Save the modified presentation.

This PHP code shows you how to add and manage picture bullets:

# Instantiates a Presentation class that represents a PPTX file
$presentation = new Presentation();
try {
    # Accesses the first slide
    $slide = $presentation->getSlides()->get_Item(0);
    # Instantiates the image for bullets
    $picture;
    $image = Images->fromFile("bullets.png");
    try {
        $picture = $presentation->getImages()->addImage($image);
    } finally {
        if (!java_is_null($image)) {
            $image->dispose();
        }
    }
    # Adds and accesses Autoshape
    $autoShape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
    # Accesses the autoshape textframe
    $textFrame = $autoShape->getTextFrame();
    # Removes the default paragraph
    $textFrame->getParagraphs()->removeAt(0);
    # Creates a new paragraph
    $paragraph = new Paragraph();
    $paragraph->setText("Welcome to Aspose.Slides");
    # Sets paragraph bullet style and image
    $paragraph->getParagraphFormat()->getBullet()->setType(BulletType::Picture);
    $paragraph->getParagraphFormat()->getBullet()->getPicture()->setImage($picture);
    # Sets bullet Height
    $paragraph->getParagraphFormat()->getBullet()->setHeight(100);
    # Adds paragraph to text frame
    $textFrame->getParagraphs()->add($paragraph);
    # Writes the presentation as a PPTX file
    $presentation->save("ParagraphPictureBulletsPPTX_out.pptx", SaveFormat::Pptx);
    # Writes the presentation as a PPT file
    $presentation->save("ParagraphPictureBulletsPPT_out.ppt", SaveFormat::Ppt);
} catch (JavaException $e) {
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

Manage Multilevel Bullets

Bullet lists help you to organize and present information quickly and efficiently. Multilevel bullets are easy to read and understand.

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add an autoshape in the new slide.
  4. Access the autoshape’s TextFrame.
  5. Remove the default paragraph in the TextFrame.
  6. Create the first paragraph instance through the Paragraph class and set the depth to 0.
  7. Create the second paragraph instance through the Paragraph class and set the depth set to 1.
  8. Create the third paragraph instance through the Paragraph class and set the depth set to 2.
  9. Create the fourth paragraph instance through the Paragraph class and set the depth set to 3.
  10. Add the new paragraphs to the TextFrame paragraph collection.
  11. Save the modified presentation.

This PHP code shows you how to add and manage multilevel bullets:

# Instantiates a Presentation class that represents a PPTX file
$pres = new Presentation();
try {
    # Accesses the first slide
    $slide = $pres->getSlides()->get_Item(0);
    # Adds and accesses Autoshape
    $aShp = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
    # Accesses the text frame of created autoshape
    $text = $aShp->addTextFrame("");
    # Clears the default paragraph
    $text->getParagraphs()->clear();
    # Adds the first paragraph
    $para1 = new Paragraph();
    $para1->setText("Content");
    $para1->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
    $para1->getParagraphFormat()->getBullet()->setChar(8226);
    $para1->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $para1->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
    # Sets the bullet level
    $para1->getParagraphFormat()->setDepth(0);
    # Adds the second paragraph
    $para2 = new Paragraph();
    $para2->setText("Second Level");
    $para2->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
    $para2->getParagraphFormat()->getBullet()->setChar('-');
    $para2->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $para2->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
    # Sets the bullet level
    $para2->getParagraphFormat()->setDepth(1);
    # Adds the third paragraph
    $para3 = new Paragraph();
    $para3->setText("Third Level");
    $para3->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
    $para3->getParagraphFormat()->getBullet()->setChar(8226);
    $para3->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $para3->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
    # Sets the bullet level
    $para3->getParagraphFormat()->setDepth(2);
    # Adds the fourth paragraph
    $para4 = new Paragraph();
    $para4->setText("Fourth Level");
    $para4->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
    $para4->getParagraphFormat()->getBullet()->setChar('-');
    $para4->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $para4->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
    # Sets the bullet level
    $para4->getParagraphFormat()->setDepth(3);
    # Adds paragraphs to collection
    $text->getParagraphs()->add($para1);
    $text->getParagraphs()->add($para2);
    $text->getParagraphs()->add($para3);
    $text->getParagraphs()->add($para4);
    # Writes the presentation as a PPTX file
    $pres->save("MultilevelBullet.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($pres)) {
        $pres->dispose();
    }
}

Manage Paragraph with Custom Numbered List

The IBulletFormat interface provides the NumberedBulletStartWith property and others that allow you to manage paragraphs with custom numbering or formatting.

  1. Create an instance of the Presentation class.
  2. Access the slide containing the paragraph.
  3. Add an autoshape to the slide.
  4. Access the autoshape TextFrame.
  5. Remove the default paragraph in the TextFrame.
  6. Create the first paragraph instance through the Paragraph class and set NumberedBulletStartWith to 2.
  7. Create the second paragraph instance through the Paragraph class and set NumberedBulletStartWith to 3.
  8. Create the third paragraph instance through the Paragraph class and set NumberedBulletStartWith to 7.
  9. Add the new paragraphs to the TextFrame paragraph collection.
  10. Save the modified presentation.

This PHP code shows you how to add and manage paragraphs with custom numbering or formatting:

$presentation = new Presentation();
try {
    $shape = $presentation->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
    # Accesses the text frame of created autoshape
    $textFrame = $shape->getTextFrame();
    # Removes the default exisiting paragraph
    $textFrame->getParagraphs()->removeAt(0);
    # First list
    $paragraph1 = new Paragraph();
    $paragraph1->setText("bullet 2");
    $paragraph1->getParagraphFormat()->setDepth(4);
    $paragraph1->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(2);
    $paragraph1->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
    $textFrame->getParagraphs()->add($paragraph1);
    $paragraph2 = new Paragraph();
    $paragraph2->setText("bullet 3");
    $paragraph2->getParagraphFormat()->setDepth(4);
    $paragraph2->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(3);
    $paragraph2->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
    $textFrame->getParagraphs()->add($paragraph2);
    $paragraph5 = new Paragraph();
    $paragraph5->setText("bullet 7");
    $paragraph5->getParagraphFormat()->setDepth(4);
    $paragraph5->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(7);
    $paragraph5->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
    $textFrame->getParagraphs()->add($paragraph5);
    $presentation->save("SetCustomBulletsNumber-slides.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

Set Paragraph Indent

  1. Create an instance of Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add a rectangle autoshape to the slide.
  4. Add a TextFrame with three paragraphs to the rectangle autoshape.
  5. Hide the rectangle lines.
  6. Set the indent for each Paragraph through their BulletOffset property.
  7. Write the modified presentation as a PPT file.

This PHP code shows you how to set a paragraph indent:

# Instantiate Presentation Class
$pres = new Presentation();
try {
    # Get first slide
    $sld = $pres->getSlides()->get_Item(0);
    # Add a Rectangle Shape
    $rect = $sld->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 500, 150);
    # Add TextFrame to the Rectangle
    $tf = $rect->addTextFrame("This is first line \rThis is second line \rThis is third line");
    # Set the text to fit the shape
    $tf->getTextFrameFormat()->setAutofitType(TextAutofitType::Shape);
    # Hide the lines of the Rectangle
    $rect->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
    # Get first Paragraph in the TextFrame and set its Indent
    $para1 = $tf->getParagraphs()->get_Item(0);
    # Setting paragraph bullet style and symbol
    $para1->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
    $para1->getParagraphFormat()->getBullet()->setChar(8226);
    $para1->getParagraphFormat()->setAlignment(TextAlignment->Left);
    $para1->getParagraphFormat()->setDepth(2);
    $para1->getParagraphFormat()->setIndent(30);
    # Get second Paragraph in the TextFrame and set its Indent
    $para2 = $tf->getParagraphs()->get_Item(1);
    $para2->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
    $para2->getParagraphFormat()->getBullet()->setChar(8226);
    $para2->getParagraphFormat()->setAlignment(TextAlignment->Left);
    $para2->getParagraphFormat()->setDepth(2);
    $para2->getParagraphFormat()->setIndent(40);
    # Get third Paragraph in the TextFrame and set its Indent
    $para3 = $tf->getParagraphs()->get_Item(2);
    $para3->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
    $para3->getParagraphFormat()->getBullet()->setChar(8226);
    $para3->getParagraphFormat()->setAlignment(TextAlignment->Left);
    $para3->getParagraphFormat()->setDepth(2);
    $para3->getParagraphFormat()->setIndent(50);
    # Write the Presentation to disk
    $pres->save("InOutDent_out.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($pres)) {
        $pres->dispose();
    }
}

Set Hanging Indent for Paragraph

This PHP code shows you how to set the hanging indent for a paragraph:

$pres = new Presentation();
try {
    $autoShape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 250, 550, 150);
    $para1 = new Paragraph();
    $para1->setText("Example");
    $para2 = new Paragraph();
    $para2->setText("Set Hanging Indent for Paragraph");
    $para3 = new Paragraph();
    $para3->setText("This code shows you how to set the hanging indent for a paragraph: ");
    $para2->getParagraphFormat()->setMarginLeft(10.0);
    $para3->getParagraphFormat()->setMarginLeft(20.0);
    $autoShape->getTextFrame()->getParagraphs()->add($para1);
    $autoShape->getTextFrame()->getParagraphs()->add($para2);
    $autoShape->getTextFrame()->getParagraphs()->add($para3);
    $pres->save("pres.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($pres)) {
        $pres->dispose();
    }
}

Manage End Paragraph Run Properties for Paragraph

  1. Create an instance of Presentation class.
  2. Get the reference for the slide containing the paragraph through its position.
  3. Add a rectangle autoshape to the slide.
  4. Add a TextFrame with two paragraphs to the Rectangle.
  5. Set the FontHeight and Font type for the paragraphs.
  6. Set the End properties for the paragraphs.
  7. Write the modified presentation as a PPTX file.

This PHP code shows you how to set the End properties for paragraphs in PowerPoint:

$pres = new Presentation();
try {
    $shape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 10, 200, 250);
    $para1 = new Paragraph();
    $para1->getPortions()->add(new Portion("Sample text"));
    $para2 = new Paragraph();
    $para2->getPortions()->add(new Portion("Sample text 2"));
    $portionFormat = new PortionFormat();
    $portionFormat::setFontHeight(48);
    $portionFormat::setLatinFont(new FontData("Times New Roman"));
    $para2->setEndParagraphPortionFormat($portionFormat);
    $shape->getTextFrame()->getParagraphs()->add($para1);
    $shape->getTextFrame()->getParagraphs()->add($para2);
    $pres->save($resourcesOutputPath . "pres.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($pres)) {
        $pres->dispose();
    }
}

Import HTML Text into Paragraphs

Aspose.Slides provides enhanced support for importing HTML text into paragraphs.

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add an autoshape to the slide.
  4. Add and access autoshape ITextFrame.
  5. Remove the default paragraph in the ITextFrame.
  6. Read the source HTML file in a TextReader.
  7. Create the first paragraph instance through the Paragraph class.
  8. Add the HTML file content in the read TextReader to the TextFrame’s ParagraphCollection.
  9. Save the modified presentation.

This PHP code is an implementation of the steps for importing HTML texts in paragraphs:

# Create Empty presentation instance
$pres = new Presentation();
try {
    # Acesss the default first slide of presentation
    $slide = $pres->getSlides()->get_Item(0);
    # Adding the AutoShape to accomodate the HTML content
    $ashape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 10, $pres->getSlideSize()->getSize()->getWidth() - 20, $pres->getSlideSize()->getSize()->getHeight() - 10);
    $ashape->getFillFormat()->setFillType(FillType::NoFill);
    # Adding text frame to the shape
    $ashape->addTextFrame("");
    # Clearing all paragraphs in added text frame
    $ashape->getTextFrame()->getParagraphs()->clear();
    # Loading the HTML file using stream reader
    $tr = new StreamReader("file.html");
    # Adding text from HTML stream reader in text frame
    $ashape->getTextFrame()->getParagraphs()->addFromHtml($tr->readToEnd());
    # Saving Presentation
    $pres->save("output_out.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($pres)) {
        $pres->dispose();
    }
}

Export Paragraphs Text to HTML

Aspose.Slides provides enhanced support for exporting texts (contained in paragraphs) to HTML.

  1. Create an instance of the Presentation class and load the desired presentation.
  2. Access the relevant slide’s reference through its index.
  3. Access the shape containing the text that will be exported to HTML.
  4. Access the shape TextFrame.
  5. Create an instance of StreamWriter and add the new HTML file.
  6. Provide a starting index to StreamWriter and export your preferred paragraphs.

This PHP code shows you how to export PowerPoint paragraph texts to HTML:

# Load the presentation file
$pres = new Presentation("ExportingHTMLText.pptx");
try {
    # Acesss the default first slide of presentation
    $slide = $pres->getSlides()->get_Item(0);
    # Desired index
    $index = 0;
    # Accessing the added shape
    $ashape = $slide->getShapes()->get_Item($index);
    # Creating output HTML file
    $os = new Java("java.io.FileOutputStream", "output.html");
    $writer = new OutputStreamWriter($os, "UTF-8");
    # Extracting first paragraph as HTML
    # Writing Paragraphs data to HTML by providing paragraph starting index, total paragraphs to be copied
    $writer->write($ashape->getTextFrame()->getParagraphs()->exportToHtml(0, $ashape->getTextFrame()->getParagraphs()->getCount(), null));
    $writer->close();
} catch (JavaException $e) {
} finally {
    if (!java_is_null($pres)) {
        $pres->dispose();
    }
}

Save a Paragraph as an Image

In this section, we will explore two examples that demonstrate how to save a text paragraph, represented by the Paragraph class, as an image. Both examples include obtaining the image of a shape containing the paragraph using the getImage methods from the Shape class, calculating the bounds of the paragraph within the shape, and exporting it as a bitmap image. These approaches allow you to extract specific parts of the text from PowerPoint presentations and save them as separate images, which can be useful for further use in various scenarios.

Let’s assume we have a presentation file called sample.pptx with one slide, where the first shape is a text box containing three paragraphs.

The text box with three paragraphs

Example 1

In this example, we obtain the second paragraph as an image. To do this, we extract the image of the shape from the first slide of the presentation and then calculate the bounds of the second paragraph in the shape’s text frame. The paragraph is then redrawn onto a new bitmap image, which is saved in PNG format. This method is especially useful when you need to save a specific paragraph as a separate image while preserving the exact dimensions and formatting of the text.

$imageIO = new Java("javax.imageio.ImageIO");

$presentation = new Presentation("sample.pptx");
try {
    $firstShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);

    // Save the shape in memory as a bitmap.
    $shapeImage = $firstShape->getImage();
    $shapeImageStream = new Java("java.io.ByteArrayOutputStream");
    $shapeImage->save($shapeImageStream, ImageFormat::Png);
    $shapeImage->dispose();

    // Create a shape bitmap from memory.
    $shapeImageInputStream = new Java("java.io.ByteArrayInputStream", $shapeImageStream->toByteArray());
    $shapeBitmap = $imageIO->read($shapeImageInputStream);

    // Calculate the boundaries of the second paragraph.
    $secondParagraph = $firstShape->getTextFrame()->getParagraphs()->get_Item(1);
    $paragraphRectangle = $secondParagraph->getRect();

    // Calculate the coordinates and size for the output image (minimum size - 1x1 pixel).
    $imageX = floor(java_values($paragraphRectangle->getX()));
    $imageY = floor(java_values($paragraphRectangle->getY()));
    $imageWidth = max(1, ceil(java_values($paragraphRectangle->getWidth())));
    $imageHeight = max(1, ceil(java_values($paragraphRectangle->getHeight())));

    // Crop the shape bitmap to get the paragraph bitmap only.
    $paragraphBitmap = $shapeBitmap->getSubimage($imageX, $imageY, $imageWidth, $imageHeight);

    $imageIO->write($paragraphBitmap, "png", new Java("java.io.File", "paragraph.png"));
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

The result:

The paragraph image

Example 2

In this example, we extend the previous approach by adding scaling factors to the paragraph image. The shape is extracted from the presentation and saved as an image with a scaling factor of 2. This allows for a higher resolution output when exporting the paragraph. The paragraph bounds are then calculated considering the scale. Scaling can be particularly useful when a more detailed image is needed, for example, for use in high-quality printed materials.

$imageIO = new Java("javax.imageio.ImageIO");

$imageScaleX = 2;
$imageScaleY = $imageScaleX;

$presentation = new Presentation("sample.pptx");
try {
    $firstShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);

    // Save the shape in memory as a bitmap with scaling.
    $shapeImage = $firstShape->getImage(ShapeThumbnailBounds::Shape, $imageScaleX, $imageScaleY);
    $shapeImageStream = new Java("java.io.ByteArrayOutputStream");
    $shapeImage->save($shapeImageStream, ImageFormat::Png);
    $shapeImage->dispose();

    // Create a shape bitmap from memory.
    $shapeImageInputStream = new Java("java.io.ByteArrayInputStream", $shapeImageStream->toByteArray());
    $shapeBitmap = $imageIO->read($shapeImageInputStream);

    // Calculate the boundaries of the second paragraph.
    $secondParagraph = $firstShape->getTextFrame()->getParagraphs()->get_Item(1);
    $paragraphRectangle = $secondParagraph->getRect();
    $paragraphRectangle->setRect(
            java_values($paragraphRectangle->getX()) * $imageScaleX,
            java_values($paragraphRectangle->getY()) * $imageScaleY,
            java_values($paragraphRectangle->getWidth()) * $imageScaleX,
            java_values($paragraphRectangle->getHeight()) * $imageScaleY
    );

    // Calculate the coordinates and size for the output image (minimum size - 1x1 pixel).
    $imageX = floor(java_values($paragraphRectangle->getX()));
    $imageY = floor(java_values($paragraphRectangle->getY()));
    $imageWidth = max(1, ceil(java_values($paragraphRectangle->getWidth())));
    $imageHeight = max(1, ceil(java_values($paragraphRectangle->getHeight())));

    // Crop the shape bitmap to get the paragraph bitmap only.
    $paragraphBitmap = $shapeBitmap->getSubimage($imageX, $imageY, $imageWidth, $imageHeight);

    $imageIO->write($paragraphBitmap, "png", new Java("java.io.File", "paragraph.png"));
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}