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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================== */
21 package org
.apache
.poi
.hslf
.extractor
;
24 import java
.util
.HashSet
;
26 import org
.apache
.poi
.POIOLE2TextExtractor
;
27 import org
.apache
.poi
.poifs
.filesystem
.POIFSFileSystem
;
28 import org
.apache
.poi
.hslf
.*;
29 import org
.apache
.poi
.hslf
.model
.*;
30 import org
.apache
.poi
.hslf
.usermodel
.*;
33 * This class can be used to extract text from a PowerPoint file.
34 * Can optionally also get the notes from one.
39 public class PowerPointExtractor
extends POIOLE2TextExtractor
41 private HSLFSlideShow _hslfshow
;
42 private SlideShow _show
;
43 private Slide
[] _slides
;
45 private boolean slidesByDefault
= true;
46 private boolean notesByDefault
= false;
49 * Basic extractor. Returns all the text, and optionally all the notes
51 public static void main(String args
[]) throws IOException
54 System
.err
.println("Useage:");
55 System
.err
.println("\tPowerPointExtractor [-notes] <file>");
59 boolean notes
= false;
68 PowerPointExtractor ppe
= new PowerPointExtractor(file
);
69 System
.out
.println(ppe
.getText(true,notes
));
74 * Creates a PowerPointExtractor, from a file
75 * @param fileName The name of the file to extract from
77 public PowerPointExtractor(String fileName
) throws IOException
{
78 this(new FileInputStream(fileName
));
81 * Creates a PowerPointExtractor, from an Input Stream
82 * @param iStream The input stream containing the PowerPoint document
84 public PowerPointExtractor(InputStream iStream
) throws IOException
{
85 this(new POIFSFileSystem(iStream
));
88 * Creates a PowerPointExtractor, from an open POIFSFileSystem
89 * @param fs the POIFSFileSystem containing the PowerPoint document
91 public PowerPointExtractor(POIFSFileSystem fs
) throws IOException
{
92 this(new HSLFSlideShow(fs
));
96 * Creates a PowerPointExtractor, from a HSLFSlideShow
97 * @param ss the HSLFSlideShow to extract text from
99 public PowerPointExtractor(HSLFSlideShow ss
) throws IOException
{
102 _show
= new SlideShow(_hslfshow
);
103 _slides
= _show
.getSlides();
107 * Shuts down the underlying streams
109 public void close() throws IOException
{
117 * Should a call to getText() return slide text?
120 public void setSlidesByDefault(boolean slidesByDefault
) {
121 this.slidesByDefault
= slidesByDefault
;
124 * Should a call to getText() return notes text?
127 public void setNotesByDefault(boolean notesByDefault
) {
128 this.notesByDefault
= notesByDefault
;
132 * Fetches all the slide text from the slideshow,
133 * but not the notes, unless you've called
134 * setSlidesByDefault() and setNotesByDefault()
137 public String
getText() {
138 return getText(slidesByDefault
,notesByDefault
);
142 * Fetches all the notes text from the slideshow, but not the slide text
144 public String
getNotes() {
145 return getText(false,true);
149 * Fetches text from the slideshow, be it slide text or note text.
150 * Because the final block of text in a TextRun normally have their
151 * last \n stripped, we add it back
152 * @param getSlideText fetch slide text
153 * @param getNoteText fetch note text
155 public String
getText(boolean getSlideText
, boolean getNoteText
) {
156 StringBuffer ret
= new StringBuffer();
159 for(int i
=0; i
<_slides
.length
; i
++) {
160 Slide slide
= _slides
[i
];
161 TextRun
[] runs
= slide
.getTextRuns();
162 for(int j
=0; j
<runs
.length
; j
++) {
163 TextRun run
= runs
[j
];
165 String text
= run
.getText();
167 if(! text
.endsWith("\n")) {
179 // Not currently using _notes, as that can have the notes of
180 // master sheets in. Grab Slide list, then work from there,
181 // but ensure no duplicates
182 HashSet seenNotes
= new HashSet();
183 for(int i
=0; i
<_slides
.length
; i
++) {
184 Notes notes
= _slides
[i
].getNotesSheet();
185 if(notes
== null) { continue; }
186 Integer id
= new Integer(notes
._getSheetNumber());
187 if(seenNotes
.contains(id
)) { continue; }
190 TextRun
[] runs
= notes
.getTextRuns();
191 if(runs
!= null && runs
.length
> 0) {
192 for(int j
=0; j
<runs
.length
; j
++) {
193 TextRun run
= runs
[j
];
194 String text
= run
.getText();
196 if(! text
.endsWith("\n")) {
204 return ret
.toString();