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.

Tutorial2.java 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.awt.Color;
  21. import java.awt.Rectangle;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  25. import org.apache.poi.xslf.usermodel.XSLFSlide;
  26. import org.apache.poi.xslf.usermodel.XSLFTextBox;
  27. import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
  28. import org.apache.poi.xslf.usermodel.XSLFTextRun;
  29. /**
  30. * Basic paragraph and text formatting
  31. */
  32. public final class Tutorial2 {
  33. private Tutorial2() {}
  34. public static void main(String[] args) throws IOException{
  35. try (XMLSlideShow ppt = new XMLSlideShow()) {
  36. XSLFSlide slide1 = ppt.createSlide();
  37. XSLFTextBox shape1 = slide1.createTextBox();
  38. // initial height of the text box is 100 pt but
  39. Rectangle anchor = new Rectangle(10, 100, 300, 100);
  40. shape1.setAnchor(anchor);
  41. XSLFTextParagraph p1 = shape1.addNewTextParagraph();
  42. XSLFTextRun r1 = p1.addNewTextRun();
  43. r1.setText("Paragraph Formatting");
  44. r1.setFontSize(24d);
  45. r1.setFontColor(new Color(85, 142, 213));
  46. XSLFTextParagraph p2 = shape1.addNewTextParagraph();
  47. // If spaceBefore >= 0, then space is a percentage of normal line height.
  48. // If spaceBefore < 0, the absolute value of linespacing is the spacing in points
  49. p2.setSpaceBefore(-20d); // 20 pt from the previous paragraph
  50. p2.setSpaceAfter(300d); // 3 lines after the paragraph
  51. XSLFTextRun r2 = p2.addNewTextRun();
  52. r2.setText("Paragraph properties apply to all text residing within the corresponding paragraph.");
  53. r2.setFontSize(16d);
  54. XSLFTextParagraph p3 = shape1.addNewTextParagraph();
  55. XSLFTextRun r3 = p3.addNewTextRun();
  56. r3.setText("Run Formatting");
  57. r3.setFontSize(24d);
  58. r3.setFontColor(new Color(85, 142, 213));
  59. XSLFTextParagraph p4 = shape1.addNewTextParagraph();
  60. p4.setSpaceBefore(-20d); // 20 pt from the previous paragraph
  61. p4.setSpaceAfter(300d); // 3 lines after the paragraph
  62. XSLFTextRun r4 = p4.addNewTextRun();
  63. r4.setFontSize(16d);
  64. r4.setText(
  65. "Run level formatting is the most granular property level and allows " +
  66. "for the specifying of all low level text properties. The text run is " +
  67. "what all paragraphs are derived from and thus specifying various " +
  68. "properties per run will allow for a diversely formatted text paragraph.");
  69. // resize the shape to fit text
  70. shape1.resizeToFitText();
  71. try (FileOutputStream out = new FileOutputStream("text.pptx")) {
  72. ppt.write(out);
  73. }
  74. }
  75. }
  76. }