1 51 package org.apache.fop.pdf; 52 53 import java.io.UnsupportedEncodingException ; 55 import java.util.ArrayList ; 56 57 58 66 67 public class PDFOutline extends PDFObject { 68 69 72 private ArrayList _subentries; 73 74 77 private PDFOutline _parent; 78 79 private PDFOutline _prev; 80 private PDFOutline _next; 81 82 private PDFOutline _first; 83 private PDFOutline _last; 84 85 private int _count; 86 87 88 91 private String _title; 92 93 String _actionRef; 94 95 96 97 102 public PDFOutline(int number, String title, String action) { 103 super(number); 104 _subentries = new ArrayList (); 105 _count = 0; 106 _parent = null; 107 _prev = null; 108 _next = null; 109 _first = null; 110 _last = null; 111 _title = title; 112 _actionRef = action; 113 114 115 } 116 117 public void setTitle(String title) { 118 _title = title; 119 } 120 121 124 public void addOutline(PDFOutline outline) { 125 if (_subentries.size() > 0) { 126 outline._prev = 127 (PDFOutline)_subentries.get(_subentries.size() - 1); 128 outline._prev._next = outline; 129 } else { 130 _first = outline; 131 } 132 133 _subentries.add(outline); 134 outline._parent = this; 135 136 incrementCount(); 138 _last = outline; 139 140 } 141 142 private void incrementCount() { 143 _count++; 145 if (_parent != null) { 146 _parent.incrementCount(); 147 } 148 } 149 150 151 154 protected byte[] toPDF() { 155 StringBuffer result = new StringBuffer (this.number + " " 156 + this.generation 157 + " obj\n<<\n"); 158 if (_parent == null) { 159 if (_first != null && _last != null) { 161 result.append(" /First " + _first.referencePDF() + "\n"); 162 result.append(" /Last " + _last.referencePDF() + "\n"); 163 } 165 } else { 166 result.append(" /Title (" + escapeString(_title) + ")\n"); 168 result.append(" /Parent " + _parent.referencePDF() + "\n"); 169 if (_first != null && _last != null) { 170 result.append(" /First " + _first.referencePDF() + "\n"); 171 result.append(" /Last " + _last.referencePDF() + "\n"); 172 } 173 if (_prev != null) { 174 result.append(" /Prev " + _prev.referencePDF() + "\n"); 175 } 176 if (_next != null) { 177 result.append(" /Next " + _next.referencePDF() + "\n"); 178 } 179 if (_count > 0) { 180 result.append(" /Count -" + _count + "\n"); 181 } 182 183 if (_actionRef != null) { 184 result.append(" /A " + _actionRef + "\n"); 185 } 186 187 188 } 189 result.append(">> endobj\n"); 190 191 try { 192 return result.toString().getBytes(PDFDocument.ENCODING); 193 } catch (UnsupportedEncodingException ue) { 194 return result.toString().getBytes(); 195 } 196 } 197 198 201 private String escapeString(String s) { 202 StringBuffer result = new StringBuffer (); 203 if (s != null) { 204 int l = s.length(); 205 206 result.append("\\376\\377"); 208 209 for (int i = 0; i < l; i++) { 210 char ch = s.charAt(i); 211 int high = (ch & 0xff00) >>> 8; 212 int low = ch & 0xff; 213 result.append("\\"); 214 result.append(Integer.toOctalString(high)); 215 result.append("\\"); 216 result.append(Integer.toOctalString(low)); 217 } 218 } 219 220 return result.toString(); 221 } 222 223 } 224 | Popular Tags |