在这个简单的主题中,我们将引导您了解如何在 MS Windows、macOS 或 Ubuntu 操作系统中使用 Java 创建 PowerPoint Presentation。本主题涵盖设置环境的详细步骤,并通过使用几行简单的代码在 Java PPT 演示文稿中生成。
用 Java 生成 PowerPoint 演示文稿的步骤
- 从 Maven 存储库下载并安装 Aspose.Slides for Java
- 实例化 Presentation 类对象以创建一个空的演示文稿
- 创建一张空白幻灯片并将其添加到演示幻灯片集合中
- 使用 AddAutoShape 方法,在新创建的幻灯片中插入一个 Rectangle 形状
- 使用 addTextFrame 方法插入文本框并设置文本相关属性
- 将演示文稿以 PPTX 格式保存在磁盘上
上述Java 中的步骤使用简单的 API 接口在磁盘上创建 PPTX 文件,不依赖于 PowerPoint。首先,使用 Presentation 类实例创建一个空的演示文稿,然后在演示文稿中添加一个空白幻灯片。然后,在形状内部添加一个文本框架,并在使用 save 方法将演示文稿保存到磁盘之前设置其各自的文本属性。
使用 Java 创建 PowerPoint 演示文稿的代码
import com.aspose.slides.FillType; | |
import com.aspose.slides.IAutoShape; | |
import com.aspose.slides.IPortionFormat; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.ITextFrame; | |
import com.aspose.slides.License; | |
import com.aspose.slides.NullableBool; | |
import com.aspose.slides.Presentation; | |
import com.aspose.slides.SaveFormat; | |
import com.aspose.slides.ShapeType; | |
import com.aspose.slides.SlideLayoutType; | |
import java.awt.Color; | |
public class CreatePresentation { | |
public static void main(String[] args){ | |
// Setting the linence for the product | |
License SlidesLicense = new License(); | |
SlidesLicense.setLicense("Aspose.Total.lic"); | |
// Create an empty presentation using the Presentation class instance | |
Presentation presentation = new Presentation(); | |
// Insert a Blank slide inside the presentation | |
ISlide slide = presentation.getSlides().addEmptySlide(presentation.getLayoutSlides() | |
.getByType(SlideLayoutType.Blank)); | |
// Add an auto-shape of Rectangle type | |
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 0); | |
// Fill the shape with Green color | |
autoShape.getFillFormat().setFillType(FillType.Solid); | |
autoShape.getFillFormat().getSolidFillColor().setColor(Color.GREEN); | |
// Adding some text inside the shape | |
ITextFrame textFrame = autoShape.addTextFrame("This is Aspose.Slides"); | |
// Set text related properties | |
IPortionFormat portionFormat = textFrame.getParagraphs().get_Item(0) | |
.getPortions().get_Item(0).getPortionFormat(); | |
portionFormat.getFillFormat().setFillType(FillType.Solid); | |
portionFormat.getFillFormat().getSolidFillColor().setColor(Color.RED); | |
portionFormat.setFontBold(NullableBool.True); | |
portionFormat.setFontItalic(NullableBool.True); | |
portionFormat.setFontHeight(14); | |
// Save the generated presentation on the disk | |
presentation.save("NewJavaPresentation.pptx", SaveFormat.Pptx); | |
} | |
} |
在 Java 演示文稿 中,可以使用上面示例中给出的几行代码来生成。您还可以使用 SaveFormat 枚举器将演示文稿保存为其他格式,如 PPT、PPS、PPSX、ODP、POT 和 POTX。演示文稿中的文本可以通过使用 ParagraphFormat 和 PortionFormat 类公开的不同选项进行自定义,其中包括设置文本换行、文本自动调整、缩进、边距、项目符号、文本突出显示和删除线等选项。
在本主题中,我们学习了如何使用不同格式的 Java PowerPoint 演示文稿 创建。如果您有兴趣将演示幻灯片转换为 SVG,请访问 如何使用 Java 将 PPTX 转换为 SVG 上的文章中提到的详细信息。