KickJava   Java API By Example, From Geeks To Geeks.

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


1 package it.stefanochizzolini.clown.samples;
2
3 import it.stefanochizzolini.clown.bytes.FileInputStream;
4 import it.stefanochizzolini.clown.objects.PdfArray;
5 import it.stefanochizzolini.clown.objects.PdfDictionary;
6 import it.stefanochizzolini.clown.objects.PdfIndirectObject;
7 import it.stefanochizzolini.clown.objects.PdfInteger;
8 import it.stefanochizzolini.clown.objects.PdfName;
9 import it.stefanochizzolini.clown.objects.PdfReference;
10 import it.stefanochizzolini.clown.documents.Document;
11 import it.stefanochizzolini.clown.files.File;
12 import it.stefanochizzolini.clown.tokens.FileFormatException;
13
14 import java.io.RandomAccessFile JavaDoc;
15
16 /**
17   This sample demonstrates how to perform advanced editing over a PDF document
18   structure accessing primitive objects. Particularly, it adds an 'open action' to
19   the document so that it starts with the content of page 2 magnified just enough to
20   fit the height of the page within the window.
21 */

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

35
36     // 1. User choice.
37
String JavaDoc filePath = loader.getPdfFileChoice("Please select a PDF file");
38
39     // 2. Document editing.
40
File file;
41     try
42     {
43       // Open the PDF file!
44
file = new File(
45         new FileInputStream(
46           new RandomAccessFile JavaDoc(filePath,"r")
47           )
48         );
49     }
50     catch(FileFormatException e)
51     {throw new RuntimeException JavaDoc(filePath + " file has a bad file format.",e);}
52     catch(Exception JavaDoc e)
53     {throw new RuntimeException JavaDoc(filePath + " file access error.",e);}
54
55     // Get the PDF document!
56
Document document = file.getDocument();
57
58     // Create the action dictionary!
59
PdfDictionary action = new PdfDictionary();
60
61     // Define a go-to action!
62
action.put(new PdfName("S"),new PdfName("GoTo"));
63
64     // Create the destination array!
65
PdfArray destination = new PdfArray();
66
67     // Associate the destination to the action!
68
action.put(new PdfName("D"),destination);
69
70     // Get a reference of the 2nd page!
71
PdfReference pageReference = (PdfReference)document.getPages().get(1).getBaseObject();
72
73     // Associate the 2nd page to the destination!
74
destination.add(pageReference);
75
76     // Define the location of the document window on the page (fit vertically)!
77
destination.add(new PdfName("FitV"));
78
79     // Define window left-edge horizontal coordinate!
80
destination.add(new PdfInteger(-32768));
81
82     // Add the action to the file!
83
PdfIndirectObject actionIndirect = file.getIndirectObjects().add(action);
84
85     // Associate the action to the document!
86
document.getBaseDataObject().put(new PdfName("OpenAction"),actionIndirect.getReference());
87     document.update();
88
89     // 3. Serialization.
90
loader.serialize(file,this.getClass().getSimpleName());
91   }
92 }
Popular Tags