選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

XSLFPowerPointExtractor.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. extractor.close();
  67. }
  68. /**
  69. * Should a call to getText() return slide text?
  70. * Default is yes
  71. */
  72. public void setSlidesByDefault(boolean slidesByDefault) {
  73. this.slidesByDefault = slidesByDefault;
  74. }
  75. /**
  76. * Should a call to getText() return notes text?
  77. * Default is no
  78. */
  79. public void setNotesByDefault(boolean notesByDefault) {
  80. this.notesByDefault = notesByDefault;
  81. }
  82. /**
  83. * Should a call to getText() return text from master? Default is no
  84. */
  85. public void setMasterByDefault(boolean masterByDefault) {
  86. this.masterByDefault = masterByDefault;
  87. }
  88. /**
  89. * Gets the slide text, but not the notes text
  90. */
  91. public String getText() {
  92. return getText(slidesByDefault, notesByDefault);
  93. }
  94. /**
  95. * Gets the requested text from the file
  96. * @param slideText Should we retrieve text from slides?
  97. * @param notesText Should we retrieve text from notes?
  98. */
  99. public String getText(boolean slideText, boolean notesText) {
  100. return getText(slideText, notesText, masterByDefault);
  101. }
  102. /**
  103. * Gets the requested text from the file
  104. * @param slideText Should we retrieve text from slides?
  105. * @param notesText Should we retrieve text from notes?
  106. * @param masterText Should we retrieve text from master slides?
  107. */
  108. @SuppressWarnings("deprecation")
  109. public String getText(boolean slideText, boolean notesText, boolean masterText) {
  110. StringBuffer text = new StringBuffer();
  111. XSLFSlide[] slides = slideshow.getSlides();
  112. XSLFCommentAuthors commentAuthors = slideshow.getCommentAuthors();
  113. for (XSLFSlide slide : slides) {
  114. try {
  115. XSLFNotes notes = slide.getNotes();
  116. XSLFComments comments = slide.getComments();
  117. XSLFSlideLayout layout = slide.getSlideLayout();
  118. XSLFSlideMaster master = layout.getSlideMaster();
  119. // TODO Do the slide's name
  120. // (Stored in docProps/app.xml)
  121. // Do the slide's text if requested
  122. if (slideText) {
  123. extractText(slide.getCommonSlideData(), false, text);
  124. // If requested, get text from the master and it's layout
  125. if(masterText) {
  126. if(layout != null) {
  127. extractText(layout.getCommonSlideData(), true, text);
  128. }
  129. if(master != null) {
  130. extractText(master.getCommonSlideData(), true, text);
  131. }
  132. }
  133. // If the slide has comments, do those too
  134. if (comments != null) {
  135. for (CTComment comment : comments.getCTCommentsList().getCmArray()) {
  136. // Do the author if we can
  137. if (commentAuthors != null) {
  138. CTCommentAuthor author = commentAuthors.getAuthorById(comment.getAuthorId());
  139. if(author != null) {
  140. text.append(author.getName() + ": ");
  141. }
  142. }
  143. // Then the comment text, with a new line afterwards
  144. text.append(comment.getText());
  145. text.append("\n");
  146. }
  147. }
  148. }
  149. // Do the notes if requested
  150. if (notesText && notes != null) {
  151. extractText(notes.getCommonSlideData(), false, text);
  152. }
  153. } catch (Exception e) {
  154. throw new RuntimeException(e);
  155. }
  156. }
  157. return text.toString();
  158. }
  159. private void extractText(XSLFCommonSlideData data, boolean skipPlaceholders, StringBuffer text) {
  160. for(DrawingTextBody textBody : data.getDrawingText()) {
  161. if(skipPlaceholders && textBody instanceof DrawingTextPlaceholder) {
  162. DrawingTextPlaceholder ph = (DrawingTextPlaceholder)textBody;
  163. if(! ph.isPlaceholderCustom()) {
  164. // Skip non-customised placeholder text
  165. continue;
  166. }
  167. }
  168. for (DrawingParagraph p : textBody.getParagraphs()) {
  169. text.append(p.getText());
  170. text.append("\n");
  171. }
  172. }
  173. }
  174. }