翻譯|使用教程|編輯:李顯亮|2021-01-21 10:05:36.097|閱讀 685 次
概述:PowerPoint演示文稿使您可以創(chuàng)建包含文字,圖形,圖表,動(dòng)畫和其他元素的精美幻燈片,以使演示文稿更具吸引力。在本文中,將學(xué)習(xí)如何使用Java實(shí)現(xiàn)PowerPoint自動(dòng)化功能。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
PowerPoint演示文稿使您可以創(chuàng)建包含文字,圖形,圖表,動(dòng)畫和其他元素的精美幻燈片,以使演示文稿更具吸引力。在本文中,將學(xué)習(xí)如何使用Java實(shí)現(xiàn)PowerPoint自動(dòng)化功能。特別是,將了解如何使用Java創(chuàng)建PowerPoint演示文稿并向幻燈片中添加各種元素。
為了實(shí)現(xiàn)PowerPoint自動(dòng)化功能,Aspose提供了Java API的Aspose.Slides。該API可以輕松地從Java應(yīng)用程序中創(chuàng)建,編輯,轉(zhuǎn)換和操作PowerPoint演示文稿。
>>你可以點(diǎn)擊這里下載Aspose.Slides for java最新版測(cè)試體驗(yàn)。
首先從PowerPoint自動(dòng)化開始,首先創(chuàng)建一個(gè)空的演示文稿文檔,然后將其另存為PPTX文件。以下是創(chuàng)建演示文稿文檔的步驟。
下面的代碼示例演示如何使用Java創(chuàng)建PowerPoint演示文稿。
// Instantiate a Presentation object that represents a presentation file Presentation presentation = new Presentation(); // Get the first slide ISlide slide = presentation.getSlides().get_Item(0); // Add content to slide... // Save presentation presentation.save("NewPresentation.pptx", SaveFormat.Pptx);
Aspose.Slides for Java還允許您打開現(xiàn)有的PowerPoint演示文稿以更新其內(nèi)容。以下是加載PowerPoint PPTX文件的步驟。
下面的代碼示例演示如何使用Java打開現(xiàn)有的PowerPoint演示文稿。
// Instantiate a Presentation object that represents a presentation file Presentation presentation = new Presentation("presentation.pptx"); // Get the first slide ISlide slide = presentation.getSlides().get_Item(0); // add or update content to slide... // Save presentation presentation.save("NewPresentation.pptx", SaveFormat.Pptx);
現(xiàn)在讓我們看一下如何將幻燈片添加到演示文稿文檔中。可以針對(duì)新演示文稿或現(xiàn)有演示文稿執(zhí)行此操作。以下是將幻燈片添加到演示文稿的步驟。
下面的代碼示例演示如何使用Java將幻燈片添加到演示文稿中。
// Instantiate a Presentation object that represents a presentation file Presentation presentation = new Presentation("presentation.pptx"); // Access the slides collection ISlideCollection 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);
創(chuàng)建演示文稿并向其中添加幻燈片后,就可以開始在其中插入不同的元素了。首先,讓我們看一下使用Aspose.Slides for Java將文本添加到幻燈片的步驟。
下面的代碼示例演示如何使用Java將文本添加到PowerPoint演示文稿中。
// Instantiate a Presentation object that represents a presentation file Presentation presentation = new Presentation("presentation.pptx"); // Get the first slide ISlide sld = (ISlide) presentation.getSlides().get_Item(0); // Add an AutoShape of Rectangle type IAutoShape ashp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 150, 50); // Add ITextFrame to the Rectangle ashp.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 White ashp.getShapeStyle().getLineColor().setColor(java.awt.Color.WHITE); // Remove any fill formatting in the shape ashp.getFillFormat().setFillType(FillType.NoFill); // Save presentation presentation.save("NewPresentation.pptx", SaveFormat.Pptx);
表是一個(gè)重要元素,用于以行和列的形式組織內(nèi)容。要將表格添加到幻燈片,可以按照以下步驟操作。
下面的代碼示例演示如何使用Java在PowerPoint演示文稿中創(chuàng)建表。
// Instantiate a Presentation object that represents a presentation file Presentation presentation = new Presentation("presentation.pptx"); // Access first slide ISlide sld = presentation.getSlides().get_Item(0); // Define columns with widths and rows with heights double[] dblCols = { 50, 50, 50 }; double[] dblRows = { 50, 30, 30, 30, 30 }; // Add table shape to slide ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows); // Set border format for each cell for (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在PowerPoint演示文稿中添加圖像的步驟。
下面的代碼示例演示如何使用Java將圖像添加到PowerPoint演示文稿中。
// Instantiate a Presentation object that represents a presentation file Presentation presentation = new Presentation("presentation.pptx"); // Access first slide ISlide sld = presentation.getSlides().get_Item(0); // Instantiate the IPPImage class IPPImage imgx = null; try { // Add image to slide imgx = presentation.getImages().addImage(new FileInputStream(new File("greentick.png"))); } catch (IOException e) { } // Add Picture Frame with height and width equivalent of Picture sld.getShapes().addPictureFrame(ShapeType.Rectangle, 50, 150, imgx.getWidth(), imgx.getHeight(), imgx); // Save presentation presentation.save("NewPresentation.pptx", SaveFormat.Pptx);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn