-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPresentationSplit.java
More file actions
140 lines (125 loc) · 6.31 KB
/
Copy pathPresentationSplit.java
File metadata and controls
140 lines (125 loc) · 6.31 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
package com.devexpress.demos;
import com.devexpress.jpeg.JpegBackend;
import com.devexpress.jpeg.JpegCodecSettings;
import com.devexpress.docs.presentation.DocumentFormat;
import com.devexpress.docs.presentation.Presentation;
import com.devexpress.docs.presentation.Slide;
import java.io.*;
import java.util.*;
public class PresentationSplit {
static final String INPUT_FILE_PATH = "SharedFiles/Sample.pptx";
static final String OUTPUT_DIRECTORY_NAME = "Split_Presentation";
static final String SLIDE_RANGE = "1-2";
static final boolean IS_SINGLE_FILE_OUTPUT = false;
static final boolean IS_PDF_OUTPUT = false;
static final DocumentFormat OUTPUT_FORMAT = DocumentFormat.PPTX;
static {
JpegCodecSettings.setBackend(JpegBackend.MANAGED);
}
public static void main(String[] args) {
try {
InputStream inputResource = PresentationSplit.class.getResourceAsStream("/" + INPUT_FILE_PATH);
if (inputResource == null)
throw new FileNotFoundException("Resource not found: " + INPUT_FILE_PATH);
File outputDirectory = new File(OUTPUT_DIRECTORY_NAME);
if (outputDirectory.exists())
deleteDirectory(outputDirectory);
outputDirectory.mkdirs();
int[] slideIndexes = parseSlideRange(SLIDE_RANGE);
List<ByteArrayOutputStream> slideStreams = extractSlides(inputResource, slideIndexes, IS_SINGLE_FILE_OUTPUT, IS_PDF_OUTPUT, OUTPUT_FORMAT);
inputResource.close();
if (slideStreams.isEmpty())
throw new IllegalStateException("Failed to split presentation.");
for (int index = 0; index < slideStreams.size(); index++) {
String extension = IS_PDF_OUTPUT ? "pdf" : "pptx";
File outputFile = new File(outputDirectory, "slide_" + (index + 1) + "." + extension);
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
slideStreams.get(index).writeTo(fos);
}
}
System.out.println("Created " + outputDirectory.getAbsolutePath());
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
static int[] parseSlideRange(String slideRange) {
if (slideRange == null || slideRange.isBlank()) return new int[0];
SortedSet<Integer> result = new TreeSet<>();
String[] tokens = slideRange.split("[,;\\s]+");
for (String raw : tokens) {
String part = raw.trim();
if (part.isEmpty()) continue;
int dashIndex = part.indexOf('-');
if (dashIndex > 0) {
try {
int start = Integer.parseInt(part.substring(0, dashIndex).trim());
int end = Integer.parseInt(part.substring(dashIndex + 1).trim());
if (start <= 0 || end <= 0) continue;
if (end < start) { int tmp = start; start = end; end = tmp; }
for (int v = start; v <= end; v++) result.add(v - 1);
} catch (NumberFormatException ignored) {}
continue;
}
try {
int single = Integer.parseInt(part);
if (single > 0) result.add(single - 1);
} catch (NumberFormatException ignored) {}
}
return result.stream().mapToInt(Integer::intValue).toArray();
}
static List<ByteArrayOutputStream> extractSlides(InputStream inputStream, int[] slideIndexes,
boolean isSingleFileOutput, boolean isPdfOutput, DocumentFormat documentFormat) throws IOException {
try (Presentation presentation = new Presentation(inputStream)) {
return isSingleFileOutput
? extractSlidesSingleFile(slideIndexes, isPdfOutput, documentFormat, presentation)
: extractSlidesDifferentFiles(slideIndexes, isPdfOutput, documentFormat, presentation);
}
}
static List<ByteArrayOutputStream> extractSlidesDifferentFiles(int[] slideIndexes, boolean isPdfOutput,
DocumentFormat documentFormat, Presentation presentation) throws IOException {
List<ByteArrayOutputStream> outputStreams = new ArrayList<>();
List<Slide> slides = presentation.getSlides();
int[] effectiveIndexes = slideIndexes.length == 0
? java.util.stream.IntStream.range(0, slides.size()).toArray()
: slideIndexes;
for (int slideIndex : effectiveIndexes) {
if (slideIndex < 0 || slideIndex >= slides.size()) continue;
try (Presentation single = new Presentation()) {
single.getSlides().clear();
single.setSlideSize(presentation.getSlideSize());
single.getSlides().add(slides.get(slideIndex));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (isPdfOutput) single.exportToPdf(baos);
else single.saveDocument(baos, documentFormat);
outputStreams.add(baos);
}
}
return outputStreams;
}
static List<ByteArrayOutputStream> extractSlidesSingleFile(int[] slideIndexes, boolean isPdfOutput,
DocumentFormat documentFormat, Presentation presentation) throws IOException {
List<Slide> slides = presentation.getSlides();
Set<Integer> keepSet = new HashSet<>();
if (slideIndexes.length == 0) {
for (int i = 0; i < slides.size(); i++) keepSet.add(i);
} else {
for (int idx : slideIndexes) keepSet.add(idx);
}
for (int index = slides.size() - 1; index >= 0; index--) {
if (!keepSet.contains(index)) presentation.getSlides().remove(index);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (isPdfOutput) presentation.exportToPdf(baos);
else presentation.saveDocument(baos, documentFormat);
return Collections.singletonList(baos);
}
static void deleteDirectory(File dir) {
File[] files = dir.listFiles();
if (files != null)
for (File file : files)
if (file.isDirectory()) deleteDirectory(file);
else file.delete();
dir.delete();
}
}