1 package it.stefanochizzolini.clown.samples; 2 3 import it.stefanochizzolini.clown.bytes.*; 4 import it.stefanochizzolini.clown.documents.contents.*; 5 import it.stefanochizzolini.clown.documents.contents.entities.*; 6 import it.stefanochizzolini.clown.documents.contents.colorSpaces.*; 7 import it.stefanochizzolini.clown.documents.contents.fonts.*; 8 import it.stefanochizzolini.clown.documents.contents.xObjects.*; 9 import it.stefanochizzolini.clown.objects.*; 10 import it.stefanochizzolini.clown.documents.*; 11 import it.stefanochizzolini.clown.files.*; 12 import it.stefanochizzolini.clown.tokens.*; 13 import java.awt.geom.Point2D ; 14 import java.util.*; 15 16 24 public class PagesManipulationSample 25 implements ISample 26 { 27 public void run( 28 PDFClownSampleLoader loader 29 ) 30 { 31 37 Scanner in = new Scanner(System.in); 38 39 File file; 40 java.io.RandomAccessFile baseInputStream; 41 while(true) 42 { 43 String filePath = loader.getPdfFileChoice("Please select a PDF file"); 45 46 FileInputStream inputStream; 48 try 49 { 50 baseInputStream = new java.io.RandomAccessFile (filePath,"r"); 52 inputStream = new FileInputStream(baseInputStream); 53 } 54 catch(Exception e) 55 {throw new RuntimeException (filePath + " file access error.",e);} 56 try 57 { 58 file = new File(inputStream); 60 } 61 catch(FileFormatException e) 62 {throw new RuntimeException (filePath + " file has a bad file format.",e);} 63 64 Document doc = file.getDocument(); 66 Pages pages = doc.getPages(); 68 int pageCount = pages.size(); 70 if(pageCount > 1) { 73 int action = 0; 74 System.out.println("\nAvailable operations:"); 76 System.out.println("[0] Page removal"); 77 System.out.println("[1] Page addition"); 78 System.out.println("[2] Page movement"); 79 System.out.println("[3] Page extraction"); 80 System.out.print("Select the operation to perform: "); 81 try{action = Integer.parseInt(in.nextLine()); } 82 catch(Exception e){} 83 84 switch(action) 85 { 86 case 0: { 88 int fromPageIndex = getPageChoice("Select the start page to remove",pageCount); 90 int toPageIndex = getPageChoice("Select the end page to remove",pageCount) + 1; 92 93 List<Page> removingPages = pages.subList(fromPageIndex,toPageIndex); 94 95 97 pages.removeAll(removingPages); pages.update(); 98 99 101 doc.decontextualize(removingPages); 102 103 break; 104 } 105 case 1: { 107 String sourceFilePath = loader.getPdfFileChoice("Select the source PDF file"); 108 java.io.RandomAccessFile baseSourceInputStream; 109 FileInputStream sourceInputStream; 110 try 112 { 113 baseSourceInputStream = new java.io.RandomAccessFile (sourceFilePath,"r"); 114 sourceInputStream = new FileInputStream(baseSourceInputStream); 115 } 116 catch(Exception e) 117 {throw new RuntimeException (sourceFilePath + " file access error.",e);} 118 File sourceFile; 119 try 121 {sourceFile = new File(sourceInputStream);} 122 catch(FileFormatException e) 123 {throw new RuntimeException (sourceFilePath + " file has a bad file format.",e);} 124 Pages sourcePages = sourceFile.getDocument().getPages(); 126 int sourcePageCount = sourcePages.size(); 128 129 int fromSourcePageIndex = getPageChoice("Select the start source page to add",sourcePageCount); 131 int toSourcePageIndex = getPageChoice("Select the end source page to add",sourcePageCount) + 1; 133 int targetPageIndex = getPageChoice("Select the position where to insert the source pages",pageCount+1); 135 136 140 142 Collection<Page> addingPages = (Collection<Page>)doc.contextualize( 143 sourcePages.subList( 144 fromSourcePageIndex, 145 toSourcePageIndex 146 ) 147 ); 148 150 if(targetPageIndex == pageCount) 151 {pages.addAll(addingPages);} 152 else 153 {pages.addAll(targetPageIndex,addingPages);} 154 pages.update(); 155 156 try{baseSourceInputStream.close();} 157 catch(Exception e){} 158 159 break; 160 } 161 case 2: { 163 int fromSourcePageIndex = getPageChoice("Select the start page to move",pageCount); 165 int toSourcePageIndex = getPageChoice("Select the end page to move",pageCount) + 1; 167 int targetPageIndex = getPageChoice("Select the position where to insert the pages",pageCount+1); 169 170 List<Page> movingPages = pages.subList(fromSourcePageIndex,toSourcePageIndex); 171 172 174 pages.removeAll(movingPages); 175 176 pageCount -= movingPages.size(); 178 if(targetPageIndex > fromSourcePageIndex) 179 {targetPageIndex -= movingPages.size(); } 180 181 183 if(targetPageIndex == pageCount) 184 {pages.addAll(movingPages);} 185 else 186 {pages.addAll(targetPageIndex,movingPages);} 187 pages.update(); 188 189 break; 190 } 191 case 3: { 193 int fromPageIndex = getPageChoice("Select the start page",pageCount); 195 int toPageIndex = getPageChoice("Select the end page",pageCount) + 1; 197 198 file = new File(); 200 doc = file.getDocument(); 201 206 doc.getPages().addAll( 207 (Collection<Page>)doc.contextualize( 208 pages.subList(fromPageIndex,toPageIndex) 209 ) 210 ); 211 212 break; 213 } 214 } 215 216 loader.serialize(file,"PagesManipulationSample"); 218 219 try{baseInputStream.close();} 220 catch(Exception e){} 221 222 break; 223 } 224 else { 226 try{baseInputStream.close();} 227 catch(Exception e){} 228 229 System.out.println("\nSorry, the document you selected (" + filePath + ") has just a single page: try another document, please!"); 230 } 231 } 232 } 233 234 private int getPageChoice( 235 String inputDescription, 236 int pageCount 237 ) 238 { 239 Scanner in = new Scanner(System.in); 240 int pageIndex = 0; 241 System.out.print("\n" + inputDescription + " [1-" + pageCount + "]: "); 243 try 244 {pageIndex = Integer.parseInt(in.nextLine()) - 1; } 245 catch(Exception e) 246 {} 247 if(pageIndex < 0) 248 pageIndex = 0; 249 else if(pageIndex >= pageCount) 250 pageIndex = pageCount - 1; 251 252 return pageIndex; 253 } 254 } | Popular Tags |