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.

PowerPointExtractor.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.hslf.extractor;
  16. import java.io.*;
  17. import java.util.*;
  18. import org.apache.poi.POIOLE2TextExtractor;
  19. import org.apache.poi.hslf.model.*;
  20. import org.apache.poi.hslf.usermodel.*;
  21. import org.apache.poi.poifs.filesystem.*;
  22. /**
  23. * This class can be used to extract text from a PowerPoint file. Can optionally
  24. * also get the notes from one.
  25. *
  26. * @author Nick Burch
  27. */
  28. public final class PowerPointExtractor extends POIOLE2TextExtractor {
  29. private HSLFSlideShowImpl _hslfshow;
  30. private HSLFSlideShow _show;
  31. private List<HSLFSlide> _slides;
  32. private boolean _slidesByDefault = true;
  33. private boolean _notesByDefault = false;
  34. private boolean _commentsByDefault = false;
  35. private boolean _masterByDefault = false;
  36. /**
  37. * Basic extractor. Returns all the text, and optionally all the notes
  38. */
  39. public static void main(String args[]) throws IOException {
  40. if (args.length < 1) {
  41. System.err.println("Useage:");
  42. System.err.println("\tPowerPointExtractor [-notes] <file>");
  43. System.exit(1);
  44. }
  45. boolean notes = false;
  46. boolean comments = false;
  47. boolean master = true;
  48. String file;
  49. if (args.length > 1) {
  50. notes = true;
  51. file = args[1];
  52. if (args.length > 2) {
  53. comments = true;
  54. }
  55. } else {
  56. file = args[0];
  57. }
  58. PowerPointExtractor ppe = new PowerPointExtractor(file);
  59. System.out.println(ppe.getText(true, notes, comments, master));
  60. ppe.close();
  61. }
  62. /**
  63. * Creates a PowerPointExtractor, from a file
  64. *
  65. * @param fileName The name of the file to extract from
  66. */
  67. public PowerPointExtractor(String fileName) throws IOException {
  68. this(new FileInputStream(fileName));
  69. }
  70. /**
  71. * Creates a PowerPointExtractor, from an Input Stream
  72. *
  73. * @param iStream The input stream containing the PowerPoint document
  74. */
  75. public PowerPointExtractor(InputStream iStream) throws IOException {
  76. this(new POIFSFileSystem(iStream));
  77. }
  78. /**
  79. * Creates a PowerPointExtractor, from an open POIFSFileSystem
  80. *
  81. * @param fs the POIFSFileSystem containing the PowerPoint document
  82. */
  83. public PowerPointExtractor(POIFSFileSystem fs) throws IOException {
  84. this(fs.getRoot());
  85. }
  86. /**
  87. * Creates a PowerPointExtractor, from an open NPOIFSFileSystem
  88. *
  89. * @param fs the NPOIFSFileSystem containing the PowerPoint document
  90. */
  91. public PowerPointExtractor(NPOIFSFileSystem fs) throws IOException {
  92. this(fs.getRoot());
  93. }
  94. /**
  95. * Creates a PowerPointExtractor, from a specific place
  96. * inside an open NPOIFSFileSystem
  97. *
  98. * @param dir the POIFS Directory containing the PowerPoint document
  99. */
  100. public PowerPointExtractor(DirectoryNode dir) throws IOException {
  101. this(new HSLFSlideShowImpl(dir));
  102. }
  103. /**
  104. * @deprecated Use {@link #PowerPointExtractor(DirectoryNode)} instead
  105. */
  106. @Deprecated
  107. public PowerPointExtractor(DirectoryNode dir, POIFSFileSystem fs) throws IOException {
  108. this(new HSLFSlideShowImpl(dir, fs));
  109. }
  110. /**
  111. * Creates a PowerPointExtractor, from a HSLFSlideShow
  112. *
  113. * @param ss the HSLFSlideShow to extract text from
  114. */
  115. public PowerPointExtractor(HSLFSlideShowImpl ss) {
  116. super(ss);
  117. _hslfshow = ss;
  118. _show = new HSLFSlideShow(_hslfshow);
  119. _slides = _show.getSlides();
  120. }
  121. /**
  122. * Should a call to getText() return slide text? Default is yes
  123. */
  124. public void setSlidesByDefault(boolean slidesByDefault) {
  125. this._slidesByDefault = slidesByDefault;
  126. }
  127. /**
  128. * Should a call to getText() return notes text? Default is no
  129. */
  130. public void setNotesByDefault(boolean notesByDefault) {
  131. this._notesByDefault = notesByDefault;
  132. }
  133. /**
  134. * Should a call to getText() return comments text? Default is no
  135. */
  136. public void setCommentsByDefault(boolean commentsByDefault) {
  137. this._commentsByDefault = commentsByDefault;
  138. }
  139. /**
  140. * Should a call to getText() return text from master? Default is no
  141. */
  142. public void setMasterByDefault(boolean masterByDefault) {
  143. this._masterByDefault = masterByDefault;
  144. }
  145. /**
  146. * Fetches all the slide text from the slideshow, but not the notes, unless
  147. * you've called setSlidesByDefault() and setNotesByDefault() to change this
  148. */
  149. public String getText() {
  150. return getText(_slidesByDefault, _notesByDefault, _commentsByDefault, _masterByDefault);
  151. }
  152. /**
  153. * Fetches all the notes text from the slideshow, but not the slide text
  154. */
  155. public String getNotes() {
  156. return getText(false, true);
  157. }
  158. public List<OLEShape> getOLEShapes() {
  159. List<OLEShape> list = new ArrayList<OLEShape>();
  160. for (HSLFSlide slide : _slides) {
  161. for (HSLFShape shape : slide.getShapes()) {
  162. if (shape instanceof OLEShape) {
  163. list.add((OLEShape) shape);
  164. }
  165. }
  166. }
  167. return list;
  168. }
  169. /**
  170. * Fetches text from the slideshow, be it slide text or note text. Because
  171. * the final block of text in a TextRun normally have their last \n
  172. * stripped, we add it back
  173. *
  174. * @param getSlideText fetch slide text
  175. * @param getNoteText fetch note text
  176. */
  177. public String getText(boolean getSlideText, boolean getNoteText) {
  178. return getText(getSlideText, getNoteText, _commentsByDefault, _masterByDefault);
  179. }
  180. public String getText(boolean getSlideText, boolean getNoteText, boolean getCommentText, boolean getMasterText) {
  181. StringBuffer ret = new StringBuffer();
  182. if (getSlideText) {
  183. if (getMasterText) {
  184. for (HSLFSlideMaster master : _show.getSlideMasters()) {
  185. for(HSLFShape sh : master.getShapes()){
  186. if(sh instanceof HSLFTextShape){
  187. if(HSLFMasterSheet.isPlaceholder(sh)) {
  188. // don't bother about boiler
  189. // plate text on master
  190. // sheets
  191. continue;
  192. }
  193. HSLFTextShape tsh = (HSLFTextShape)sh;
  194. String text = tsh.getText();
  195. if (text != null){
  196. ret.append(text);
  197. if (!text.endsWith("\n")) {
  198. ret.append("\n");
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. for (int i = 0; i < _slides.size(); i++) {
  206. HSLFSlide slide = _slides.get(i);
  207. // Slide header, if set
  208. HeadersFooters hf = slide.getHeadersFooters();
  209. if (hf != null && hf.isHeaderVisible() && hf.getHeaderText() != null) {
  210. ret.append(hf.getHeaderText() + "\n");
  211. }
  212. // Slide text
  213. textRunsToText(ret, slide.getTextParagraphs());
  214. // Table text
  215. for (HSLFShape shape : slide.getShapes()){
  216. if (shape instanceof HSLFTable){
  217. extractTableText(ret, (HSLFTable)shape);
  218. }
  219. }
  220. // Slide footer, if set
  221. if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) {
  222. ret.append(hf.getFooterText() + "\n");
  223. }
  224. // Comments, if requested and present
  225. if (getCommentText) {
  226. Comment[] comments = slide.getComments();
  227. for (int j = 0; j < comments.length; j++) {
  228. ret.append(comments[j].getAuthor() + " - " + comments[j].getText() + "\n");
  229. }
  230. }
  231. }
  232. if (getNoteText) {
  233. ret.append("\n");
  234. }
  235. }
  236. if (getNoteText) {
  237. // Not currently using _notes, as that can have the notes of
  238. // master sheets in. Grab Slide list, then work from there,
  239. // but ensure no duplicates
  240. HashSet<Integer> seenNotes = new HashSet<Integer>();
  241. HeadersFooters hf = _show.getNotesHeadersFooters();
  242. for (int i = 0; i < _slides.size(); i++) {
  243. HSLFNotes notes = _slides.get(i).getNotes();
  244. if (notes == null) {
  245. continue;
  246. }
  247. Integer id = Integer.valueOf(notes._getSheetNumber());
  248. if (seenNotes.contains(id)) {
  249. continue;
  250. }
  251. seenNotes.add(id);
  252. // Repeat the Notes header, if set
  253. if (hf != null && hf.isHeaderVisible() && hf.getHeaderText() != null) {
  254. ret.append(hf.getHeaderText() + "\n");
  255. }
  256. // Notes text
  257. textRunsToText(ret, notes.getTextParagraphs());
  258. // Repeat the notes footer, if set
  259. if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) {
  260. ret.append(hf.getFooterText() + "\n");
  261. }
  262. }
  263. }
  264. return ret.toString();
  265. }
  266. private void extractTableText(StringBuffer ret, HSLFTable table) {
  267. for (int row = 0; row < table.getNumberOfRows(); row++){
  268. for (int col = 0; col < table.getNumberOfColumns(); col++){
  269. HSLFTableCell cell = table.getCell(row, col);
  270. //defensive null checks; don't know if they're necessary
  271. if (cell != null){
  272. String txt = cell.getText();
  273. txt = (txt == null) ? "" : txt;
  274. ret.append(txt);
  275. if (col < table.getNumberOfColumns()-1){
  276. ret.append("\t");
  277. }
  278. }
  279. }
  280. ret.append('\n');
  281. }
  282. }
  283. private void textRunsToText(StringBuffer ret, List<List<HSLFTextParagraph>> paragraphs) {
  284. if (paragraphs==null) {
  285. return;
  286. }
  287. for (List<HSLFTextParagraph> lp : paragraphs) {
  288. for (HSLFTextParagraph p : lp) {
  289. for (HSLFTextRun r : p.getTextRuns()) {
  290. ret.append(r.getRawText());
  291. }
  292. if (ret.length() > 0 && ret.charAt(ret.length()-1) != '\n') {
  293. ret.append("\n");
  294. }
  295. }
  296. }
  297. }
  298. }