Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

XSLFPowerPointExtractor.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 org.apache.poi.POIXMLTextExtractor;
  17. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  18. import org.apache.poi.openxml4j.opc.OPCPackage;
  19. import org.apache.poi.xslf.XSLFSlideShow;
  20. import org.apache.poi.xslf.usermodel.DrawingParagraph;
  21. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  22. import org.apache.poi.xslf.usermodel.XSLFCommonSlideData;
  23. import org.apache.poi.xslf.usermodel.XSLFSlide;
  24. import org.apache.xmlbeans.XmlException;
  25. import org.openxmlformats.schemas.presentationml.x2006.main.*;
  26. import java.io.IOException;
  27. public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
  28. private XMLSlideShow slideshow;
  29. private boolean slidesByDefault = true;
  30. private boolean notesByDefault = false;
  31. public XSLFPowerPointExtractor(XMLSlideShow slideshow) {
  32. super(slideshow._getXSLFSlideShow());
  33. this.slideshow = slideshow;
  34. }
  35. public XSLFPowerPointExtractor(XSLFSlideShow slideshow) throws XmlException, IOException {
  36. this(new XMLSlideShow(slideshow));
  37. }
  38. public XSLFPowerPointExtractor(OPCPackage container) throws XmlException, OpenXML4JException, IOException {
  39. this(new XSLFSlideShow(container));
  40. }
  41. public static void main(String[] args) throws Exception {
  42. if(args.length < 1) {
  43. System.err.println("Use:");
  44. System.err.println(" HXFPowerPointExtractor <filename.pptx>");
  45. System.exit(1);
  46. }
  47. POIXMLTextExtractor extractor =
  48. new XSLFPowerPointExtractor(
  49. new XSLFSlideShow(args[0]));
  50. System.out.println(extractor.getText());
  51. }
  52. /**
  53. * Should a call to getText() return slide text?
  54. * Default is yes
  55. */
  56. public void setSlidesByDefault(boolean slidesByDefault) {
  57. this.slidesByDefault = slidesByDefault;
  58. }
  59. /**
  60. * Should a call to getText() return notes text?
  61. * Default is no
  62. */
  63. public void setNotesByDefault(boolean notesByDefault) {
  64. this.notesByDefault = notesByDefault;
  65. }
  66. /**
  67. * Gets the slide text, but not the notes text
  68. */
  69. public String getText() {
  70. return getText(slidesByDefault, notesByDefault);
  71. }
  72. /**
  73. * Gets the requested text from the file
  74. * @param slideText Should we retrieve text from slides?
  75. * @param notesText Should we retrieve text from notes?
  76. */
  77. public String getText(boolean slideText, boolean notesText) {
  78. StringBuffer text = new StringBuffer();
  79. XSLFSlide[] slides = slideshow.getSlides();
  80. for(int i = 0; i < slides.length; i++) {
  81. CTSlide rawSlide = slides[i]._getCTSlide();
  82. CTSlideIdListEntry slideId = slides[i]._getCTSlideId();
  83. try {
  84. // For now, still very low level
  85. CTNotesSlide notes =
  86. slideshow._getXSLFSlideShow().getNotes(slideId);
  87. CTCommentList comments =
  88. slideshow._getXSLFSlideShow().getSlideComments(slideId);
  89. if(slideText) {
  90. extractText(slides[i].getCommonSlideData(), text);
  91. // Comments too for the slide
  92. if(comments != null) {
  93. for(CTComment comment : comments.getCmArray()) {
  94. // TODO - comment authors too
  95. // (They're in another stream)
  96. text.append(
  97. comment.getText() + "\n"
  98. );
  99. }
  100. }
  101. }
  102. if(notesText && notes != null) {
  103. extractText(new XSLFCommonSlideData(notes.getCSld()), text);
  104. }
  105. } catch(Exception e) {
  106. throw new RuntimeException(e);
  107. }
  108. }
  109. return text.toString();
  110. }
  111. private void extractText(XSLFCommonSlideData data, StringBuffer text) {
  112. for (DrawingParagraph p : data.getText()) {
  113. text.append(p.getText());
  114. text.append("\n");
  115. }
  116. }
  117. }