001: package it.stefanochizzolini.clown.samples;
002:
003: import it.stefanochizzolini.clown.bytes.*;
004: import it.stefanochizzolini.clown.documents.contents.*;
005: import it.stefanochizzolini.clown.documents.contents.entities.*;
006: import it.stefanochizzolini.clown.documents.contents.colorSpaces.*;
007: import it.stefanochizzolini.clown.documents.contents.fonts.*;
008: import it.stefanochizzolini.clown.documents.contents.xObjects.*;
009: import it.stefanochizzolini.clown.objects.*;
010: import it.stefanochizzolini.clown.documents.*;
011: import it.stefanochizzolini.clown.files.*;
012: import it.stefanochizzolini.clown.tokens.*;
013: import java.awt.geom.Point2D;
014: import java.util.*;
015:
016: /**
017: This sample demonstrates how to manipulate the pages collection within a document, to perform
018: movements, additions, removals and extractions of groups of pages.
019: <h3>Remarks</h3>
020: <p>This implementation is based on the <b>contextual cloning</b> functionality
021: of PDF Clown which allows to transparently and intuitively import contents from
022: one document into another.</p>
023: */
024: public class PagesManipulationSample implements ISample {
025: // <class>
026: // <static>
027: // <interface>
028: // <private>
029: private static int getPageChoice(String inputDescription,
030: int pageCount) {
031: Scanner in = new Scanner(System.in);
032: int pageIndex = 0;
033: // Getting the user's choice about which page to remove...
034: System.out.print("\n" + inputDescription + " [1-" + pageCount
035: + "]: ");
036: try {
037: pageIndex = Integer.parseInt(in.nextLine()) - 1; /* Custom choice. */
038: } catch (Exception e) {/* Default choice. */
039: }
040: if (pageIndex < 0)
041: pageIndex = 0;
042: else if (pageIndex >= pageCount)
043: pageIndex = pageCount - 1;
044:
045: return pageIndex;
046: }
047:
048: // </private>
049: // </interface>
050: // </static>
051:
052: // <dynamic>
053: // <interface>
054: // <public>
055: // <ISample>
056: public void run(PDFClownSampleLoader loader) {
057: Scanner in = new Scanner(System.in);
058:
059: while (true) {
060: // (boilerplate user choice -- ignore it)
061: String filePath = loader
062: .getPdfFileChoice("Please select a PDF file");
063:
064: // Open the PDF file!
065: File file;
066: try {
067: file = new File(filePath);
068: } catch (FileFormatException e) {
069: throw new RuntimeException(filePath
070: + " file has a bad file format.", e);
071: } catch (Exception e) {
072: throw new RuntimeException(filePath
073: + " file access error.", e);
074: }
075:
076: // Get the PDF document!
077: Document document = file.getDocument();
078:
079: // Get the page collection!
080: Pages pages = document.getPages();
081: // Get the page count!
082: int pageCount = pages.size();
083: // Are there more than one page?
084: if (pageCount > 1) // Multiple pages.
085: {
086: int action = 0;
087: // Getting the user's choice about which operation to perform...
088: System.out.println("\nAvailable operations:");
089: System.out.println("[0] Page removal");
090: System.out.println("[1] Page addition");
091: System.out.println("[2] Page movement");
092: System.out.println("[3] Page extraction");
093: System.out.print("Select the operation to perform: ");
094: try {
095: action = Integer.parseInt(in.nextLine()); /* Custom choice. */
096: } catch (Exception e) {/* Default choice. */
097: }
098:
099: switch (action) {
100: case 0: // Page removal.
101: {
102: // Get the user's choice about the first page to remove!
103: int fromPageIndex = getPageChoice(
104: "Select the start page to remove",
105: pageCount);
106: // Get the user's choice about the last page to remove!
107: int toPageIndex = getPageChoice(
108: "Select the end page to remove", pageCount) + 1;
109:
110: List<Page> removingPages = pages.subList(
111: fromPageIndex, toPageIndex);
112:
113: // Remove the pages from the pages collection!
114: /* NOTE: Shallow removal. */
115: pages.removeAll(removingPages);
116: pages.update();
117:
118: // Remove the pages from the document (decontextualize)!
119: /* NOTE: Deep removal. */
120: document.decontextualize(removingPages);
121: }
122: break;
123: case 1: // Page addition.
124: {
125: String sourceFilePath = loader
126: .getPdfFileChoice("Select the source PDF file");
127:
128: File sourceFile;
129: try {
130: sourceFile = new File(sourceFilePath);
131: } catch (FileFormatException e) {
132: throw new RuntimeException(sourceFilePath
133: + " file has a bad file format.", e);
134: } catch (Exception e) {
135: throw new RuntimeException(sourceFilePath
136: + " file access error.", e);
137: }
138:
139: // Get the page collection!
140: Pages sourcePages = sourceFile.getDocument()
141: .getPages();
142: // Get the page count!
143: int sourcePageCount = sourcePages.size();
144:
145: // Get the user's choice about the first page to add!
146: int fromSourcePageIndex = getPageChoice(
147: "Select the start source page to add",
148: sourcePageCount);
149: // Get the user's choice about the last page to add!
150: int toSourcePageIndex = getPageChoice(
151: "Select the end source page to add",
152: sourcePageCount) + 1;
153: // Get the user's choice about where to insert the source pages!
154: int targetPageIndex = getPageChoice(
155: "Select the position where to insert the source pages",
156: pageCount + 1);
157:
158: /*
159: NOTE: To be added to an alien document, pages MUST be contextualized within it first,
160: then added to the target pages collection.
161: */
162: // Add the source pages to the document (contextualize)!
163: /* NOTE: Deep addition. */
164: Collection<Page> addingPages = (Collection<Page>) document
165: .contextualize(sourcePages.subList(
166: fromSourcePageIndex,
167: toSourcePageIndex));
168: // Add the source pages to the pages collection!
169: /* NOTE: Shallow addition. */
170: if (targetPageIndex == pageCount) {
171: pages.addAll(addingPages);
172: } else {
173: pages.addAll(targetPageIndex, addingPages);
174: }
175: pages.update();
176: }
177: break;
178: case 2: // Page movement.
179: {
180: // Get the user's choice about the first page to move!
181: int fromSourcePageIndex = getPageChoice(
182: "Select the start page to move", pageCount);
183: // Get the user's choice about the last page to add!
184: int toSourcePageIndex = getPageChoice(
185: "Select the end page to move", pageCount) + 1;
186: // Get the user's choice about where to move the pages!
187: int targetPageIndex = getPageChoice(
188: "Select the position where to insert the pages",
189: pageCount + 1);
190:
191: List<Page> movingPages = pages.subList(
192: fromSourcePageIndex, toSourcePageIndex);
193:
194: // Temporarily remove the pages from the pages collection!
195: /* NOTE: Shallow removal. */
196: pages.removeAll(movingPages);
197:
198: // Adjust indexes!
199: pageCount -= movingPages.size();
200: if (targetPageIndex > fromSourcePageIndex) {
201: targetPageIndex -= movingPages.size(); /* Adjust target position due to shifting for temporary page removal. */
202: }
203:
204: // Reinsert the pages at the target position!
205: /* NOTE: Shallow addition. */
206: if (targetPageIndex == pageCount) {
207: pages.addAll(movingPages);
208: } else {
209: pages.addAll(targetPageIndex, movingPages);
210: }
211: pages.update();
212: }
213: break;
214: case 3: // Page extraction.
215: {
216: // Get the user's choice about the first page to extract!
217: int fromPageIndex = getPageChoice(
218: "Select the start page", pageCount);
219: // Get the user's choice about the last page to extract!
220: int toPageIndex = getPageChoice(
221: "Select the end page", pageCount) + 1;
222:
223: // Instantiate target file!
224: file = new File();
225: document = file.getDocument();
226: // Add the pages to the target file!
227: /*
228: NOTE: To be added to an alien document, pages MUST be contextualized within it first,
229: then added to the target pages collection.
230: */
231: document
232: .getPages()
233: .addAll(
234: (Collection<Page>) document
235: .contextualize(pages
236: .subList(
237: fromPageIndex,
238: toPageIndex)));
239: }
240: break;
241: }
242:
243: // Serialization.
244: loader.serialize(file, "PagesManipulationSample");
245:
246: break;
247: } else // Single page.
248: {
249: System.out
250: .println("\nSorry, the document you selected ("
251: + filePath
252: + ") has just a single page: try another document, please!");
253: }
254: }
255: }
256: // </ISample>
257: // </public>
258: // </interface>
259: // </dynamic>
260: // </class>
261: }
|