PPT处理控件Aspose.Slides功能演示:使用Java创建PowerPoint演示文稿

  • 使用Java创建PowerPoint演示文稿
  • 打开现有的PowerPoint演示文稿
  • 将幻灯片添加到演示文稿
  • 将文本添加到演示文稿的幻灯片
  • 在演示文稿中创建表
  • 向演示文稿添加图像

>>你可以点击这里下载Aspose.Slides for java最新版测试体验。

整合所有格式API处理控件Aspose.Total永久授权火热促销中,联系客服立马1分钟了解全部!


使用Java创建PowerPoint演示文稿

首先从PowerPoint自动化开始,首先创建一个空的演示文稿文档,然后将其另存为PPTX文件。以下是创建演示文稿文档的步骤。

  • 创建Presentation 类的实例 。
  • 使用Presentation.save(String,SaveFormat) 方法将其另存为PPTX 。

下面的代码示例演示如何使用Java创建PowerPoint演示文稿。

// Instantiate a Presentation object that represents a presentation filePresentation presentation = new Presentation();// Get the first slideISlide slide = presentation.getSlides().get_Item(0);// Add content to slide...// Save presentationpresentation.save("NewPresentation.pptx", SaveFormat.Pptx);

使用Java打开现有的PowerPoint演示文稿

Aspose.Slides for Java还允许您打开现有的PowerPoint演示文稿以更新其内容。以下是加载PowerPoint PPTX文件的步骤。

  • 创建Presentation 类的实例, 并提供PPTX文件的构造函数路径。
  • 更新演示文稿的内容。
  • 使用Presentation.save(String,SaveFormat) 方法保存更新的演示 文稿。

下面的代码示例演示如何使用Java打开现有的PowerPoint演示文稿。

// Instantiate a Presentation object that represents a presentation filePresentation presentation = new Presentation("presentation.pptx");// Get the first slideISlide slide = presentation.getSlides().get_Item(0);// add or update content to slide...// Save presentationpresentation.save("NewPresentation.pptx", SaveFormat.Pptx);

使用Java将幻灯片添加到演示文稿中

现在让我们看一下如何将幻灯片添加到演示文稿文档中。可以针对新演示文稿或现有演示文稿执行此操作。以下是将幻灯片添加到演示文稿的步骤。

  • 创建Presentation 类的实例, 并提供PPTX文件的构造函数路径。
  • 通过设置对Presentation.getSlides()的引用来实例化 ISlideCollection类 。
  • 使用ISlideCollection 对象 公开的ISlideCollection.addEmptySlide (ILayoutSlide)方法 向演示文稿添加一个空幻灯片 。
  • 使用Presentation.save(String,SaveFormat) 方法保存更新的演示 文稿。

下面的代码示例演示如何使用Java将幻灯片添加到演示文稿中。

// Instantiate a Presentation object that represents a presentation filePresentation presentation = new Presentation("presentation.pptx");// Access the slides collectionISlideCollection slds = presentation.getSlides();for (int i = 0; i < presentation.getLayoutSlides().size(); i++) { // Add an empty slide to the Slides collection slds.addEmptySlide(presentation.getLayoutSlides().get_Item(i)); } // Save presentation presentation.save("NewPresentation.pptx", SaveFormat.Pptx); 

使用Java将文本添加到演示幻灯片

创建演示文稿并向其中添加幻灯片后,就可以开始在其中插入不同的元素了。首先,让我们看一下使用Aspose.Slides for Java将文本添加到幻灯片的步骤。

  • 创建Presentation 类的实例, 并提供PPTX文件的构造函数路径。
  • 获取要在ISlide对象中添加文本的幻灯片的引用。
  • 使用ISlide.getShapes()。addAutoShape()方法添加一个矩形,并在IAutoShape 对象中获取其引用 。
  • 将TextFrame添加 到包含默认文本的形状中。
  • 设置文本的属性,例如填充颜色,填充类型等。
  • 使用Presentation.save(String,SaveFormat) 方法保存更新的演示 文稿。

下面的代码示例演示如何使用Java将文本添加到PowerPoint演示文稿中。

// Instantiate a Presentation object that represents a presentation filePresentation presentation = new Presentation("presentation.pptx");// Get the first slideISlide sld = (ISlide) presentation.getSlides().get_Item(0);// Add an AutoShape of Rectangle typeIAutoShape ashp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);// Add ITextFrame to the Rectangleashp.addTextFrame("Hello World");// Change the text color to Black (which is White by default)ashp.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().getFillFormat().setFillType(FillType.Solid);ashp.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.awt.Color.BLACK);// Change the line color of the rectangle to Whiteashp.getShapeStyle().getLineColor().setColor(java.awt.Color.WHITE);// Remove any fill formatting in the shapeashp.getFillFormat().setFillType(FillType.NoFill);// Save presentationpresentation.save("NewPresentation.pptx", SaveFormat.Pptx);

使用Java在演示文稿中创建表

表是一个重要元素,用于以行和列的形式组织内容。要将表格添加到幻灯片,可以按照以下步骤操作。

  • 创建Presentation 类的实例, 并提供PPTX文件的构造函数路径。
  • 获取要向其添加文本的幻灯片的参考。
  • 创建一个列宽数组。
  • 创建行高的数组。
  • 使用ISlide.getShapes()。addTable()方法将表添加到幻灯片中,并获取对ITable对象的引用。
  • 遍历每个单元格以将格式应用于“上”,“下”,“右”和“左”边框。
  • 在单元格中添加一些文本。
  • 使用Presentation.save(String,SaveFormat) 方法保存更新的演示 文稿。

下面的代码示例演示如何使用Java在PowerPoint演示文稿中创建表。

// Instantiate a Presentation object that represents a presentation filePresentation presentation = new Presentation("presentation.pptx");// Access first slideISlide sld = presentation.getSlides().get_Item(0);// Define columns with widths and rows with heightsdouble[] dblCols = { 50, 50, 50 };double[] dblRows = { 50, 30, 30, 30, 30 };// Add table shape to slideITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);// Set border format for each cellfor (int row = 0; row < tbl.getRows().size(); row++) { for (int cell = 0; cell < tbl.getRows().get_Item(row).size(); cell++) { tbl.getRows().get_Item(row).get_Item(cell).getBorderTop().getFillFormat().setFillType(FillType.Solid); tbl.getRows().get_Item(row).get_Item(cell).getBorderTop().getFillFormat().getSolidFillColor() .setColor(Color.RED); tbl.getRows().get_Item(row).get_Item(cell).getBorderTop().setWidth(5); tbl.getRows().get_Item(row).get_Item(cell).getBorderBottom().getFillFormat() .setFillType(FillType.Solid); tbl.getRows().get_Item(row).get_Item(cell).getBorderBottom().getFillFormat().getSolidFillColor() .setColor(Color.RED); tbl.getRows().get_Item(row).get_Item(cell).getBorderBottom().setWidth(5); tbl.getRows().get_Item(row).get_Item(cell).getBorderLeft().getFillFormat().setFillType(FillType.Solid); tbl.getRows().get_Item(row).get_Item(cell).getBorderLeft().getFillFormat().getSolidFillColor() .setColor(Color.RED); tbl.getRows().get_Item(row).get_Item(cell).getBorderLeft().setWidth(5); tbl.getRows().get_Item(row).get_Item(cell).getBorderRight().getFillFormat().setFillType(FillType.Solid); tbl.getRows().get_Item(row).get_Item(cell).getBorderRight().getFillFormat().getSolidFillColor() .setColor(Color.RED); tbl.getRows().get_Item(row).get_Item(cell).getBorderRight().setWidth(5); } } // Merge cells 1 & 2 of row 1 tbl.mergeCells(tbl.getRows().get_Item(0).get_Item(0), tbl.getRows().get_Item(1).get_Item(0), false); // Add text to the merged cell tbl.getRows().get_Item(0).get_Item(0).getTextFrame().setText("Merged Cells"); // Save presentation presentation.save("NewPresentation.pptx", SaveFormat.Pptx); 

使用Java在演示文稿中添加图像

以下是使用Java在PowerPoint演示文稿中添加图像的步骤。

  • 创建Presentation 类的实例, 并提供PPTX文件的构造函数路径。
  • 在ISlide对象中获取幻灯片的引用。
  • 创建一个IPPImage类的对象。
  • 使用Presentation.getImages()。addImage(FileInputStream)方法将图像添加到演示文稿中。
  • 将图像作为相框添加到幻灯片上,其高度和宽度与图像相等。
  • 使用Presentation.save(String,SaveFormat) 方法保存更新的演示 文稿。

下面的代码示例演示如何使用Java将图像添加到PowerPoint演示文稿中。

// Instantiate a Presentation object that represents a presentation filePresentation presentation = new Presentation("presentation.pptx");// Access first slideISlide sld = presentation.getSlides().get_Item(0);// Instantiate the IPPImage classIPPImage imgx = null;try {// Add image to slideimgx = presentation.getImages().addImage(new FileInputStream(new File("greentick.png")));}catch (IOException e) {}// Add Picture Frame with height and width equivalent of Picturesld.getShapes().addPictureFrame(ShapeType.Rectangle, 50, 150, imgx.getWidth(), imgx.getHeight(), imgx);// Save presentationpresentation.save("NewPresentation.pptx", SaveFormat.Pptx);


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月18日
下一篇 2021年1月18日

相关推荐

发表回复

登录后才能评论