View Javadoc

1   package org.freehep.graphicsio.pdf;
2   
3   import java.io.IOException;
4   
5   /**
6    * Implements a numbered PDFObject.
7    * <p>
8    * 
9    * @author Mark Donszelmann
10   * @version $Id: PDFObject.java 8584 2006-08-10 23:06:37Z duns $
11   */
12  public class PDFObject implements PDFConstants {
13  
14      protected PDF pdf;
15  
16      private PDFByteWriter out;
17  
18      private String open;
19  
20      private boolean ok;
21  
22      PDFObject(PDF pdf, PDFByteWriter writer, int objectNumber,
23              int generationNumber) throws IOException {
24          this.pdf = pdf;
25          out = writer;
26          out.println(objectNumber + " " + generationNumber + " obj");
27          out.indent();
28          ok = true;
29      }
30  
31      void close() throws IOException {
32          out.outdent();
33          out.println("endobj");
34          out.println();
35          ok = false;
36      }
37  
38      public void entry(int number) throws IOException {
39          if (!ok)
40              System.err.println("PDFWriter: 'PDFObject' was closed");
41          out.println(number);
42      }
43  
44      public void entry(Object[] objs) throws IOException {
45          if (!ok)
46              System.err.println("PDFWriter: 'PDFObject' was closed");
47          out.print("[");
48          for (int i = 0; i < objs.length; i++) {
49              if (i != 0)
50                  out.printPlain(" ");
51              out.printPlain(objs[i]);
52          }
53          out.printPlain("]");
54          out.println();
55      }
56  
57      // public void entry(String string) throws IOException {
58      // if (!ok) System.err.println("PDFWriter: 'PDFObject' was closed");
59      // out.println("("+PDFUtil.escape(string)+")");
60      // }
61  
62      PDFDictionary openDictionary() throws IOException {
63          if (!ok)
64              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
65          if (open != null)
66              System.err
67                      .println("PDFWriter error: '" + open + "' was not closed");
68          open = "PDFDictionary";
69          PDFDictionary dictionary = new PDFDictionary(pdf, out, this);
70          return dictionary;
71      }
72  
73      void close(PDFDictionary dictionary) throws IOException {
74          dictionary.close();
75          open = null;
76      }
77  
78      PDFStream openStream(String name, String[] encode) throws IOException {
79          if (!ok)
80              System.err.println("PDFWriter error: 'PDFStream' was closed");
81          if (open != null)
82              System.err
83                      .println("PDFWriter error: '" + open + "' was not closed");
84          open = "PDFStream";
85          PDFStream stream = new PDFStream(pdf, out, name, this, encode);
86          return stream;
87      }
88  
89      void close(PDFStream stream) throws IOException {
90          stream.close();
91          open = null;
92      }
93  
94      PDFDocInfo openDocInfo(PDF pdf) throws IOException {
95          if (!ok)
96              System.err.println("PDFWriter error: 'PDFDocInfo' was closed");
97          if (open != null)
98              System.err
99                      .println("PDFWriter error: '" + open + "' was not closed");
100         open = "PDFDocInfo";
101         PDFDocInfo info = new PDFDocInfo(pdf, out, this);
102         return info;
103     }
104 
105     PDFCatalog openCatalog(PDF pdf, PDFRef pageTree) throws IOException {
106         if (!ok)
107             System.err.println("PDFWriter error: 'PDFCatalog' was closed");
108         if (open != null)
109             System.err
110                     .println("PDFWriter error: '" + open + "' was not closed");
111         open = "PDFCatalog";
112         PDFCatalog catalog = new PDFCatalog(pdf, out, this, pageTree);
113         return catalog;
114     }
115 
116     PDFPageTree openPageTree(PDF pdf, PDFRef parent) throws IOException {
117         if (!ok)
118             System.err.println("PDFWriter error: 'PDFPageTree' was closed");
119         if (open != null)
120             System.err
121                     .println("PDFWriter error: '" + open + "' was not closed");
122         open = "PDFPageTree";
123         PDFPageTree tree = new PDFPageTree(pdf, out, this, parent);
124         return tree;
125     }
126 
127     PDFPage openPage(PDF pdf, PDFRef parent) throws IOException {
128         if (!ok)
129             System.err.println("PDFWriter error: 'PDFPage' was closed");
130         if (open != null)
131             System.err
132                     .println("PDFWriter error: '" + open + "' was not closed");
133         open = "PDFPage";
134         PDFPage page = new PDFPage(pdf, out, this, parent);
135         return page;
136     }
137 
138     PDFViewerPreferences openViewerPreferences(PDF pdf) throws IOException {
139         if (!ok)
140             System.err
141                     .println("PDFWriter error: 'PDFViewerPreferences' was closed");
142         if (open != null)
143             System.err
144                     .println("PDFWriter error: '" + open + "' was not closed");
145         open = "PDFViewerPreferences";
146         PDFViewerPreferences prefs = new PDFViewerPreferences(pdf, out, this);
147         return prefs;
148     }
149 
150     PDFOutlineList openOutlineList(PDF pdf, PDFRef first, PDFRef last)
151             throws IOException {
152         if (!ok)
153             System.err.println("PDFWriter error: 'PDFOutlineList' was closed");
154         if (open != null)
155             System.err
156                     .println("PDFWriter error: '" + open + "' was not closed");
157         open = "PDFOutlineList";
158         PDFOutlineList list = new PDFOutlineList(pdf, out, this, first, last);
159         return list;
160     }
161 
162     PDFOutline openOutline(PDF pdf, PDFRef parent, String title, PDFRef prev,
163             PDFRef next) throws IOException {
164         if (!ok)
165             System.err.println("PDFWriter error: 'PDFOutline' was closed");
166         if (open != null)
167             System.err
168                     .println("PDFWriter error: '" + open + "' was not closed");
169         open = "PDFOutline";
170         PDFOutline outline = new PDFOutline(pdf, out, this, parent, title,
171                 prev, next);
172         return outline;
173     }
174 
175 }