How to Add HTML Text to Slide and Set Font Style Using Aspose.Slides for Java?

Hi Team,

Could you please help me with a sample code to

  • Insert a text box in a presentation
  • Add a html text to the text box with hyperlink
  • And the text for the hyperlink must be of font style Arial and size 8 pts.

Thanks,
Thilak

@Thilakbabu,
Thank you for posting the inquiry.

To perform this sequence, you should add a rectangle autoshape, add an HTML text into a paragraph collection contained in an autoshape’s text frame, and set formatting options for the new paragraph. The following code snippet shows you how to do this:

// Add an empty transparent text box to a slide.
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 200, 100);
autoShape.getFillFormat().setFillType(FillType.NoFill);
autoShape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
autoShape.getTextFrame().getParagraphs().clear();

// Add some HTML text to the shape.
String htmlData = "<a href=\"https://www.aspose.com/\">Aspose Website</a>";
autoShape.getTextFrame().getParagraphs().addFromHtml(htmlData);

// Set the text format for the HTML text.
IParagraph lastParagraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
lastParagraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Arial"));
lastParagraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(8);

Documents:
Manage TextBox
Manage Paragraph
Text Formatting

API Reference:
addAutoShape Method
addFromHtml Method
IParagraphFormat Interface

If you fail to do so, please share your example HTML text.

@Andrey_Potapov
The above code is creating a PPT with the expected hyperlink and formatting when I test it with simple java application, but the same code used in our project is not apply the formatting. The format setting has verified and good right before the PPT is saved in our code. Can you please advise?

I am trying to add the hyperlink with mixed bold and regular text as the hyperlink display content to PPT with specified format like size, font etc.
Example text:
bold text here and bold text here of bold text here in bold text here during bold text here

Thanks,
Liqing

@liqingruan,
Thank you for the description your requirements.

You are right, parts of the hyperlink text may be formatted differently. In this case, you can use IHyperlinkManager for paragraphs or text portions. The following code snippets show you how to do this:

// Set a hyperlink to a paragraph.
paragraph.getParagraphFormat().getDefaultPortionFormat().getHyperlinkManager().setExternalHyperlinkClick("https://www.aspose.com/");
// Set a hyperlink to a text portion.
portion.getPortionFormat().getHyperlinkManager().setExternalHyperlinkClick("https://www.aspose.com/");

Documents: Manage Hyperlinks
API Reference: IHyperlinkManager interface

@Andrey_Potapov

Thanks for your timely response!
With the above mentioned code, where can I set the HTML text content for the hyperlink?

I have tried different way, but non of them can handle the use case including the following:

  1. hyperlink
  2. display the HTML text content (text that has html tags) for the hyperlink
  3. apply the style formatting to the above text content(2)

Can you please advise a solution to achieve all of the above?

Please see my code below including each try and the output description:

 final ITextFrame textFrame = textBox.getTextFrame();

      // Add some HTML text to the shape.
      String regularText = "Aspose Website";

      String htmlData =
          "<b>bold text</b> and <b>bold text</b> of <b>bold text</b> in <b>bold text</b>";
     
      String htmlDataWithlink =
          "<a href=\"https://www.aspose.com/\"><b>bold text</b> and <b>bold text</b> of <b>bold text</b> in <b>bold text</b></a>";


      IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
      final IPortion port = textFrame.getParagraphs().get_Item(0).getPortions().get_Item(0);
      IPortionFormat portionFormat = port.getPortionFormat();
      IParagraphCollection paragraphCollection = textFrame.getParagraphs();

      // set the display content text for the hyperlink
      // try 1 set html text (with hyperlink) in paragraphCollection
      // paragraphCollection.addFromHtml(htmlDataWithlink);//htmlData is displyed with hyperlink,
      // but the format is not applied to the text

      // trt2 set html text in paragraphCollection
      // paragraphCollection.addFromHtml(htmlData);//no text is diplayed

      // try3 set html text in paragraph
      // paragraph.setText(htmlData); //no text is displayed

      // try4 set regular text in paragraph
      // paragraph.setText(regularText); //no text is displayed

      // try5 set html text in textFrame
      // textFrame.setText(htmlData); //no text is displayed

      // try6 set regular text in textFrame
      // textFrame.setText(regularText); // no text is displayed

      // try7 set html text in portion
      // port.setText(htmlData); // show the htmlData as it is with format applied, but NOT applying
                              // the HTML tag (<b>) in the htmlData

      // try8 set regular text in portion
      port.setText(regularText); // show the text with format applied

      // Set a hyperlink to a text portion.
      port.getPortionFormat().getHyperlinkManager()
          .setExternalHyperlinkClick("https://www.aspose.com/");

//apply the style format	  
portionFormat.setFontHeight(10.0);
portionFormat.setLatinFont(new FontData("Montserrat"));
portionFormat.getFillFormat().setFillType(FillType.Solid);
portionFormat.getFillFormat().getSolidFillColor().setColor("#151921"));

Thanks,
Liqing

@liqingruan,
Please take a look at the following code example. It shows you how to add the formatted HTML text to an autoshape and set the hyperlink for the text.

var presentation = new Presentation("example.pptx");

var shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);

var allParagraphs = shape.getTextFrame().getParagraphs();

String htmlData = "<b>bold text</b> and <b>bold text</b> of <b>bold text</b> in <b>bold text</b>";
allParagraphs.addFromHtml(htmlData);

var lastParagraph = allParagraphs.get_Item(allParagraphs.getCount() - 1);
lastParagraph.getParagraphFormat().getDefaultPortionFormat().getHyperlinkManager().setExternalHyperlinkClick("https://www.aspose.com/");

presentation.save("example_out.pptx", SaveFormat.Pptx);
presentation.dispose();

Files: examples.zip (48.6 KB)