1
2 package org.freehep.graphicsio.pdf;
3
4 import java.io.IOException;
5 import java.io.OutputStream;
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public class PDFWriter extends PDF implements PDFConstants {
20
21 private String open = null;
22
23 public PDFWriter(OutputStream out) throws IOException {
24 this(out, "1.3");
25 }
26
27 public PDFWriter(OutputStream writer, String version) throws IOException {
28 super(new PDFByteWriter(writer));
29
30
31 out.println("%PDF-" + version);
32
33
34 out.print("%");
35 out.write(0xE2);
36 out.write(0xE3);
37 out.write(0xCF);
38 out.write(0xD3);
39 out.println();
40 out.println();
41 }
42
43 public void close(String catalogName, String docInfoName)
44 throws IOException {
45
46
47 xref();
48 trailer(catalogName, docInfoName);
49 startxref();
50 out.printPlain("%%EOF");
51 out.println();
52 out.close();
53 }
54
55 public void comment(String comment) throws IOException {
56 out.println("% " + comment);
57 }
58
59 public void object(String name, Object[] objs) throws IOException {
60 PDFObject object = openObject(name);
61 object.entry(objs);
62 close(object);
63 }
64
65 public void object(String name, int number) throws IOException {
66 PDFObject object = openObject(name);
67 object.entry(number);
68 close(object);
69 }
70
71
72
73
74
75
76
77 public PDFObject openObject(String name) throws IOException {
78
79 if (open != null)
80 System.err
81 .println("PDFWriter error: '" + open + "' was not closed");
82 open = "PDFObject: " + name;
83
84 PDFRef ref = ref(name);
85 int objectNumber = ref.getObjectNumber();
86
87 setXRef(objectNumber, out.getCount());
88 PDFObject obj = new PDFObject(this, out, objectNumber, ref
89 .getGenerationNumber());
90 return obj;
91 }
92
93 public void close(PDFObject object) throws IOException {
94 object.close();
95 open = null;
96 }
97
98 public PDFDictionary openDictionary(String name) throws IOException {
99 PDFObject object = openObject(name);
100 PDFDictionary dictionary = object.openDictionary();
101 return dictionary;
102 }
103
104 public void close(PDFDictionary dictionary) throws IOException {
105 dictionary.close();
106 open = null;
107 }
108
109 private static final String lengthSuffix = "-length";
110
111 public PDFStream openStream(String name) throws IOException {
112 return openStream(name, null);
113 }
114
115 public PDFStream openStream(String name, String[] encode)
116 throws IOException {
117 PDFObject object = openObject(name);
118 PDFStream stream = object.openStream(name, encode);
119 stream.entry("Length", ref(name + lengthSuffix));
120 return stream;
121 }
122
123 public void close(PDFStream stream) throws IOException {
124 stream.close();
125 open = null;
126 object(stream.getName() + lengthSuffix, stream.getLength());
127 }
128
129
130
131
132 private String catalogName;
133
134 private String docInfoName;
135
136 public void close() throws IOException {
137 close(catalogName, docInfoName);
138 }
139
140 public PDFDocInfo openDocInfo(String name) throws IOException {
141 docInfoName = name;
142 PDFObject object = openObject(name);
143 PDFDocInfo info = object.openDocInfo(this);
144 return info;
145 }
146
147 public void close(PDFDocInfo info) throws IOException {
148 info.close();
149 open = null;
150 }
151
152 public PDFCatalog openCatalog(String name, String pageTree)
153 throws IOException {
154 catalogName = name;
155 PDFObject object = openObject(name);
156 PDFCatalog catalog = object.openCatalog(this, ref(pageTree));
157 return catalog;
158 }
159
160 public void close(PDFCatalog catalog) throws IOException {
161 catalog.close();
162 open = null;
163 }
164
165 public PDFPageTree openPageTree(String name, String parent)
166 throws IOException {
167 PDFObject object = openObject(name);
168 PDFPageTree tree = object.openPageTree(this, ref(parent));
169 return tree;
170 }
171
172 public void close(PDFPageTree tree) throws IOException {
173 tree.close();
174 open = null;
175 }
176
177 public PDFPage openPage(String name, String parent) throws IOException {
178 PDFObject object = openObject(name);
179 PDFPage page = object.openPage(this, ref(parent));
180 return page;
181 }
182
183 public void close(PDFPage page) throws IOException {
184 page.close();
185 open = null;
186 }
187
188 public PDFViewerPreferences openViewerPreferences(String name)
189 throws IOException {
190 PDFObject object = openObject(name);
191 PDFViewerPreferences prefs = object.openViewerPreferences(this);
192 return prefs;
193 }
194
195 public void close(PDFViewerPreferences prefs) throws IOException {
196 prefs.close();
197 open = null;
198 }
199
200 public PDFOutlineList openOutlineList(String name, String first, String next)
201 throws IOException {
202 PDFObject object = openObject(name);
203 PDFOutlineList list = object.openOutlineList(this, ref(first),
204 ref(next));
205 return list;
206 }
207
208 public void close(PDFOutlineList list) throws IOException {
209 list.close();
210 open = null;
211 }
212
213 public PDFOutline openOutline(String name, String title, String parent,
214 String prev, String next) throws IOException {
215 PDFObject object = openObject(name);
216 PDFOutline outline = object.openOutline(this, ref(parent), title,
217 ref(prev), ref(next));
218 return outline;
219 }
220
221 public void close(PDFOutline outline) throws IOException {
222 outline.close();
223 open = null;
224 }
225
226 }