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.

XSLFPowerPointExtractor.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xslf.extractor;
  16. import java.io.IOException;
  17. import org.apache.poi.POIXMLTextExtractor;
  18. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  19. import org.apache.poi.openxml4j.opc.OPCPackage;
  20. import org.apache.poi.xslf.XSLFSlideShow;
  21. import org.apache.poi.xslf.usermodel.DrawingParagraph;
  22. import org.apache.poi.xslf.usermodel.DrawingTextBody;
  23. import org.apache.poi.xslf.usermodel.DrawingTextPlaceholder;
  24. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  25. import org.apache.poi.xslf.usermodel.XSLFCommentAuthors;
  26. import org.apache.poi.xslf.usermodel.XSLFComments;
  27. import org.apache.poi.xslf.usermodel.XSLFCommonSlideData;
  28. import org.apache.poi.xslf.usermodel.XSLFNotes;
  29. import org.apache.poi.xslf.usermodel.XSLFRelation;
  30. import org.apache.poi.xslf.usermodel.XSLFSlide;
  31. import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
  32. import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
  33. import org.apache.xmlbeans.XmlException;
  34. import org.openxmlformats.schemas.presentationml.x2006.main.CTComment;
  35. import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthor;
  36. public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
  37. public static final XSLFRelation[] SUPPORTED_TYPES = new XSLFRelation[] {
  38. XSLFRelation.MAIN, XSLFRelation.MACRO, XSLFRelation.MACRO_TEMPLATE,
  39. XSLFRelation.PRESENTATIONML, XSLFRelation.PRESENTATIONML_TEMPLATE,
  40. XSLFRelation.PRESENTATION_MACRO
  41. };
  42. private XMLSlideShow slideshow;
  43. private boolean slidesByDefault = true;
  44. private boolean notesByDefault = false;
  45. private boolean masterByDefault = false;
  46. public XSLFPowerPointExtractor(XMLSlideShow slideshow) {
  47. super(slideshow);
  48. this.slideshow = slideshow;
  49. }
  50. public XSLFPowerPointExtractor(XSLFSlideShow slideshow) throws XmlException, IOException {
  51. this(new XMLSlideShow(slideshow.getPackage()));
  52. }
  53. public XSLFPowerPointExtractor(OPCPackage container) throws XmlException, OpenXML4JException, IOException {
  54. this(new XSLFSlideShow(container));
  55. }
  56. public static void main(String[] args) throws Exception {
  57. if(args.length < 1) {
  58. System.err.println("Use:");
  59. System.err.println(" XSLFPowerPointExtractor <filename.pptx>");
  60. System.exit(1);
  61. }
  62. POIXMLTextExtractor extractor =
  63. new XSLFPowerPointExtractor(
  64. new XSLFSlideShow(args[0]));
  65. System.out.println(extractor.getText());
  66. }
  67. /**
  68. * Should a call to getText() return slide text?
  69. * Default is yes
  70. */
  71. public void setSlidesByDefault(boolean slidesByDefault) {
  72. this.slidesByDefault = slidesByDefault;
  73. }
  74. /**
  75. * Should a call to getText() return notes text?
  76. * Default is no
  77. */
  78. public void setNotesByDefault(boolean notesByDefault) {
  79. this.notesByDefault = notesByDefault;
  80. }
  81. /**
  82. * Should a call to getText() return text from master? Default is no
  83. */
  84. public void setMasterByDefault(boolean masterByDefault) {
  85. this.masterByDefault = masterByDefault;
  86. }
  87. /**
  88. * Gets the slide text, but not the notes text
  89. */
  90. public String getText() {
  91. return getText(slidesByDefault, notesByDefault);
  92. }
  93. /**
  94. * Gets the requested text from the file
  95. * @param slideText Should we retrieve text from slides?
  96. * @param notesText Should we retrieve text from notes?
  97. */
  98. public String getText(boolean slideText, boolean notesText) {
  99. return getText(slideText, notesText, masterByDefault);
  100. }
  101. /**
  102. * Gets the requested text from the file
  103. * @param slideText Should we retrieve text from slides?
  104. * @param notesText Should we retrieve text from notes?
  105. * @param masterText Should we retrieve text from master slides?
  106. */
  107. public String getText(boolean slideText, boolean notesText, boolean masterText) {
  108. StringBuffer text = new StringBuffer();
  109. XSLFSlide[] slides = slideshow.getSlides();
  110. XSLFCommentAuthors commentAuthors = slideshow.getCommentAuthors();
  111. for (XSLFSlide slide : slides) {
  112. try {
  113. XSLFNotes notes = slide.getNotes();
  114. XSLFComments comments = slide.getComments();
  115. XSLFSlideLayout layout = slide.getSlideLayout();
  116. XSLFSlideMaster master = layout.getSlideMaster();
  117. // TODO Do the slide's name
  118. // (Stored in docProps/app.xml)
  119. // Do the slide's text if requested
  120. if (slideText) {
  121. extractText(slide.getCommonSlideData(), false, text);
  122. // If requested, get text from the master and it's layout
  123. if(masterText) {
  124. if(layout != null) {
  125. extractText(layout.getCommonSlideData(), true, text);
  126. }
  127. if(master != null) {
  128. extractText(master.getCommonSlideData(), true, text);
  129. }
  130. }
  131. // If the slide has comments, do those too
  132. if (comments != null) {
  133. for (CTComment comment : comments.getCTCommentsList().getCmList()) {
  134. // Do the author if we can
  135. if (commentAuthors != null) {
  136. CTCommentAuthor author = commentAuthors.getAuthorById(comment.getAuthorId());
  137. if(author != null) {
  138. text.append(author.getName() + ": ");
  139. }
  140. }
  141. // Then the comment text, with a new line afterwards
  142. text.append(comment.getText());
  143. text.append("\n");
  144. }
  145. }
  146. }
  147. // Do the notes if requested
  148. if (notesText && notes != null) {
  149. extractText(notes.getCommonSlideData(), false, text);
  150. }
  151. } catch (Exception e) {
  152. throw new RuntimeException(e);
  153. }
  154. }
  155. return text.toString();
  156. }
  157. private void extractText(XSLFCommonSlideData data, boolean skipPlaceholders, StringBuffer text) {
  158. for(DrawingTextBody textBody : data.getDrawingText()) {
  159. if(skipPlaceholders && textBody instanceof DrawingTextPlaceholder) {
  160. DrawingTextPlaceholder ph = (DrawingTextPlaceholder)textBody;
  161. if(! ph.isPlaceholderCustom()) {
  162. // Skip non-customised placeholder text
  163. continue;
  164. }
  165. }
  166. for (DrawingParagraph p : textBody.getParagraphs()) {
  167. text.append(p.getText());
  168. text.append("\n");
  169. }
  170. }
  171. }
  172. }