try
{mainFile = new File(mainFilePath);}
catch(Exception e)
{throw new RuntimeException(mainFilePath + " file access error.",e);}
final Document mainDocument = mainFile.getDocument();
final Pages mainPages = mainDocument.getPages();
final int mainPagesCount = mainPages.size();
final ActionEnum action = promptAction();
switch(action)
{
case PageDataSizeCalculation:
{
System.out.println("\nThis algorithm calculates the data size (expressed in bytes) of the selected document's pages.");
System.out.println("Legend:");
System.out.println(" * full: page data size encompassing all its dependencies (like shared resources) -- this is the size of the page when extracted as a single-page document;");
System.out.println(" * differential: additional page data size -- this is the extra-content that's not shared with previous pages;");
System.out.println(" * incremental: data size of the page sublist encompassing all the previous pages and the current one.\n");
// Calculating the page data sizes...
Set<PdfReference> visitedReferences = new HashSet<PdfReference>();
long incrementalDataSize = 0;
for(Page page : mainPages)
{
long pageFullDataSize = PageManager.getSize(page);
long pageDifferentialDataSize = PageManager.getSize(page, visitedReferences);
incrementalDataSize += pageDifferentialDataSize;
System.out.println(
"Page " + (page.getIndex()+1) + ": "
+ pageFullDataSize + " (full); "
+ pageDifferentialDataSize + " (differential); "
+ incrementalDataSize + " (incremental)"
);
}
} break;
case PageAddition:
{
// 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);}
}
// Source page collection.
Pages sourcePages = sourceFile.getDocument().getPages();
// Source page count.
int sourcePagesCount = sourcePages.size();
// First page to add.
int fromSourcePageIndex = promptPageChoice("Select the start source page to add", sourcePagesCount);
// Last page to add.
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;