Usually, HTML can include text, image, audio or video. In this article, I’ll use a free API to separately demonstrate how to insert HTML formatted text or HTML with an image into PowerPoint slides using Java code.
The free API I used in this tutorial is called Free Spire.Presentation for Java which is a free and professional PowerPoint API that enables developers to create, read, write, convert and save PowerPoint documents in Java Applications without installing Microsoft Office.
Before running codes, you need to insert Spire.Presentation.jar into your project. If you’re creating a Maven project, you just need to specify the following configuration in your Maven pom.xml and then click the “Import Changes”.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Finally, it will look like the screenshot below.
Insert HTML formatted text in a PowerPoint slideFree Spire.Presentation for Java provides the addFromHTML method to support inserting HTML
formatted text in a PowerPoint slide. In the process of it, you can set the font color of the text. Here
are code snippets to do it.
import java.awt.Color;
import java.awt.Rectangle;
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
public class AddHTMLText {
public static void main(String[] args) throws Exception {
//Initiate a Presentation object
Presentation ppt = new Presentation();
//Add a Shape to the first slide, and set its size and location
IAutoShape shape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle(50, 50, 350, 120));
//Set the format type of the shape
shape.getFill().setFillType(FillFormatType.SOLID);
shape.getFill().getSolidColor().setColor(Color.white);
shape.getShapeStyle().getLineColor().setColor(Color.black);
//Clear the default paragraph
shape.getTextFrame().getParagraphs().clear();
//Insert html string with text in the shape
String code = "<ul><li style=\"color:blue\">Introduction to Brand Marketing</li><li style=\"color:green\">Brand Positioning Personality</li>" +
"<li style=\"color:gray\">Brand Design</li><li style=\"color:red\">Brand Communication Advertising</li></ul>";
shape.getTextFrame().getParagraphs().addFromHtml(code);
//Saving the resulting document
String outputFile = "output/AddHTMLStringToPPT.pptx";
ppt.saveToFile(outputFile, FileFormat.PPTX_2010);
}
}Output
Insert HTML with text and an image in a PowerPoint slide
You can insert HTML with an image through adding the local link of an image to the HTML String.
import com.spire.presentation.*;
import com.spire.presentation.collections.ShapeList;
public class AddHTMLWithImages {
public static void main(String[] args) throws Exception {
//Initiate a Presentation object
Presentation ppt = new Presentation();
//Get the shapes on the first slide
ShapeList shapes = ppt.getSlides().get(0).getShapes();
//Add HTML with text and an image to the shapes
String code ="<html><body><li style=\"color:blue\">Producer's Perspective</li><p>Emphasize the external performance of the brand</p>" +
"<img src='C:\\Users\\Test1\\Desktop\\Image.jpg'/></body></html>";
shapes.addFromHtml(code);
//Saving the resulting document
String outputFile = "output/AddHTMLWithImages.pptx";
ppt.saveToFile(outputFile,FileFormat.PPTX_2010);
}
}Output
No comments:
Post a Comment