KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > samples > SplitSample


1 package it.stefanochizzolini.clown.samples;
2
3 import it.stefanochizzolini.clown.bytes.FileInputStream;
4 import it.stefanochizzolini.clown.documents.Document;
5 import it.stefanochizzolini.clown.documents.Page;
6 import it.stefanochizzolini.clown.files.File;
7 import it.stefanochizzolini.clown.tokens.FileFormatException;
8
9 import java.io.RandomAccessFile JavaDoc;
10
11 /**
12   This sample demonstrates how to split a document into its constituting pages.
13   <h3>Remarks</h3>
14   <p>This implementation is based on the <b>contextual cloning</b> functionality
15   of PDF Clown which allows to transparently and intuitively import contents from
16   one document to another.</p>
17 */

18 public class SplitSample
19   implements ISample
20 {
21   public void run(
22     PDFClownSampleLoader loader
23     )
24   {
25     /*
26       NOTE: This procedure is made up of this sequence of actions:
27       1. User choice.
28       2. Document editing.
29       2.1. Page file creation.
30       3. Page file serialization.
31     */

32
33     // 1. User choice.
34
String JavaDoc filePath = loader.getPdfFileChoice("Please select a PDF file");
35
36     // 2. Document editing.
37
File file;
38     try
39     {
40       // Open the PDF file!
41
file = new File(
42         new FileInputStream(
43           new RandomAccessFile JavaDoc(filePath,"r")
44           )
45         );
46     }
47     catch(FileFormatException e)
48     {throw new RuntimeException JavaDoc(filePath + " file has a bad file format.",e);}
49     catch(Exception JavaDoc e)
50     {throw new RuntimeException JavaDoc(filePath + " file access error.",e);}
51
52     // Get the PDF documents!
53
Document document = file.getDocument();
54
55     // Splitting the document into single-page documents...
56
int index = 0;
57     for(Page page : document.getPages())
58     {
59       // 2.1. Page file creation.
60
File pageFile = new File();
61       Document pageDocument = pageFile.getDocument();
62       // Append current page to its own document!
63
/*
64         NOTE: Contextual cloning in action.
65       */

66       pageDocument.getPages().add((Page)page.clone(pageDocument));
67       // 3. Page file serialization.
68
loader.serialize(pageFile,this.getClass().getSimpleName() + "." + (++index),false);
69     }
70   }
71 }
Popular Tags