Manage Fonts - PowerPoint Java API

To manage font properties of a paragraph using Aspose.Slides for PHP via Java:

  1. Create an instance of the Presentation class.
  2. Obtain a slide’s reference by using its index.
  3. Access the Placeholder shapes in the slide and typecast them to AutoShape.
  4. Get the Paragraph from the TextFrame exposed by AutoShape.
  5. Justify the paragraph.
  6. Access a Paragraph’s text Portion.
  7. Define the font using FontData and set the Font of the text Portion accordingly.
    1. Set the font to bold.
    2. Set the font to italic.
  8. Set the font color using the FillFormat exposed by the Portion object.
  9. Save the modified presentation to a PPTX file.

The implementation of the above steps is given below. It takes an unadorned presentation and formats the fonts on one of the slides. The screenshots that follow show the input file and how the code snippets change it. The code changes the font, the color, and the font style.

todo:image_alt_text
Figure: The text in the input file
todo:image_alt_text
Figure: The same text with updated formatting
  # Instantiate a Presentation object that represents a PPTX file
  $pres = new Presentation("FontProperties.pptx");
  try {
    # Accessing a slide using its slide position
    $slide = $pres->getSlides()->get_Item(0);
    # Accessing the first and second placeholder in the slide and typecasting it as AutoShape
    $tf1 = $slide->getShapes()->get_Item(0)->getTextFrame();
    $tf2 = $slide->getShapes()->get_Item(1)->getTextFrame();
    # Accessing the first Paragraph
    $para1 = $tf1->getParagraphs()->get_Item(0);
    $para2 = $tf2->getParagraphs()->get_Item(0);
    # Justify the paragraph
    $para2->getParagraphFormat()->setAlignment(TextAlignment->JustifyLow);
    # Accessing the first portion
    $port1 = $para1->getPortions()->get_Item(0);
    $port2 = $para2->getPortions()->get_Item(0);
    # Define new fonts
    $fd1 = new FontData("Elephant");
    $fd2 = new FontData("Castellar");
    # Assign new fonts to portion
    $port1->getPortionFormat()->setLatinFont($fd1);
    $port2->getPortionFormat()->setLatinFont($fd2);
    # Set font to Bold
    $port1->getPortionFormat()->setFontBold(NullableBool::True);
    $port2->getPortionFormat()->setFontBold(NullableBool::True);
    # Set font to Italic
    $port1->getPortionFormat()->setFontItalic(NullableBool::True);
    $port2->getPortionFormat()->setFontItalic(NullableBool::True);
    # Set font color
    $port1->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $port1->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
    $port2->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $port2->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GREEN);
    # Save the PPTX to disk
    $pres->save("WelcomeFont.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
  }

Set Text Font Properties

To create a textbox and set font properties of the text in it:

  1. Create an instance of the Presentation class.
  2. Obtain the reference of a slide by using its index.
  3. Add an AutoShape of the type Rectangle to the slide.
  4. Remove the fill style associated with the AutoShape.
  5. Access the of the AutoShape’s TextFrame.
  6. Add some text to the TextFrame.
  7. Access the Portion object associated with the TextFrame.
  8. Define the font to be used for the Portion.
  9. Set other font properties like bold, italic, underline, color and height using the relevant properties as exposed by the Portion object.
  10. Write the modified presentation as a PPTX file.

The implementation of the above steps is given below.

todo:image_alt_text
Figure: Text with some font properties set by Aspose.Slides for PHP via Java
  # Instantiate a Presentation object that represents a PPTX file
  $pres = new Presentation();
  try {
    # Get first slide
    $sld = $pres->getSlides()->get_Item(0);
    # Add an AutoShape of Rectangle type
    $ashp = $sld->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 200, 50);
    # Remove any fill style associated with the AutoShape
    $ashp->getFillFormat()->setFillType(FillType::NoFill);
    # Access the TextFrame associated with the AutoShape
    $tf = $ashp->getTextFrame();
    $tf->setText("Aspose TextBox");
    # Access the Portion associated with the TextFrame
    $port = $tf->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
    # Set the Font for the Portion
    $port->getPortionFormat()->setLatinFont(new FontData("Times New Roman"));
    # Set Bold property of the Font
    $port->getPortionFormat()->setFontBold(NullableBool::True);
    # Set Italic property of the Font
    $port->getPortionFormat()->setFontItalic(NullableBool::True);
    # Set Underline property of the Font
    $port->getPortionFormat()->setFontUnderline(TextUnderlineType::Single);
    # Set the Height of the Font
    $port->getPortionFormat()->setFontHeight(25);
    # Set the color of the Font
    $port->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $port->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
    # Save the presentation to disk
    $pres->save("pptxFont.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
  }