001/* 002 * $Id: PdfLister.java 4784 2011-03-15 08:33:00Z blowagie $ 003 * 004 * This file is part of the iText (R) project. 005 * Copyright (c) 1998-2011 1T3XT BVBA 006 * Authors: Bruno Lowagie, Paulo Soares, et al. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Affero General Public License version 3 010 * as published by the Free Software Foundation with the addition of the 011 * following permission added to Section 15 as permitted in Section 7(a): 012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 014 * 015 * This program is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 017 * or FITNESS FOR A PARTICULAR PURPOSE. 018 * See the GNU Affero General Public License for more details. 019 * You should have received a copy of the GNU Affero General Public License 020 * along with this program; if not, see http://www.gnu.org/licenses or write to 021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 022 * Boston, MA, 02110-1301 USA, or download the license from the following URL: 023 * http://itextpdf.com/terms-of-use/ 024 * 025 * The interactive user interfaces in modified source and object code versions 026 * of this program must display Appropriate Legal Notices, as required under 027 * Section 5 of the GNU Affero General Public License. 028 * 029 * In accordance with Section 7(b) of the GNU Affero General Public License, 030 * a covered work must retain the producer line in every PDF that is created 031 * or manipulated using iText. 032 * 033 * You can be released from the requirements of the license by purchasing 034 * a commercial license. Buying such a license is mandatory as soon as you 035 * develop commercial activities involving the iText software without 036 * disclosing the source code of your own applications. 037 * These activities include: offering paid services to customers as an ASP, 038 * serving PDFs on the fly in a web application, shipping iText with a closed 039 * source product. 040 * 041 * For more information, please contact iText Software Corp. at this 042 * address: sales@itextpdf.com 043 */ 044 package com.itextpdf.text.pdf; 045 046import java.io.IOException; 047import java.io.PrintStream; 048import java.util.Iterator; 049/** 050 * List a PDF file in human-readable form (for debugging reasons mostly) 051 * @author Mark Thompson 052 */ 053 054public class PdfLister { 055 056 /** the printStream you want to write the output to. */ 057 PrintStream out; 058 059 /** 060 * Create a new lister object. 061 * @param out 062 */ 063 public PdfLister(PrintStream out) { 064 this.out = out; 065 } 066 067 /** 068 * Visualizes a PDF object. 069 * @param object a com.itextpdf.text.pdf object 070 */ 071 public void listAnyObject(PdfObject object) 072 { 073 switch (object.type()) { 074 case PdfObject.ARRAY: 075 listArray((PdfArray)object); 076 break; 077 case PdfObject.DICTIONARY: 078 listDict((PdfDictionary) object); 079 break; 080 case PdfObject.STRING: 081 out.println("(" + object.toString() + ")"); 082 break; 083 default: 084 out.println(object.toString()); 085 break; 086 } 087 } 088 /** 089 * Visualizes a PdfDictionary object. 090 * @param dictionary a com.itextpdf.text.pdf.PdfDictionary object 091 */ 092 public void listDict(PdfDictionary dictionary) 093 { 094 out.println("<<"); 095 PdfObject value; 096 for (PdfName key: dictionary.getKeys()) { 097 value = dictionary.get(key); 098 out.print(key.toString()); 099 out.print(' '); 100 listAnyObject(value); 101 } 102 out.println(">>"); 103 } 104 105 /** 106 * Visualizes a PdfArray object. 107 * @param array a com.itextpdf.text.pdf.PdfArray object 108 */ 109 public void listArray(PdfArray array) 110 { 111 out.println('['); 112 for (Iterator<PdfObject> i = array.listIterator(); i.hasNext(); ) { 113 PdfObject item = i.next(); 114 listAnyObject(item); 115 } 116 out.println(']'); 117 } 118 /** 119 * Visualizes a Stream. 120 * @param stream 121 * @param reader 122 */ 123 public void listStream(PRStream stream, PdfReaderInstance reader) 124 { 125 try { 126 listDict(stream); 127 out.println("startstream"); 128 byte[] b = PdfReader.getStreamBytes(stream); 129// byte buf[] = new byte[Math.min(stream.getLength(), 4096)]; 130// int r = 0; 131// stream.openStream(reader); 132// for (;;) { 133// r = stream.readStream(buf, 0, buf.length); 134// if (r == 0) break; 135// out.write(buf, 0, r); 136// } 137// stream.closeStream(); 138 int len = b.length - 1; 139 for (int k = 0; k < len; ++k) { 140 if (b[k] == '\r' && b[k + 1] != '\n') 141 b[k] = (byte)'\n'; 142 } 143 out.println(new String(b)); 144 out.println("endstream"); 145 } catch (IOException e) { 146 System.err.println("I/O exception: " + e); 147// } catch (java.util.zip.DataFormatException e) { 148// System.err.println("Data Format Exception: " + e); 149 } 150 } 151 /** 152 * Visualizes an imported page 153 * @param iPage 154 */ 155 public void listPage(PdfImportedPage iPage) 156 { 157 int pageNum = iPage.getPageNumber(); 158 PdfReaderInstance readerInst = iPage.getPdfReaderInstance(); 159 PdfReader reader = readerInst.getReader(); 160 161 PdfDictionary page = reader.getPageN(pageNum); 162 listDict(page); 163 PdfObject obj = PdfReader.getPdfObject(page.get(PdfName.CONTENTS)); 164 if (obj == null) 165 return; 166 switch (obj.type) { 167 case PdfObject.STREAM: 168 listStream((PRStream)obj, readerInst); 169 break; 170 case PdfObject.ARRAY: 171 for (Iterator<PdfObject> i = ((PdfArray)obj).listIterator(); i.hasNext();) { 172 PdfObject o = PdfReader.getPdfObject(i.next()); 173 listStream((PRStream)o, readerInst); 174 out.println("-----------"); 175 } 176 break; 177 } 178 } 179}