Object source = input.getParameter(CutProcessor.SOURCE_PARAMETER);
Integer start = input.getParameter(CutProcessor.START_PARAMETER);
Integer end = input.getParameter(CutProcessor.END_PARAMETER);
if (source == null) {
throw new DocumentException("Parameter 'source' + must be set");
}
if (start == null) {
throw new DocumentException("Parameter 'start' must be set");
}
if (end == null) {
end = Integer.valueOf(0);
}
PDFParser parser = PDFBox.read(source);
parser.parse();
PDDocument document = parser.getPDDocument();
Splitter splitter = new Splitter();
splitter.setSplitAtPage(1);
List<PDDocument> list = splitter.split(document);
int pageCount = list.size();
int startPage = start.intValue();
if (startPage < 1) {
startPage = 1;
}
startPage = startPage - 1;
int endPage = end.intValue();
if (endPage > pageCount) {
endPage = pageCount;
}
PDDocument destination = list.get(startPage);
PDFMergerUtility merger = new PDFMergerUtility();
for (int i = startPage + 1; i < endPage; i++) {
PDDocument tmpDocument = list.get(i);
merger.appendDocument(destination, tmpDocument);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
COSWriter writer = new COSWriter(bos);
writer.write(destination);
for (PDDocument doc : list) {
doc.close();
}
document.close();
destination.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
return new DocumentOutput(bis);
} catch (IOException e) {
throw new PDFException(e);
} catch (COSVisitorException e) {
throw new DocumentException(e);
}
}