blob: ee5f3bc5915f90d42df5dd6ab230a5d6db66faa2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.pdf;
import java.util.Vector;
/**
* This represents a single Outline object in a PDF, including the root Outlines
* object. Outlines provide the bookmark bar, usually rendered to the right of
* a PDF document in user agents such as Acrobat Reader
*
* @author Kelly A. Campbell
*
*/
public class PDFOutline extends PDFObject {
/**
* list of sub-entries (outline objects)
*/
private Vector _subentries;
/**
* parent outline object. Root Outlines parent is null
*/
private PDFOutline _parent;
private PDFOutline _prev;
private PDFOutline _next;
private PDFOutline _first;
private PDFOutline _last;
private int _count;
/**
* title to display for the bookmark entry
*/
private String _title;
String _actionRef;
/**
* @param number the object id number
* @param title the title of the outline entry (can only be null for root Outlines obj)
* @param page the page which this outline refers to.
*/
public PDFOutline(int number, String title, String action) {
super(number);
_subentries = new Vector();
_count = 0;
_parent = null;
_prev = null;
_next = null;
_first = null;
_last = null;
_title = title;
_actionRef = action;
}
public void setTitle(String title) {
_title = title;
}
/**
* Add a sub element to this outline
*/
public void addOutline(PDFOutline outline) {
if (_subentries.size() > 0) {
outline._prev =
(PDFOutline)_subentries.elementAt(_subentries.size() - 1);
outline._prev._next = outline;
} else {
_first = outline;
}
_subentries.addElement(outline);
outline._parent = this;
incrementCount(); // note: count is not just the immediate children
_last = outline;
}
private void incrementCount() {
// count is a total of our immediate subentries and all descendent subentries
_count++;
if (_parent != null) {
_parent.incrementCount();
}
}
/**
* represent the object in PDF
*/
protected byte[] toPDF() {
StringBuffer result = new StringBuffer(this.number + " "
+ this.generation
+ " obj\n<<\n");
if (_parent == null) {
// root Outlines object
if (_first != null && _last != null) {
result.append(" /First " + _first.referencePDF() + "\n");
result.append(" /Last " + _last.referencePDF() + "\n");
// no count... we start with the outline completely closed for now
}
} else {
// subentry Outline object
result.append(" /Title (" + escapeString(_title) + ")\n");
result.append(" /Parent " + _parent.referencePDF() + "\n");
if (_first != null && _last != null) {
result.append(" /First " + _first.referencePDF() + "\n");
result.append(" /Last " + _last.referencePDF() + "\n");
}
if (_prev != null) {
result.append(" /Prev " + _prev.referencePDF() + "\n");
}
if (_next != null) {
result.append(" /Next " + _next.referencePDF() + "\n");
}
if (_count > 0) {
result.append(" /Count -" + _count + "\n");
}
if (_actionRef != null) {
result.append(" /A " + _actionRef + "\n");
}
}
result.append(">> endobj\n");
return result.toString().getBytes();
}
/**
* escape parens, and other special chars for PDF
*/
private String escapeString(String s) {
StringBuffer result = new StringBuffer();
if (s != null) {
int l = s.length();
for (int i = 0; i < l; i++) {
char ch = s.charAt(i);
if (ch > 127) {
result.append("\\");
result.append(Integer.toOctalString((int)ch));
} else {
switch (ch) {
case '(':
case ')':
case '\\':
result.append('\\');
break;
}
result.append(ch);
}
}
}
return result.toString();
}
}
|