KickJava   Java API By Example, From Geeks To Geeks.

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


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

21 public class MergeSample
22   implements ISample
23 {
24   public void run(
25     PDFClownSampleLoader loader
26     )
27   {
28     /*
29       NOTE: This procedure is made up of this sequence of actions:
30       1. User choice.
31       2. Document editing (core).
32       3. Serialization.
33     */

34
35     // 1. User choice.
36
String JavaDoc mainFilePath = loader.getPdfFileChoice("Please select main PDF file");
37     String JavaDoc secondaryFilePath = loader.getPdfFileChoice("Please select secondary PDF file");
38
39     // 2. Document editing.
40
// Opening the PDF files...
41
// Main file.
42
File mainFile;
43     try
44     {
45       mainFile = new File(
46         new FileInputStream(
47           new RandomAccessFile JavaDoc(mainFilePath,"r")
48           )
49         );
50     }
51     catch(FileFormatException e)
52     {throw new RuntimeException JavaDoc(mainFilePath + " file has a bad file format.",e);}
53     catch(Exception JavaDoc e)
54     {throw new RuntimeException JavaDoc(mainFilePath + " file access error.",e);}
55
56     // Secondary file.
57
File secondaryFile;
58     try
59     {
60       secondaryFile = new File(
61         new FileInputStream(
62           new RandomAccessFile JavaDoc(secondaryFilePath,"r")
63           )
64         );
65     }
66     catch(FileFormatException e)
67     {throw new RuntimeException JavaDoc(secondaryFilePath + " file has a bad file format.",e);}
68     catch(Exception JavaDoc e)
69     {throw new RuntimeException JavaDoc(secondaryFilePath + " file access error.",e);}
70
71     // Get the PDF documents!
72
Document mainDocument = mainFile.getDocument();
73     Pages mainPages = mainDocument.getPages();
74     Document secondaryDocument = secondaryFile.getDocument();
75
76     // Append the secondary document's pages to the main document!
77
/*
78       NOTE: To be added to an alien document,
79       pages MUST be firstly contextualized within it,
80       then added to the target pages collection.
81     */

82     mainPages.addAll(
83       (Collection JavaDoc<Page>)mainDocument.contextualize(
84         (Collection JavaDoc<Page>)secondaryDocument.getPages()
85         )
86       );
87     mainPages.update(); // Fundamental to override original page collection.
88

89     // 3. Serialization.
90
loader.serialize(mainFile,this.getClass().getSimpleName());
91   }
92 }
Popular Tags