-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPptxGenerator.java
More file actions
321 lines (291 loc) · 14.1 KB
/
PptxGenerator.java
File metadata and controls
321 lines (291 loc) · 14.1 KB
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
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.pptx4j.pml.Pic;
import org.docx4j.XmlUtils;
import org.pptx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.PresentationMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart;
import org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart;
import org.docx4j.openpackaging.parts.PresentationML.SlideMasterPart;
import org.docx4j.openpackaging.parts.PresentationML.SlidePart;
import org.docx4j.dml.*;
import org.pptx4j.pml.Shape;
import org.pptx4j.pml.SldLayout;
import javax.xml.bind.JAXBException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.pptx4j.model.SlideSizesWellKnown;
import org.pptx4j.pml.STPlaceholderType;
import org.pptx4j.pml.CTPlaceholder;
import org.pptx4j.pml.NvPr;
import org.pptx4j.pml.Shape.NvSpPr;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.MappedByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jason moofang
*
*/
public class PptxGenerator {
SlideMasterPart slideMaster;
PresentationMLPackage presentationMLPackage;
MainPresentationPart pp;
int slideNum = 0;
public PptxGenerator() throws Exception {
presentationMLPackage = PresentationMLPackage.createPackage(SlideSizesWellKnown.SCREEN16x10, true);
pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
new PartName("/ppt/presentation.xml"));
slideMaster = (SlideMasterPart)presentationMLPackage.getParts().getParts().get(
new PartName("/ppt/slideMasters/slideMaster1.xml"));
}
public SlidePart createSlide(SlideLayoutPart layoutPart) throws Exception {
slideNum ++;
return presentationMLPackage.createSlidePart(pp, layoutPart,
new PartName("/ppt/slides/slide" + slideNum + ".xml"));
}
public SlidePart createTwoContentSlide() throws Exception {
SlideLayoutPart layout = createLayout(4, "twocontent");
return createSlide(layout);
}
public SlidePart createStandardSlide() throws Exception {
SlideLayoutPart layout = createLayout(2, "standard");
return createSlide(layout);
}
public void addTitle(SlidePart slide, String content){
// create a new shape and set its id and name
Shape titleShape = createShape(2, "Title 1", STPlaceholderType.CTR_TITLE, 0);
// create SpPr
titleShape.setSpPr(new CTShapeProperties());
ArrayList<String> s = new ArrayList<String>();
s.add(content);
CTTextBody ctTB = createTextBody(s);
ctTB.getP().get(0).getPPr().setAlgn(STTextAlignType.CTR);
//ctTP.getPPr().setAlgn(STTextAlignType.CTR);
// add textbody
titleShape.setTxBody(ctTB);
// add shape
slide.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(titleShape);
}
public void addContent1(SlidePart slide, ArrayList<String> content){
// create a new shape and set its id and name
Shape standardShape = createShape(3, "Content Placeholder 2", STPlaceholderType.BODY, 1);
// create SpPr
standardShape.setSpPr(new CTShapeProperties());
CTTextBody ctTB = createTextBody(content);
//ctTB.getP().get(0).getPPr().setAlgn(STTextAlignType.CTR);
ctTB.getP().get(0).getPPr().setLvl(0);
// add textbody
standardShape.setTxBody(ctTB);
// add shape
slide.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(standardShape);
}
public void addImage(SlidePart slidePart, String filepath, long offx, long offy, long extcx, long extcy) throws Exception {
// Add image part
File file = new File(filepath);
BinaryPartAbstractImage imagePart
= BinaryPartAbstractImage.createImagePart(presentationMLPackage, slidePart, file);
// Create p:pic
java.util.HashMap<String, String>mappings = new java.util.HashMap<String, String>();
mappings.put("id1", "4");
mappings.put("name", "Picture");
mappings.put("descr", "nothing much");
mappings.put("rEmbedId", imagePart.getSourceRelationship().getId() );
mappings.put("offx", Long.toString(offx));
mappings.put("offy", Long.toString(offy));
mappings.put("extcx", Long.toString(extcx));
mappings.put("extcy", Long.toString(extcy));
Object o = org.docx4j.XmlUtils.unmarshallFromTemplate(PICTURE_XML,
mappings, Context.jcPML, Pic.class ) ;
// Add p:pic to slide
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(o);
}
public void addTextShape(SlidePart slide, String text, long offx, long offy, long extcx, long extcy) throws Exception {
java.util.HashMap<String, String>mappings = new java.util.HashMap<String, String>();
mappings.put("content_text", text );
mappings.put("offx", Long.toString(offx));
mappings.put("offy", Long.toString(offy));
mappings.put("extcx", Long.toString(extcx));
mappings.put("extcy", Long.toString(extcy));
Shape sample = ((Shape)XmlUtils.unmarshallFromTemplate(SHAPE_XML, mappings, Context.jcPML));
slide.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
}
public void save(String outputfilepath) throws Exception {
presentationMLPackage.save(new java.io.File(outputfilepath));
}
SlideLayoutPart createLayout(int layoutNumber, String slideName){
SlideLayoutPart layoutPart = null;
try {
// create new slide layout
layoutPart = new SlideLayoutPart(new PartName("/ppt/slideLayouts/slideLayout"+layoutNumber+".xml"));
// read in slide layout, unmarshal, and set
layoutPart.setJaxbElement((SldLayout) XmlUtils.unmarshalString(getLayoutXML(slideName), Context.jcPML));
// add reference in slideMaster to layout and vice versa
slideMaster.addSlideLayoutIdListEntry(layoutPart);
layoutPart.addTargetPart(slideMaster);
} catch (JAXBException e) {
System.err.println("Error: PC> JAXB slide layout write failed");
} catch (InvalidFormatException e) {
System.err.println("Error: PC> SlideLayoutPart creation failed");
} catch (Exception e){
System.err.println("Error: PC> slideLayout did not pass validation");
}
return layoutPart;
}
String getLayoutXML(String layout){
try {
return readFile(System.getProperty("user.dir") + "/" + layout + ".xml");
} catch (Exception e) {
return "";
}
}
Shape createShape(int id, String name, STPlaceholderType type, int idx){
Shape scratch = new Shape();
// create NvSpPr, set attribs
scratch.setNvSpPr(new NvSpPr());{
scratch.getNvSpPr().setCNvPr(new CTNonVisualDrawingProps());
scratch.getNvSpPr().getCNvPr().setId(id);
scratch.getNvSpPr().getCNvPr().setName(name);
scratch.getNvSpPr().setCNvSpPr(new CTNonVisualDrawingShapeProps());
scratch.getNvSpPr().getCNvSpPr().setSpLocks(new CTShapeLocking());
scratch.getNvSpPr().getCNvSpPr().getSpLocks().setNoGrp(Boolean.TRUE);}
// create NvPr, set attribs
scratch.getNvSpPr().setNvPr(new NvPr());{
scratch.getNvSpPr().getNvPr().setPh(new CTPlaceholder());
if(type == STPlaceholderType.TITLE || type == STPlaceholderType.CTR_TITLE)
scratch.getNvSpPr().getNvPr().getPh().setType(type);
if(idx>0)
scratch.getNvSpPr().getNvPr().getPh().setIdx(Long.valueOf(idx));}
return scratch;
}
CTTextBody createTextBody(ArrayList<String> paras) {
CTTextBody ctTB = new CTTextBody();
ctTB.setBodyPr(new CTTextBodyProperties());
//ctTB.getBodyPr().setRtlCol(Boolean.TRUE);
//ctTB.getBodyPr().setAnchor(STTextAnchoringType.CTR);
ctTB.setLstStyle(new CTTextListStyle());
// create P
List<CTTextParagraph> p = ctTB.getP();
if(paras.size()==0){
paras = new ArrayList<String>();
paras.add("(Add Content)");
}
for(String str : paras){
p.add(createParagraph(str));
}
return ctTB;
}
CTTextParagraph createParagraph(String para) {
CTTextParagraph ctTP = new CTTextParagraph();
ctTP.setPPr(new CTTextParagraphProperties());
ctTP.getEGTextRun().add(createRun(para));
ctTP.setEndParaRPr(new CTTextCharacterProperties());
ctTP.getEndParaRPr().setLang("en-US");
//ctTP.getEndParaRPr().setDirty(Boolean.FALSE);
return ctTP;
}
CTRegularTextRun createRun(String str) {
CTRegularTextRun ctRTR = new CTRegularTextRun();
ctRTR.setRPr(new CTTextCharacterProperties());
ctRTR.getRPr().setLang("en-US");
ctRTR.getRPr().setDirty(Boolean.FALSE);
ctRTR.getRPr().setSmtClean(Boolean.FALSE);
ctRTR.setT(str);
return ctRTR;
}
String readFile(String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return Charset.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
}
private static String SHAPE_XML =
"<p:sp xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\">"
+ "<p:nvSpPr>"
+ "<p:cNvPr id=\"4\" name=\"Title 3\" />"
+ "<p:cNvSpPr>"
+ "<a:spLocks noGrp=\"1\" />"
+ "</p:cNvSpPr>"
+ "<p:nvPr />"
+ "</p:nvSpPr>"
+ " <p:spPr>"
+ " <a:xfrm>"
+ " <a:off x=\"${offx}\" y=\"${offy}\" />"
+ " <a:ext cx=\"${extcx}\" cy=\"${extcy}\" />"
+ " </a:xfrm>"
+ " </p:spPr>"
+ "<p:txBody>"
+ "<a:bodyPr />"
+ "<a:lstStyle />"
+ "<a:p>"
+ "<a:r>"
+ "<a:rPr lang=\"en-US\" smtClean=\"0\" />"
+ "<a:t>${content_text}</a:t>"
+ "</a:r>"
+ "<a:endParaRPr lang=\"en-US\" />"
+ "</a:p>"
+ "</p:txBody>"
+ "</p:sp>";
private static String PICTURE_XML =
"<p:pic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\"> "
+ "<p:nvPicPr>"
+ "<p:cNvPr id=\"${id1}\" name=\"${name}\" descr=\"${descr}\"/>"
+ "<p:cNvPicPr>"
+ "<a:picLocks noChangeAspect=\"1\"/>"
+ "</p:cNvPicPr>"
+ "<p:nvPr/>"
+ "</p:nvPicPr>"
+ "<p:blipFill>"
+ "<a:blip r:embed=\"${rEmbedId}\" cstate=\"print\"/>"
+ "<a:stretch>"
+ "<a:fillRect/>"
+ "</a:stretch>"
+ "</p:blipFill>"
+ "<p:spPr>"
+ "<a:xfrm>"
+ "<a:off x=\"${offx}\" y=\"${offy}\"/>"
+ "<a:ext cx=\"${extcx}\" cy=\"${extcy}\"/>"
+ "</a:xfrm>"
+ "<a:prstGeom prst=\"rect\">"
+ "<a:avLst/>"
+ "</a:prstGeom>"
+ "</p:spPr>"
+ "</p:pic>";
public static void main(String[] args) throws Exception {
String outputfilepath = System.getProperty("user.dir") + "/pptx-test.pptx";
PptxGenerator g = new PptxGenerator();
SlidePart slide1 = g.createTwoContentSlide();
g.addTitle(slide1, "Das Title");
g.addImage(slide1, System.getProperty("user.dir") + "/ffffuuuuuuu.jpg", 4514812, 1071812, 4014375, 4014375);
g.addImage(slide1, System.getProperty("user.dir") + "/haha_logo.png", 8214812, 107181, 704375, 1004375);
g.addImage(slide1, System.getProperty("user.dir") + "/pagination_bg.png", 8414812, 5307181, 754375, 404375);
ArrayList<String> contents = new ArrayList<String>();
contents.add("Your");
contents.add("stuff");
contents.add("here");
g.addContent1(slide1, contents);
g.addTextShape(slide1, "Company in Confidence", 114812, 5071812, 4014375, 514375);
SlidePart slide2 = g.createStandardSlide();
g.addTitle(slide2, "Check it out, a graph!");
g.addImage(slide2, System.getProperty("user.dir") + "/graph.png", 514812, 1071812, 8514375, 4014375);
g.addImage(slide2, System.getProperty("user.dir") + "/haha_logo.png", 8214812, 107181, 704375, 1004375);
g.addImage(slide2, System.getProperty("user.dir") + "/pagination_bg.png", 8414812, 5307181, 754375, 404375);
g.addTextShape(slide2, "Company in Confidence", 114812, 5071812, 4014375, 514375);
g.save(outputfilepath);
SlidePart slide3 = g.createStandardSlide();
g.addTitle(slide3, "I can haz text under mai graph!?");
g.addImage(slide3, System.getProperty("user.dir") + "/graph2.png", 514812, 1071812, 8514375, 2014375);
g.addImage(slide3, System.getProperty("user.dir") + "/haha_logo.png", 8214812, 107181, 704375, 1004375);
g.addImage(slide3, System.getProperty("user.dir") + "/pagination_bg.png", 8414812, 5307181, 754375, 404375);
g.addTextShape(slide3, "Company in Confidence", 114812, 5071812, 4014375, 514375);
g.addTextShape(slide3, "I can write maaaaaaaaaaaaaaannnnnnnnnyyyyyyyyyy thingssss\n In moar lines", 514812, 3271812, 8514375, 1814375);
g.save(outputfilepath);
}
}