int toSourcePageIndex = promptPageChoice("Select the end source page to add", fromSourcePageIndex + 1, sourcePagesCount) + 1;
// Target position.
int targetPageIndex = promptPageChoice("Select the position where to insert the source pages", mainPagesCount + 1);
// Add the chosen page range to the main document!
new PageManager(mainDocument).add(
targetPageIndex,
sourcePages.subList(
fromSourcePageIndex,
toSourcePageIndex
)
);
// Serialize the main file!
serialize(mainFile, action);
} break;
case PageMovement:
{
// First page to move.
int fromSourcePageIndex = promptPageChoice("Select the start page to move", mainPagesCount);
// Last page to move.
int toSourcePageIndex = promptPageChoice("Select the end page to move", fromSourcePageIndex + 1, mainPagesCount) + 1;
// Target position.
int targetPageIndex = promptPageChoice("Select the position where to insert the pages", mainPagesCount + 1);
// Move the chosen page range!
new PageManager(mainDocument).move(
fromSourcePageIndex,
toSourcePageIndex,
targetPageIndex
);
// Serialize the main file!
serialize(mainFile, action);
} break;
case PageRemoval:
{
// First page to remove.
int fromPageIndex = promptPageChoice("Select the start page to remove", mainPagesCount);
// Last page to remove.
int toPageIndex = promptPageChoice("Select the end page to remove", fromPageIndex + 1, mainPagesCount) + 1;
// Remove the chosen page range!
new PageManager(mainDocument).remove(
fromPageIndex,
toPageIndex
);
// Serialize the main file!
serialize(mainFile, action);
} break;
case PageExtraction:
{
// First page to extract.
int fromPageIndex = promptPageChoice("Select the start page", mainPagesCount);
// Last page to extract.
int toPageIndex = promptPageChoice("Select the end page", fromPageIndex + 1, mainPagesCount) + 1;
// Extract the chosen page range!
Document targetDocument = new PageManager(mainDocument).extract(
fromPageIndex,
toPageIndex
);
// Serialize the target file!
serialize(targetDocument.getFile(), action);
} break;
case DocumentMerge:
{
// Source file.
File sourceFile;
{
String sourceFilePath = promptPdfFileChoice("Select the source PDF file");
try
{sourceFile = new File(sourceFilePath);}
catch(Exception e)
{throw new RuntimeException(sourceFilePath + " file access error.",e);}
}
// Append the chosen source document to the main document!
new PageManager(mainDocument).add(sourceFile.getDocument());
// Serialize the main file!
serialize(mainFile, action);
} break;
case DocumentBurst:
{
// Split the document into single-page documents!
List<Document> splitDocuments = new PageManager(mainDocument).split();
// Serialize the split files!
int index = 0;
for(Document splitDocument : splitDocuments)
{serialize(splitDocument.getFile(), action, ++index, false);}
} break;
case DocumentSplitByPageIndex:
{
// Number of splits to apply to the source document.
int splitCount;
try
{splitCount = Integer.parseInt(promptChoice("Number of split positions: "));}
catch(Exception e)
{splitCount = 0;}
// Split positions within the source document.
int[] splitIndexes = new int[splitCount];
{
int prevSplitIndex = 0;
for(int index = 0; index < splitCount; index++)
{
int splitIndex = promptPageChoice("Position " + (index + 1) + " of " + splitCount, prevSplitIndex + 1, mainPagesCount);
splitIndexes[index] = splitIndex;
prevSplitIndex = splitIndex;
}
}
// Split the document at the chosen positions!
List<Document> splitDocuments = new PageManager(mainDocument).split(splitIndexes);
// Serialize the split files!
int index = 0;
for(Document splitDocument : splitDocuments)
{serialize(splitDocument.getFile(), action, ++index, false);}
} break;
case DocumentSplitOnMaximumFileSize:
{
// Maximum file size.
long maxDataSize;
{
long mainFileSize = new java.io.File(mainFilePath).length();
int kbMaxDataSize;
do
{
try
{kbMaxDataSize = Integer.parseInt(promptChoice("Max file size (KB): "));}
catch(Exception e)
{kbMaxDataSize = 0;}
} while(kbMaxDataSize == 0);
maxDataSize = kbMaxDataSize << 10;
if(maxDataSize > mainFileSize)
{maxDataSize = mainFileSize;}
}
// Split the document on maximum file size!
List<Document> splitDocuments = new PageManager(mainDocument).split(maxDataSize);
// Serialize the split files!
int index = 0;
for(Document splitDocument : splitDocuments)
{serialize(splitDocument.getFile(), action, ++index, false);}