You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tutorial1.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.examples.xslf;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import org.apache.poi.xslf.usermodel.SlideLayout;
  23. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  24. import org.apache.poi.xslf.usermodel.XSLFSlide;
  25. import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
  26. import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
  27. import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
  28. import org.apache.poi.xslf.usermodel.XSLFTextShape;
  29. /**
  30. * Demonstrates how to create slides with predefined layout
  31. * and fill the placeholder shapes
  32. */
  33. public final class Tutorial1 {
  34. private Tutorial1() {}
  35. public static void main(String[] args) throws IOException{
  36. try (XMLSlideShow ppt = new XMLSlideShow()) {
  37. // XSLFSlide#createSlide() with no arguments creates a blank slide
  38. /*XSLFSlide blankSlide =*/
  39. ppt.createSlide();
  40. XSLFSlideMaster master = ppt.getSlideMasters().get(0);
  41. XSLFSlideLayout layout1 = master.getLayout(SlideLayout.TITLE);
  42. XSLFSlide slide1 = ppt.createSlide(layout1);
  43. XSLFTextShape[] ph1 = slide1.getPlaceholders();
  44. XSLFTextShape titlePlaceholder1 = ph1[0];
  45. titlePlaceholder1.setText("This is a title");
  46. XSLFTextShape subtitlePlaceholder1 = ph1[1];
  47. subtitlePlaceholder1.setText("this is a subtitle");
  48. XSLFSlideLayout layout2 = master.getLayout(SlideLayout.TITLE_AND_CONTENT);
  49. XSLFSlide slide2 = ppt.createSlide(layout2);
  50. XSLFTextShape[] ph2 = slide2.getPlaceholders();
  51. XSLFTextShape titlePlaceholder2 = ph2[0];
  52. titlePlaceholder2.setText("This is a title");
  53. XSLFTextShape bodyPlaceholder = ph2[1];
  54. // we are going to add text by paragraphs. Clear the default placehoder text before that
  55. bodyPlaceholder.clearText();
  56. XSLFTextParagraph p1 = bodyPlaceholder.addNewTextParagraph();
  57. p1.setIndentLevel(0);
  58. p1.addNewTextRun().setText("Level1 text");
  59. XSLFTextParagraph p2 = bodyPlaceholder.addNewTextParagraph();
  60. p2.setIndentLevel(1);
  61. p2.addNewTextRun().setText("Level2 text");
  62. XSLFTextParagraph p3 = bodyPlaceholder.addNewTextParagraph();
  63. p3.setIndentLevel(2);
  64. p3.addNewTextRun().setText("Level3 text");
  65. try (FileOutputStream out = new FileOutputStream("slides.pptx")) {
  66. ppt.write(out);
  67. }
  68. }
  69. }
  70. }