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
|
/* ====================================================================
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;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.POIDataSamples;
import org.apache.poi.ooxml.POIXMLProperties.CoreProperties;
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.sl.usermodel.AutoNumberingScheme;
import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.xslf.usermodel.*;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry;
import static org.junit.jupiter.api.Assertions.*;
class TestXSLFSlideShow {
private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
private OPCPackage pack;
@BeforeEach
void setUp() throws Exception {
pack = OPCPackage.open(slTests.openResourceAsStream("sample.pptx"));
}
@AfterEach
void tearDown() throws IOException {
pack.close();
}
@Test
void testContainsMainContentType() throws Exception {
boolean found = false;
for(PackagePart part : pack.getParts()) {
if(part.getContentType().equals(XSLFRelation.MAIN.getContentType())) {
found = true;
}
}
assertTrue(found);
}
@Test
void testOpen() throws IOException, OpenXML4JException, XmlException {
// With the finalized uri, should be fine
XSLFSlideShow xml = new XSLFSlideShow(pack);
// Check the core
assertNotNull(xml.getPresentation());
// Check it has some slides
assertNotEquals(0, xml.getSlideReferences().sizeOfSldIdArray());
assertNotEquals(0, xml.getSlideMasterReferences().sizeOfSldMasterIdArray());
xml.close();
}
@Test
void testSlideBasics() throws IOException, OpenXML4JException, XmlException {
XSLFSlideShow xml = new XSLFSlideShow(pack);
// Should have 1 master
assertEquals(1, xml.getSlideMasterReferences().sizeOfSldMasterIdArray());
// Should have three sheets
assertEquals(2, xml.getSlideReferences().sizeOfSldIdArray());
// Check they're as expected
CTSlideIdListEntry[] slides = xml.getSlideReferences().getSldIdArray();
assertEquals(256, slides[0].getId());
assertEquals(257, slides[1].getId());
assertEquals("rId2", slides[0].getId2());
assertEquals("rId3", slides[1].getId2());
// Now get those objects
assertNotNull(xml.getSlide(slides[0]));
assertNotNull(xml.getSlide(slides[1]));
// And check they have notes as expected
assertNotNull(xml.getNotes(slides[0]));
assertNotNull(xml.getNotes(slides[1]));
// And again for the master
CTSlideMasterIdListEntry[] masters = xml.getSlideMasterReferences().getSldMasterIdArray();
// see SlideAtom.USES_MASTER_SLIDE_ID
assertEquals(0x80000000L, masters[0].getId());
assertEquals("rId1", masters[0].getId2());
assertNotNull(xml.getSlideMaster(masters[0]));
xml.close();
}
@Test
void testMetadataBasics() throws IOException, OpenXML4JException, XmlException {
XSLFSlideShow xml = new XSLFSlideShow(pack);
assertNotNull(xml.getProperties().getCoreProperties());
assertNotNull(xml.getProperties().getExtendedProperties());
CTProperties props = xml.getProperties().getExtendedProperties().getUnderlyingProperties();
assertEquals("Microsoft Office PowerPoint", props.getApplication());
assertEquals(0, props.getCharacters());
assertEquals(0, props.getLines());
CoreProperties cprops = xml.getProperties().getCoreProperties();
assertNull(cprops.getTitle());
assertFalse(cprops.getUnderlyingProperties().getSubjectProperty().isPresent());
xml.close();
}
@Test
void testMasterBackground() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFBackground b = ppt.getSlideMasters().get(0).getBackground();
b.setFillColor(Color.RED);
XSLFSlide sl = ppt.createSlide();
XSLFAutoShape as = sl.createAutoShape();
as.setAnchor(new Rectangle2D.Double(100,100,100,100));
as.setShapeType(ShapeType.CLOUD);
XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt);
ppt.close();
XSLFBackground b2 = ppt2.getSlideMasters().get(0).getBackground();
assertEquals(Color.RED, b2.getFillColor());
ppt2.close();
}
@Test
void testSlideImportContent() throws IOException{
try (XMLSlideShow ppt = new XMLSlideShow(slTests.openResourceAsStream("templatePPTWithOnlyOneText.pptx"))) {
XSLFSlide templateSlide = ppt.getSlides().get(0);
XSLFTextShape templateTextShape = (XSLFTextShape) templateSlide.getShapes().get(0);
XmlObject templateTextRunXmlObject = templateTextShape.getTextParagraphs().get(0).getTextRuns().get(0).getXmlObject();
XSLFSlide copySlide = ppt.createSlide();
copySlide.importContent(templateSlide);
XSLFTextShape copyTextShape = (XSLFTextShape) copySlide.getShapes().get(0);
XmlObject copyTextRunXmlObject = copyTextShape.getTextParagraphs().get(0).getTextRuns().get(0).getXmlObject();
assertNotEquals(templateTextRunXmlObject, copyTextRunXmlObject);
}
}
@Test
void testBulletStyle_DoesNotThrowException() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFAutoShape shape = slide.createAutoShape();
shape.setAnchor(new Rectangle2D.Double(50., 50., 300., 50.));
XSLFTextParagraph p = shape.addNewTextParagraph();
p.addNewTextRun().setText("Text bullet style bug");
assertDoesNotThrow(() -> p.setBulletStyle(20., Color.RED, "Calibri", AutoNumberingScheme.arabicParenRight));
ppt.close();
}
@Test
void testBulletStyle_setsCorrectValue() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFAutoShape shape = slide.createAutoShape();
shape.setAnchor(new Rectangle2D.Double(50., 50., 300., 50.));
XSLFTextParagraph p = shape.addNewTextParagraph();
p.addNewTextRun().setText("Text bullet style bug");
double fontSize = 20.;
AutoNumberingScheme scheme = AutoNumberingScheme.arabicParenRight;
p.setBulletStyle(fontSize, scheme);
assertEquals(fontSize, p.getBulletFontSize());
assertEquals(scheme, p.getAutoNumberingScheme());
ppt.close();
}
}
|