Source Code Cross Referenced for PagesManipulationSample.java in  » PDF » PDFClown-0.0.5 » it » stefanochizzolini » clown » samples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » PDFClown 0.0.5 » it.stefanochizzolini.clown.samples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.