KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfPageLabels


1 /*
2  * $Id: PdfPageLabels.java 2685 2007-04-16 12:09:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 2001, 2002 Paulo Soares
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.util.HashMap JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.TreeMap JavaDoc;
56
57 import com.lowagie.text.factories.RomanAlphabetFactory;
58 import com.lowagie.text.factories.RomanNumberFactory;
59
60 /** Page labels are used to identify each
61  * page visually on the screen or in print.
62  * @author Paulo Soares (psoares@consiste.pt)
63  */

64 public class PdfPageLabels {
65
66     /** Logical pages will have the form 1,2,3,...
67      */

68     public static final int DECIMAL_ARABIC_NUMERALS = 0;
69     /** Logical pages will have the form I,II,III,IV,...
70      */

71     public static final int UPPERCASE_ROMAN_NUMERALS = 1;
72     /** Logical pages will have the form i,ii,iii,iv,...
73      */

74     public static final int LOWERCASE_ROMAN_NUMERALS = 2;
75     /** Logical pages will have the form of uppercase letters
76      * (A to Z for the first 26 pages, AA to ZZ for the next 26, and so on)
77      */

78     public static final int UPPERCASE_LETTERS = 3;
79     /** Logical pages will have the form of uppercase letters
80      * (a to z for the first 26 pages, aa to zz for the next 26, and so on)
81      */

82     public static final int LOWERCASE_LETTERS = 4;
83     /** No logical page numbers are generated but fixed text may
84      * still exist
85      */

86     public static final int EMPTY = 5;
87     /** Dictionary values to set the logical page styles
88      */

89     static PdfName numberingStyle[] = new PdfName[]{PdfName.D, PdfName.R,
90                 new PdfName("r"), PdfName.A, new PdfName("a")};
91     /** The sequence of logical pages. Will contain at least a value for page 1
92      */

93     TreeMap JavaDoc map;
94     
95     /** Creates a new PdfPageLabel with a default logical page 1
96      */

97     public PdfPageLabels() {
98         map = new TreeMap JavaDoc();
99         addPageLabel(1, DECIMAL_ARABIC_NUMERALS, null, 1);
100     }
101
102     /** Adds or replaces a page label.
103      * @param page the real page to start the numbering. First page is 1
104      * @param numberStyle the numbering style such as LOWERCASE_ROMAN_NUMERALS
105      * @param text the text to prefix the number. Can be <CODE>null</CODE> or empty
106      * @param firstPage the first logical page number
107      */

108     public void addPageLabel(int page, int numberStyle, String JavaDoc text, int firstPage) {
109         if (page < 1 || firstPage < 1)
110             throw new IllegalArgumentException JavaDoc("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 JavaDoc iPage = new Integer JavaDoc(page);
115         Object JavaDoc obj = new Object JavaDoc[]{iPage, pdfName, text, new Integer JavaDoc(firstPage)};
116         map.put(iPage, obj);
117     }
118
119     /** Adds or replaces a page label. The first logical page has the default
120      * of 1.
121      * @param page the real page to start the numbering. First page is 1
122      * @param numberStyle the numbering style such as LOWERCASE_ROMAN_NUMERALS
123      * @param text the text to prefix the number. Can be <CODE>null</CODE> or empty
124      */

125     public void addPageLabel(int page, int numberStyle, String JavaDoc text) {
126         addPageLabel(page, numberStyle, text, 1);
127     }
128     
129     /** Adds or replaces a page label. There is no text prefix and the first
130      * logical page has the default of 1.
131      * @param page the real page to start the numbering. First page is 1
132      * @param numberStyle the numbering style such as LOWERCASE_ROMAN_NUMERALS
133      */

134     public void addPageLabel(int page, int numberStyle) {
135         addPageLabel(page, numberStyle, null, 1);
136     }
137     
138     /** Removes a page label. The first page label can not be removed, only changed.
139      * @param page the real page to remove
140      */

141     public void removePageLabel(int page) {
142         if (page <= 1)
143             return;
144         map.remove(new Integer JavaDoc(page));
145     }
146
147     /** Gets the page label dictionary to insert into the document.
148      * @return the page label dictionary
149      */

150     PdfDictionary getDictionary() {
151         PdfDictionary dic = new PdfDictionary();
152         PdfArray array = new PdfArray();
153         for (Iterator JavaDoc it = map.values().iterator(); it.hasNext();) {
154             Object JavaDoc obj[] = (Object JavaDoc[])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 JavaDoc text = (String JavaDoc)obj[2];
160             if (text != null)
161                 subDic.put(PdfName.P, new PdfString(text, PdfObject.TEXT_UNICODE));
162             int st = ((Integer JavaDoc)obj[3]).intValue();
163             if (st != 1)
164                 subDic.put(PdfName.ST, new PdfNumber(st));
165             array.add(new PdfNumber(((Integer JavaDoc)obj[0]).intValue() - 1));
166             array.add(subDic);
167         }
168         dic.put(PdfName.NUMS, array);
169         return dic;
170     }
171     
172     /**
173      * Retrieves the page labels from a PDF as an array of String objects.
174      * @param reader a PdfReader object that has the page labels you want to retrieve
175      * @return a String array
176      */

177     public static String JavaDoc[] getPageLabels(PdfReader reader) {
178         
179         int n = reader.getNumberOfPages();
180         String JavaDoc[] labelstrings = new String JavaDoc[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 JavaDoc numberTree = new HashMap JavaDoc();
189         for (Iterator JavaDoc i = numbers.listIterator(); i.hasNext(); ) {
190             pageIndex = (PdfNumber)i.next();
191             pageLabel = (PdfDictionary) PdfReader.getPdfObject((PdfObject)i.next());
192             numberTree.put(new Integer JavaDoc(pageIndex.intValue()), pageLabel);
193         }
194         
195         int pagecount = 1;
196         Integer JavaDoc current;
197         String JavaDoc prefix = "";
198         char type = 'D';
199         for (int i = 0; i < n; i++) {
200             current = new Integer JavaDoc(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