001/* 002 * $Id: ContentByteUtils.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: Kevin Day, 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 */ 044package com.itextpdf.text.pdf.parser; 045 046import java.io.ByteArrayOutputStream; 047import java.io.IOException; 048import java.util.ListIterator; 049 050import com.itextpdf.text.pdf.PRIndirectReference; 051import com.itextpdf.text.pdf.PRStream; 052import com.itextpdf.text.pdf.PdfArray; 053import com.itextpdf.text.pdf.PdfDictionary; 054import com.itextpdf.text.pdf.PdfName; 055import com.itextpdf.text.pdf.PdfObject; 056import com.itextpdf.text.pdf.PdfReader; 057 058/** 059 * @author kevin 060 * @since 5.0.1 061 */ 062public class ContentByteUtils { 063 private ContentByteUtils() { 064 // TODO Auto-generated constructor stub 065 } 066 067 /** 068 * Gets the content bytes from a content object, which may be a reference 069 * a stream or an array. 070 * @param contentObject the object to read bytes from 071 * @return the content bytes 072 * @throws IOException 073 */ 074 public static byte[] getContentBytesFromContentObject(final PdfObject contentObject) throws IOException { 075 final byte[] result; 076 switch (contentObject.type()) 077 { 078 case PdfObject.INDIRECT: 079 final PRIndirectReference ref = (PRIndirectReference) contentObject; 080 final PdfObject directObject = PdfReader.getPdfObject(ref); 081 result = getContentBytesFromContentObject(directObject); 082 break; 083 case PdfObject.STREAM: 084 final PRStream stream = (PRStream) PdfReader.getPdfObject(contentObject); 085 result = PdfReader.getStreamBytes(stream); 086 break; 087 case PdfObject.ARRAY: 088 // Stitch together all content before calling processContent(), because 089 // processContent() resets state. 090 final ByteArrayOutputStream allBytes = new ByteArrayOutputStream(); 091 final PdfArray contentArray = (PdfArray) contentObject; 092 final ListIterator<PdfObject> iter = contentArray.listIterator(); 093 while (iter.hasNext()) 094 { 095 final PdfObject element = iter.next(); 096 allBytes.write(getContentBytesFromContentObject(element)); 097 allBytes.write((byte)' '); 098 } 099 result = allBytes.toByteArray(); 100 break; 101 default: 102 final String msg = "Unable to handle Content of type " + contentObject.getClass(); 103 throw new IllegalStateException(msg); 104 } 105 return result; 106 } 107 108 /** 109 * Gets the content bytes of a page from a reader 110 * @param reader the reader to get content bytes from 111 * @param pageNum the page number of page you want get the content stream from 112 * @return a byte array with the effective content stream of a page 113 * @throws IOException 114 * @since 5.0.1 115 */ 116 public static byte[] getContentBytesForPage(PdfReader reader, int pageNum) throws IOException { 117 final PdfDictionary pageDictionary = reader.getPageN(pageNum); 118 final PdfObject contentObject = pageDictionary.get(PdfName.CONTENTS); 119 if (contentObject == null) 120 return new byte[0]; 121 122 final byte[] contentBytes = ContentByteUtils.getContentBytesFromContentObject(contentObject); 123 return contentBytes; 124 } 125 126}