Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created January 17, 2021 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/5f98550474ef95055a837ce652e4a328 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/5f98550474ef95055a837ce652e4a328 to your computer and use it in GitHub Desktop.
Create PowerPoint Presentations in Java
// 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);
// 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);
// 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);
// 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);
// 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);
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment