KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PdfLister.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 2002 Mark Thompson
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  * This class by Mark Thompson. Copyright (C) 2002 Mark Thompson
24  *
25  * Contributor(s): all the names of the contributors are added in the source code
26  * where applicable.
27  *
28  * Alternatively, the contents of this file may be used under the terms of the
29  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
30  * provisions of LGPL are applicable instead of those above. If you wish to
31  * allow use of your version of this file only under the terms of the LGPL
32  * License and not to allow others to use your version of this file under
33  * the MPL, indicate your decision by deleting the provisions above and
34  * replace them with the notice and other provisions required by the LGPL.
35  * If you do not delete the provisions above, a recipient may use your version
36  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
37  *
38  * This library is free software; you can redistribute it and/or modify it
39  * under the terms of the MPL as stated above or under the terms of the GNU
40  * Library General Public License as published by the Free Software Foundation;
41  * either version 2 of the License, or any later version.
42  *
43  * This library is distributed in the hope that it will be useful, but WITHOUT
44  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
45  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
46  * details.
47  *
48  * If you didn't download this code from the following link, you should check if
49  * you aren't using an obsolete version:
50  * http://www.lowagie.com/iText/
51  */

52
53  package com.lowagie.text.pdf;
54
55 import java.io.IOException JavaDoc;
56 import java.io.PrintStream JavaDoc;
57 import java.util.Iterator JavaDoc;
58 /**
59  * List a PDF file in human-readable form (for debugging reasons mostly)
60  * @author Mark Thompson
61  */

62
63 public class PdfLister {
64
65     /** the printStream you want to write the output to. */
66     PrintStream JavaDoc out;
67
68     /**
69      * Create a new lister object.
70      * @param out
71      */

72     public PdfLister(PrintStream JavaDoc out) {
73         this.out = out;
74     }
75
76     /**
77      * Visualizes a PDF object.
78      * @param object a com.lowagie.text.pdf object
79      */

80     public void listAnyObject(PdfObject object)
81     {
82         switch (object.type()) {
83         case PdfObject.ARRAY:
84             listArray((PdfArray)object);
85             break;
86         case PdfObject.DICTIONARY:
87             listDict((PdfDictionary) object);
88             break;
89         case PdfObject.STRING:
90             out.println("(" + object.toString() + ")");
91             break;
92         default:
93             out.println(object.toString());
94             break;
95         }
96     }
97     /**
98      * Visualizes a PdfDictionary object.
99      * @param dictionary a com.lowagie.text.pdf.PdfDictionary object
100      */

101     public void listDict(PdfDictionary dictionary)
102     {
103         out.println("<<");
104         PdfName key;
105         PdfObject value;
106         for (Iterator JavaDoc i = dictionary.getKeys().iterator(); i.hasNext(); ) {
107             key = (PdfName) i.next();
108             value = dictionary.get(key);
109             out.print(key.toString());
110             out.print(' ');
111             listAnyObject(value);
112         }
113         out.println(">>");
114     }
115
116     /**
117      * Visualizes a PdfArray object.
118      * @param array a com.lowagie.text.pdf.PdfArray object
119      */

120     public void listArray(PdfArray array)
121     {
122         out.println('[');
123         for (Iterator JavaDoc i = array.getArrayList().iterator(); i.hasNext(); ) {
124             PdfObject item = (PdfObject)i.next();
125             listAnyObject(item);
126         }
127         out.println(']');
128     }
129     /**
130      * Visualizes a Stream.
131      * @param stream
132      * @param reader
133      */

134     public void listStream(PRStream stream, PdfReaderInstance reader)
135     {
136         try {
137             listDict(stream);
138             out.println("startstream");
139             byte[] b = PdfReader.getStreamBytes(stream);
140 // byte buf[] = new byte[Math.min(stream.getLength(), 4096)];
141
// int r = 0;
142
// stream.openStream(reader);
143
// for (;;) {
144
// r = stream.readStream(buf, 0, buf.length);
145
// if (r == 0) break;
146
// out.write(buf, 0, r);
147
// }
148
// stream.closeStream();
149
int len = b.length - 1;
150             for (int k = 0; k < len; ++k) {
151                 if (b[k] == '\r' && b[k + 1] != '\n')
152                     b[k] = (byte)'\n';
153             }
154             out.println(new String JavaDoc(b));
155             out.println("endstream");
156         } catch (IOException JavaDoc e) {
157             System.err.println("I/O exception: " + e);
158 // } catch (java.util.zip.DataFormatException e) {
159
// System.err.println("Data Format Exception: " + e);
160
}
161     }
162     /**
163      * Visualizes an imported page
164      * @param iPage
165      */

166     public void listPage(PdfImportedPage iPage)
167     {
168         int pageNum = iPage.getPageNumber();
169         PdfReaderInstance readerInst = iPage.getPdfReaderInstance();
170         PdfReader reader = readerInst.getReader();
171
172         PdfDictionary page = reader.getPageN(pageNum);
173         listDict(page);
174         PdfObject obj = PdfReader.getPdfObject(page.get(PdfName.CONTENTS));
175         if (obj == null)
176             return;
177         switch (obj.type) {
178         case PdfObject.STREAM:
179             listStream((PRStream)obj, readerInst);
180             break;
181         case PdfObject.ARRAY:
182             for (Iterator JavaDoc i = ((PdfArray)obj).getArrayList().iterator(); i.hasNext();) {
183                 PdfObject o = PdfReader.getPdfObject((PdfObject)i.next());
184                 listStream((PRStream)o, readerInst);
185                 out.println("-----------");
186             }
187             break;
188         }
189     }
190 }
191
Popular Tags