1 50 51 package com.lowagie.text.pdf; 52 53 import java.util.HashMap ; 54 import java.util.Iterator ; 55 import java.util.TreeMap ; 56 57 import com.lowagie.text.factories.RomanAlphabetFactory; 58 import com.lowagie.text.factories.RomanNumberFactory; 59 60 64 public class PdfPageLabels { 65 66 68 public static final int DECIMAL_ARABIC_NUMERALS = 0; 69 71 public static final int UPPERCASE_ROMAN_NUMERALS = 1; 72 74 public static final int LOWERCASE_ROMAN_NUMERALS = 2; 75 78 public static final int UPPERCASE_LETTERS = 3; 79 82 public static final int LOWERCASE_LETTERS = 4; 83 86 public static final int EMPTY = 5; 87 89 static PdfName numberingStyle[] = new PdfName[]{PdfName.D, PdfName.R, 90 new PdfName("r"), PdfName.A, new PdfName("a")}; 91 93 TreeMap map; 94 95 97 public PdfPageLabels() { 98 map = new TreeMap (); 99 addPageLabel(1, DECIMAL_ARABIC_NUMERALS, null, 1); 100 } 101 102 108 public void addPageLabel(int page, int numberStyle, String text, int firstPage) { 109 if (page < 1 || firstPage < 1) 110 throw new IllegalArgumentException ("In a page label the page numbers must be greater or equal to 1."); 111 PdfName pdfName = null; 112 if (numberStyle >= 0 && numberStyle < numberingStyle.length) 113 pdfName = numberingStyle[numberStyle]; 114 Integer iPage = new Integer (page); 115 Object obj = new Object []{iPage, pdfName, text, new Integer (firstPage)}; 116 map.put(iPage, obj); 117 } 118 119 125 public void addPageLabel(int page, int numberStyle, String text) { 126 addPageLabel(page, numberStyle, text, 1); 127 } 128 129 134 public void addPageLabel(int page, int numberStyle) { 135 addPageLabel(page, numberStyle, null, 1); 136 } 137 138 141 public void removePageLabel(int page) { 142 if (page <= 1) 143 return; 144 map.remove(new Integer (page)); 145 } 146 147 150 PdfDictionary getDictionary() { 151 PdfDictionary dic = new PdfDictionary(); 152 PdfArray array = new PdfArray(); 153 for (Iterator it = map.values().iterator(); it.hasNext();) { 154 Object obj[] = (Object [])it.next(); 155 PdfDictionary subDic = new PdfDictionary(); 156 PdfName pName = (PdfName)obj[1]; 157 if (pName != null) 158 subDic.put(PdfName.S, pName); 159 String text = (String )obj[2]; 160 if (text != null) 161 subDic.put(PdfName.P, new PdfString(text, PdfObject.TEXT_UNICODE)); 162 int st = ((Integer )obj[3]).intValue(); 163 if (st != 1) 164 subDic.put(PdfName.ST, new PdfNumber(st)); 165 array.add(new PdfNumber(((Integer )obj[0]).intValue() - 1)); 166 array.add(subDic); 167 } 168 dic.put(PdfName.NUMS, array); 169 return dic; 170 } 171 172 177 public static String [] getPageLabels(PdfReader reader) { 178 179 int n = reader.getNumberOfPages(); 180 String [] labelstrings = new String [n]; 181 182 PdfDictionary dict = reader.getCatalog(); 183 PdfDictionary labels = (PdfDictionary)PdfReader.getPdfObject((PdfObject)dict.get(PdfName.PAGELABELS)); 184 PdfArray numbers = (PdfArray)PdfReader.getPdfObject((PdfObject)labels.get(PdfName.NUMS)); 185 186 PdfNumber pageIndex; 187 PdfDictionary pageLabel; 188 HashMap numberTree = new HashMap (); 189 for (Iterator i = numbers.listIterator(); i.hasNext(); ) { 190 pageIndex = (PdfNumber)i.next(); 191 pageLabel = (PdfDictionary) PdfReader.getPdfObject((PdfObject)i.next()); 192 numberTree.put(new Integer (pageIndex.intValue()), pageLabel); 193 } 194 195 int pagecount = 1; 196 Integer current; 197 String prefix = ""; 198 char type = 'D'; 199 for (int i = 0; i < n; i++) { 200 current = new Integer (i); 201 if (numberTree.containsKey(current)) { 202 PdfDictionary d = (PdfDictionary)numberTree.get(current); 203 if (d.contains(PdfName.ST)) { 204 pagecount = ((PdfNumber)d.get(PdfName.ST)).intValue(); 205 } 206 else { 207 pagecount = 1; 208 } 209 if (d.contains(PdfName.P)) { 210 prefix = ((PdfString)d.get(PdfName.P)).toString(); 211 } 212 if (d.contains(PdfName.S)) { 213 type = ((PdfName)d.get(PdfName.S)).toString().charAt(1); 214 } 215 } 216 switch(type) { 217 default: 218 labelstrings[i] = prefix + pagecount; 219 break; 220 case 'R': 221 labelstrings[i] = prefix + RomanNumberFactory.getUpperCaseString(pagecount); 222 break; 223 case 'r': 224 labelstrings[i] = prefix + RomanNumberFactory.getLowerCaseString(pagecount); 225 break; 226 case 'A': 227 labelstrings[i] = prefix + RomanAlphabetFactory.getUpperCaseString(pagecount); 228 break; 229 case 'a': 230 labelstrings[i] = prefix + RomanAlphabetFactory.getLowerCaseString(pagecount); 231 break; 232 } 233 pagecount++; 234 } 235 return labelstrings; 236 } 237 } | Popular Tags |