本教程包含有关如何使用 Java 以 HTML 格式创建 PowerPoint 幻灯片的信息。这是一个非常简单的过程,其中会创建一个新的 presentation 或加载现有的演示文稿,然后将其另存为 HTML。如果您想将现有演示文稿呈现为 HTML,这使您能够借助几行代码在 Java 中呈现**幻灯片。
使用 Java 创建 HTML 演示文稿的步骤
- 将 Aspose.Slides for Java 从 Maven 存储库添加到您的项目中,以创建 HTML 演示文稿
- 创建一个空的 Presentation 类对象
- 创建一个 Slide 类对象并为其添加菱形
- 在菱形中设置一些文本
- 将生成的演示文稿另存为磁盘上的 HTML 文件
这些步骤有助于使用 Java* 创建一个 *HTML 演示文稿,其中创建一个新演示文稿并将其保存为可以在任何浏览器中打开的 HTML。请注意,不必从头开始创建演示文稿,因为您也可以加载现有演示文稿来创建 HTML 演示文稿。如果需要,您还可以将选定的幻灯片保存到输出 HTML 文件中。
使用 Java 创建 HTML 演示幻灯片的代码
import com.aspose.slides.License; | |
import com.aspose.slides.IAutoShape; | |
import com.aspose.slides.IParagraph; | |
import com.aspose.slides.IPortion; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.ITextFrame; | |
import com.aspose.slides.Presentation; | |
import com.aspose.slides.SaveFormat; | |
import com.aspose.slides.ShapeType; | |
public class SlidesMainFunction { | |
public static void main(String[] args) {//Create PowerPoint Slides in HTML using Java | |
// Instantiate license to create presentation in HTML | |
License slidesLicense = new License(); | |
slidesLicense.setLicense("Aspose.Slides.lic"); | |
//Create an empty presentation class object | |
Presentation targetPresentation = new Presentation(); | |
try | |
{ | |
// Get access to the first slide instance from the default slides collection | |
ISlide targetSlide = targetPresentation.getSlides().get_Item(0); | |
// Add a diamond shape to display some text in it | |
IAutoShape targetShape = targetSlide.getShapes().addAutoShape(ShapeType.Diamond, 150, 75, 150, 50); | |
// Add a text frame in the diamond | |
targetShape.addTextFrame(" "); | |
// Get access to the newly created text frame | |
ITextFrame targetTxtFrame = targetShape.getTextFrame(); | |
// Get access to the first paragraph object in the text frame | |
IParagraph targetPara = targetTxtFrame.getParagraphs().get_Item(0); | |
// Get access to the first portion in the paragraph | |
IPortion targetPortion = targetPara.getPortions().get_Item(0); | |
// Add some test data to display in diamond shape | |
targetPortion.setText("Aspose sample text in the textbox"); | |
// Save the HTML presentation on the disk | |
targetPresentation.save("TextBox_out.html", SaveFormat.Html); | |
} | |
finally | |
{ | |
if (targetPresentation != null) | |
targetPresentation.dispose(); | |
} | |
System.out.println("Done"); | |
} | |
} |
此代码详细演示了使用 Java* 的 HTML 演示的示例之一,以便呈现完整的操作序列,最终生成 HTML 演示。可以观察到,我们需要依次创建 Presentation、ISlide、IAutoShape、ITextFrame、IParagraph 和 IPortion 类的对象来创建一个新的演示文稿。您还可以创建多张幻灯片并添加类似于 MS PowerPoint 的不同类型的内容。
在本文中,我们学习了以 HTML 格式创建 PowerPoint 演示文稿。但是,如果您想保护演示文稿,请参阅 如何使用 Java 保护 PowerPoint 演示文稿 上的文章。