inputCommand.getInputFile().getPasswordBytes());
pdfReader.removeUnusedObjects();
pdfReader.consolidateNamedDestinations();
int n = pdfReader.getNumberOfPages();
BookmarksProcessor bookmarkProcessor = new BookmarksProcessor(SimpleBookmark.getBookmark(pdfReader), n);
int fileNum = 0;
LOG.info("Found " + n + " pages in input pdf document.");
Integer[] limits = inputCommand.getSplitPageNumbers();
// limits list validation end clean
TreeSet limitsList = validateSplitLimits(limits, n);
if (limitsList.isEmpty()) {
throw new SplitException(SplitException.ERR_NO_PAGE_LIMITS);
}
// HERE I'M SURE I'VE A LIMIT LIST WITH VALUES, I CAN START BOOKMARKS
int currentPage;
Document currentDocument = new Document(pdfReader.getPageSizeWithRotation(1));
int relativeCurrentPage = 0;
int endPage = n;
int startPage = 1;
PdfImportedPage importedPage;
File tmpFile = null;
File outFile = null;
Iterator itr = limitsList.iterator();
if (itr.hasNext()) {
endPage = ((Integer) itr.next()).intValue();
}
for (currentPage = 1; currentPage <= n; currentPage++) {
relativeCurrentPage++;
// check if i've to read one more page or to open a new doc
if (relativeCurrentPage == 1) {
LOG.debug("Creating a new document.");
fileNum++;
tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile());
String bookmark = null;
if (bookmarksTable != null && bookmarksTable.size() > 0) {
bookmark = (String) bookmarksTable.get(new Integer(currentPage));
}
FileNameRequest request = new FileNameRequest(currentPage, fileNum, bookmark);
outFile = new File(inputCommand.getOutputFile(), prefixParser.generateFileName(request));
startPage = currentPage;
currentDocument = new Document(pdfReader.getPageSizeWithRotation(currentPage));
pdfWriter = new PdfSmartCopy(currentDocument, new FileOutputStream(tmpFile));
// set creator
currentDocument.addCreator(ConsoleServicesFacade.CREATOR);
setCompressionSettingOnWriter(inputCommand, pdfWriter);
setPdfVersionSettingOnWriter(inputCommand, pdfWriter, Character.valueOf(pdfReader.getPdfVersion()));
currentDocument.open();
}
importedPage = pdfWriter.getImportedPage(pdfReader, currentPage);
pdfWriter.addPage(importedPage);
// if it's time to close the document
if (currentPage == endPage) {
LOG.info("Temporary document " + tmpFile.getName() + " done, now adding bookmarks...");
// manage bookmarks
List bookmarks = bookmarkProcessor.processBookmarks(startPage, endPage);
if (bookmarks != null) {
pdfWriter.setOutlines(bookmarks);
}
relativeCurrentPage = 0;
currentDocument.close();