1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xslf.usermodel;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.PackageHelper;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPresentation;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdList;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideSize;
import org.openxmlformats.schemas.presentationml.x2006.main.PresentationDocument;
import java.awt.Dimension;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* High level representation of a ooxml slideshow.
* This is the first object most users will construct whether
* they are reading or writing a slideshow. It is also the
* top level object for creating new slides/etc.
*/
@Beta
public class XMLSlideShow extends POIXMLDocument {
private static POILogger _logger = POILogFactory.getLogger(XMLSlideShow.class);
private CTPresentation _presentation;
private List<XSLFSlide> _slides;
private Map<String, XSLFSlideMaster> _masters;
private List<XSLFPictureData> _pictures;
private XSLFTableStyles _tableStyles;
private XSLFNotesMaster _notesMaster;
private XSLFCommentAuthors _commentAuthors;
public XMLSlideShow() {
this(empty());
}
public XMLSlideShow(OPCPackage pkg) {
super(pkg);
try {
if(getCorePart().getContentType().equals(XSLFRelation.THEME_MANAGER.getContentType())) {
rebase(getPackage());
}
//build a tree of POIXMLDocumentParts, this presentation being the root
load(XSLFFactory.getInstance());
} catch (Exception e){
throw new POIXMLException(e);
}
}
public XMLSlideShow(InputStream is) throws IOException {
this(PackageHelper.open(is));
}
static final OPCPackage empty() {
InputStream is = XMLSlideShow.class.getResourceAsStream("empty.pptx");
if (is == null) {
throw new RuntimeException("Missing resource 'empty.pptx'");
}
try {
return OPCPackage.open(is);
} catch (Exception e){
throw new POIXMLException(e);
}
}
// TODO get rid of this method
@Deprecated
public XSLFSlideShow _getXSLFSlideShow() throws OpenXML4JException, IOException, XmlException{
return new XSLFSlideShow(getPackage());
}
@Override
protected void onDocumentRead() throws IOException {
try {
PresentationDocument doc =
PresentationDocument.Factory.parse(getCorePart().getInputStream());
_presentation = doc.getPresentation();
Map<String, XSLFSlide> shIdMap = new HashMap<String, XSLFSlide>();
_masters = new HashMap<String, XSLFSlideMaster>();
for (POIXMLDocumentPart p : getRelations()) {
if (p instanceof XSLFSlide) {
shIdMap.put(p.getPackageRelationship().getId(), (XSLFSlide) p);
} else if (p instanceof XSLFSlideMaster) {
XSLFSlideMaster master = (XSLFSlideMaster)p;
_masters.put(p.getPackageRelationship().getId(), master);
}else if (p instanceof XSLFTableStyles){
_tableStyles = (XSLFTableStyles)p;
} else if (p instanceof XSLFNotesMaster) {
_notesMaster = (XSLFNotesMaster)p;
} else if (p instanceof XSLFCommentAuthors) {
_commentAuthors = (XSLFCommentAuthors)p;
}
}
_slides = new ArrayList<XSLFSlide>();
if (_presentation.isSetSldIdLst()) {
List<CTSlideIdListEntry> slideIds = _presentation.getSldIdLst().getSldIdList();
for (CTSlideIdListEntry slId : slideIds) {
XSLFSlide sh = shIdMap.get(slId.getId2());
if (sh == null) {
_logger.log(POILogger.WARN, "Slide with r:id " + slId.getId() + " was defined, but didn't exist in package, skipping");
continue;
}
_slides.add(sh);
}
}
} catch (XmlException e) {
throw new POIXMLException(e);
}
}
@Override
protected void commit() throws IOException {
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
Map<String, String> map = new HashMap<String, String>();
map.put(STRelationshipId.type.getName().getNamespaceURI(), "r");
xmlOptions.setSaveSuggestedPrefixes(map);
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
_presentation.save(out, xmlOptions);
out.close();
}
/**
* Get the document's embedded files.
*/
public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
return Collections.unmodifiableList(
getPackage().getPartsByName(Pattern.compile("/ppt/embeddings/.*?"))
);
}
/**
* Returns all Pictures, which are referenced from the document itself.
* @return a {@link List} of {@link PackagePart}.
* The returned {@link List} is unmodifiable.
*/
public List<XSLFPictureData> getAllPictures() {
if(_pictures == null){
List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?"));
_pictures = new ArrayList<XSLFPictureData>(mediaParts.size());
for(PackagePart part : mediaParts){
_pictures.add(new XSLFPictureData(part, null));
}
}
return Collections.unmodifiableList(_pictures);
}
/**
* Create a slide and initialize it from the specified layout.
*
* @param layout
* @return created slide
*/
public XSLFSlide createSlide(XSLFSlideLayout layout) {
int slideNumber = 256, cnt = 1;
CTSlideIdList slideList;
if (!_presentation.isSetSldIdLst()) slideList = _presentation.addNewSldIdLst();
else {
slideList = _presentation.getSldIdLst();
for(CTSlideIdListEntry slideId : slideList.getSldIdList()){
slideNumber = (int)Math.max(slideId.getId() + 1, slideNumber);
cnt++;
}
}
XSLFSlide slide = (XSLFSlide)createRelationship(
XSLFRelation.SLIDE, XSLFFactory.getInstance(), cnt);
CTSlideIdListEntry slideId = slideList.addNewSldId();
slideId.setId(slideNumber);
slideId.setId2(slide.getPackageRelationship().getId());
layout.copyLayout(slide);
slide.addRelation(layout.getPackageRelationship().getId(), layout);
PackagePartName ppName = layout.getPackagePart().getPartName();
slide.getPackagePart().addRelationship(ppName, TargetMode.INTERNAL,
layout.getPackageRelationship().getRelationshipType());
_slides.add(slide);
return slide;
}
/**
* Create a blank slide.
*/
public XSLFSlide createSlide() {
String masterId = _presentation.getSldMasterIdLst().getSldMasterIdArray(0).getId2();
XSLFSlideMaster master = _masters.get(masterId);
XSLFSlideLayout layout = master.getLayout(SlideLayout.BLANK);
if(layout == null) throw new IllegalArgumentException("Blank layout was not found");
return createSlide(layout);
}
/**
* Return the Notes Master, if there is one.
* (May not be present if no notes exist)
*/
public XSLFNotesMaster getNotesMaster() {
return _notesMaster;
}
public XSLFSlideMaster[] getSlideMasters() {
return _masters.values().toArray(new XSLFSlideMaster[_masters.size()]);
}
/**
* Return all the slides in the slideshow
*/
public XSLFSlide[] getSlides() {
return _slides.toArray(new XSLFSlide[_slides.size()]);
}
/**
* Returns the list of comment authors, if there is one.
* Will only be present if at least one slide has comments on it.
*/
public XSLFCommentAuthors getCommentAuthors() {
return _commentAuthors;
}
/**
*
* @param newIndex 0-based index of the slide
*/
public void setSlideOrder(XSLFSlide slide, int newIndex){
int oldIndex = _slides.indexOf(slide);
if(oldIndex == -1) throw new IllegalArgumentException("Slide not found");
// fix the usermodel container
_slides.add(newIndex, _slides.remove(oldIndex));
// fix ordering in the low-level xml
List<CTSlideIdListEntry> slideIds = _presentation.getSldIdLst().getSldIdList();
CTSlideIdListEntry oldEntry = slideIds.get(oldIndex);
slideIds.add(newIndex, oldEntry);
slideIds.remove(oldEntry);
}
public XSLFSlide removeSlide(int index){
XSLFSlide slide = _slides.remove(index);
removeRelation(slide);
_presentation.getSldIdLst().getSldIdList().remove(index);
return slide;
}
/**
* Returns the current page size
*
* @return the page size
*/
public Dimension getPageSize(){
CTSlideSize sz = _presentation.getSldSz();
int cx = sz.getCx();
int cy = sz.getCy();
return new Dimension((int)Units.toPoints(cx), (int)Units.toPoints(cy));
}
/**
* Sets the page size to the given <code>Dimension</code> object.
*
* @param pgSize page size
*/
public void setPageSize(Dimension pgSize){
CTSlideSize sz = CTSlideSize.Factory.newInstance();
sz.setCx(Units.toEMU(pgSize.getWidth()));
sz.setCy(Units.toEMU(pgSize.getHeight()));
_presentation.setSldSz(sz);
}
@Internal
public CTPresentation getCTPresentation(){
return _presentation;
}
/**
* Adds a picture to the workbook.
*
* @param pictureData The bytes of the picture
* @param format The format of the picture.
*
* @return the index to this picture (1 based).
* @see XSLFPictureData#PICTURE_TYPE_EMF
* @see XSLFPictureData#PICTURE_TYPE_WMF
* @see XSLFPictureData#PICTURE_TYPE_PICT
* @see XSLFPictureData#PICTURE_TYPE_JPEG
* @see XSLFPictureData#PICTURE_TYPE_PNG
* @see XSLFPictureData#PICTURE_TYPE_DIB
*/
public int addPicture(byte[] pictureData, int format) {
getAllPictures();
int imageNumber = getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?")).size() + 1;
XSLFPictureData img = (XSLFPictureData) createRelationship(
XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber, true);
_pictures.add(img);
try {
OutputStream out = img.getPackagePart().getOutputStream();
out.write(pictureData);
out.close();
} catch (IOException e) {
throw new POIXMLException(e);
}
return imageNumber - 1;
}
public XSLFTableStyles getTableStyles(){
return _tableStyles;
}
}
|