package it.stefanochizzolini.clown.samples; import it.stefanochizzolini.clown.documents.Document; import it.stefanochizzolini.clown.documents.Pages; import it.stefanochizzolini.clown.files.File; import it.stefanochizzolini.clown.tools.PageManager; import java.util.Scanner; /** This sample demonstrates how to manipulate the pages collection within a document, to perform movements, additions, removals and extractions of groups of pages. @author Stefano Chizzolini (http://www.stefanochizzolini.it) @version 0.0.6 */ public class PageManagementSample implements ISample { // // // // private static int getPageChoice( String inputDescription, int pageCount ) { Scanner in = new Scanner(System.in); int pageIndex = 0; // Getting the user's choice about which page to remove... System.out.print("\n" + inputDescription + " [1-" + pageCount + "]: "); try {pageIndex = Integer.parseInt(in.nextLine()) - 1; /* Custom choice. */} catch(Exception e) {/* Default choice. */} if(pageIndex < 0) pageIndex = 0; else if(pageIndex >= pageCount) pageIndex = pageCount - 1; return pageIndex; } // // // // // // // public void run( PDFClownSampleLoader loader ) { Scanner in = new Scanner(System.in); while(true) { // (boilerplate user choice -- ignore it) String filePath = loader.getPdfFileChoice("Please select a PDF file"); // Open the PDF file! File file; try{file = new File(filePath);} catch(Exception e){throw new RuntimeException(filePath + " file access error.",e);} // Get the PDF document! Document document = file.getDocument(); // Get the page count! int pageCount = document.getPages().size(); // Are there more than one page? if(pageCount > 1) // Multiple pages. { // Getting the user's choice about which operation to perform... System.out.println("\nAvailable operations:"); System.out.println("[0] Page removal"); System.out.println("[1] Page addition"); System.out.println("[2] Page movement"); System.out.println("[3] Page extraction"); System.out.print("Select the operation to perform: "); int action = 0; try{action = Integer.parseInt(in.nextLine()); /* Custom choice. */} catch(Exception e){/* Default choice. */} switch(action) { case 0: // Page removal. { // Get the user's choice about the first page to remove! int fromPageIndex = getPageChoice("Select the start page to remove",pageCount); // Get the user's choice about the last page to remove! int toPageIndex = getPageChoice("Select the end page to remove",pageCount) + 1; new PageManager(document).remove( fromPageIndex, toPageIndex ); } break; case 1: // Page addition. { File sourceFile; { String sourceFilePath = loader.getPdfFileChoice("Select the source PDF file"); try{sourceFile = new File(sourceFilePath);} catch(Exception e){throw new RuntimeException(sourceFilePath + " file access error.",e);} } // Get the source page collection! Pages sourcePages = sourceFile.getDocument().getPages(); // Get the source page count! int sourcePageCount = sourcePages.size(); // Get the user's choice about the first page to add! int fromSourcePageIndex = getPageChoice("Select the start source page to add",sourcePageCount); // Get the user's choice about the last page to add! int toSourcePageIndex = getPageChoice("Select the end source page to add",sourcePageCount) + 1; // Get the user's choice about where to insert the source pages! int targetPageIndex = getPageChoice("Select the position where to insert the source pages",pageCount+1); new PageManager(document).add( targetPageIndex, sourcePages.subList( fromSourcePageIndex, toSourcePageIndex ) ); } break; case 2: // Page movement. { // Get the user's choice about the first page to move! int fromSourcePageIndex = getPageChoice("Select the start page to move",pageCount); // Get the user's choice about the last page to add! int toSourcePageIndex = getPageChoice("Select the end page to move",pageCount) + 1; // Get the user's choice about where to move the pages! int targetPageIndex = getPageChoice("Select the position where to insert the pages",pageCount+1); new PageManager(document).move( fromSourcePageIndex, toSourcePageIndex, targetPageIndex ); } break; case 3: // Page extraction. { // Get the user's choice about the first page to extract! int fromPageIndex = getPageChoice("Select the start page",pageCount); // Get the user's choice about the last page to extract! int toPageIndex = getPageChoice("Select the end page",pageCount) + 1; document = new PageManager(document).extract( fromPageIndex, toPageIndex ); file = document.getFile(); } break; } // Serialization. loader.serialize(file,this.getClass().getSimpleName()); break; } else // Single page. { System.out.println("\nSorry, the document you selected (" + filePath + ") has just a single page: try another document, please!"); } } } // // // // // }