1 31 package org.pdfbox.pdfwriter; 32 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 36 import java.util.Iterator ; 37 import java.util.List ; 38 39 import org.pdfbox.cos.COSArray; 40 import org.pdfbox.cos.COSBase; 41 import org.pdfbox.cos.COSBoolean; 42 import org.pdfbox.cos.COSDictionary; 43 import org.pdfbox.cos.COSFloat; 44 import org.pdfbox.cos.COSInteger; 45 import org.pdfbox.cos.COSName; 46 import org.pdfbox.cos.COSString; 47 48 import org.pdfbox.util.ImageParameters; 49 import org.pdfbox.util.PDFOperator; 50 51 57 public class ContentStreamWriter 58 { 59 private OutputStream output; 60 63 public static final byte[] SPACE = new byte[] { 32 }; 64 65 68 public static final byte[] EOL = System.getProperty("line.separator").getBytes(); 69 70 75 public ContentStreamWriter( OutputStream out ) 76 { 77 output = out; 78 } 79 80 88 public void writeTokens( List tokens, int start, int end ) throws IOException 89 { 90 for( int i=start; i<end; i++ ) 91 { 92 Object o = tokens.get( i ); 93 writeObject( o ); 94 output.write( 32 ); 96 } 97 output.flush(); 98 } 99 100 private void writeObject( Object o ) throws IOException 101 { 102 if( o instanceof COSString ) 103 { 104 ((COSString)o).writePDF( output ); 105 } 106 else if( o instanceof COSFloat ) 107 { 108 ((COSFloat)o).writePDF( output ); 109 } 110 else if( o instanceof COSInteger ) 111 { 112 ((COSInteger)o).writePDF( output ); 113 } 114 else if( o instanceof COSBoolean ) 115 { 116 ((COSBoolean)o).writePDF( output ); 117 } 118 else if( o instanceof COSName ) 119 { 120 ((COSName)o).writePDF( output ); 121 } 122 else if( o instanceof COSArray ) 123 { 124 COSArray array = (COSArray)o; 125 output.write(COSWriter.ARRAY_OPEN); 126 for( int i=0; i<array.size(); i++ ) 127 { 128 writeObject( array.get( i ) ); 129 output.write( SPACE ); 130 } 131 132 output.write( COSWriter.ARRAY_CLOSE ); 133 } 134 else if( o instanceof COSDictionary ) 135 { 136 COSDictionary obj = (COSDictionary)o; 137 output.write( COSWriter.DICT_OPEN ); 138 for (Iterator i = obj.keyList().iterator(); i.hasNext();) 139 { 140 COSName name = (COSName) i.next(); 141 COSBase value = obj.getItem(name); 142 if (value != null) 143 { 144 writeObject( name ); 145 output.write( SPACE ); 146 147 writeObject( value ); 148 149 output.write( SPACE ); 150 151 } 152 } 153 output.write( COSWriter.DICT_CLOSE ); 154 output.write( SPACE ); 155 } 156 else if( o instanceof PDFOperator ) 157 { 158 PDFOperator op = (PDFOperator)o; 159 if( op.getOperation().equals( "BI" ) ) 160 { 161 output.write( "BI".getBytes() ); 162 ImageParameters params = op.getImageParameters(); 163 COSDictionary dic = params.getDictionary(); 164 Iterator iter = dic.keyList().iterator(); 165 while( iter.hasNext() ) 166 { 167 COSName key = (COSName)iter.next(); 168 Object value = dic.getDictionaryObject( key ); 169 key.writePDF( output ); 170 output.write( SPACE ); 171 writeObject( value ); 172 output.write( EOL ); 173 } 174 output.write( "ID".getBytes() ); 175 output.write( EOL ); 176 output.write( op.getImageData() ); 177 } 178 else 179 { 180 output.write( op.getOperation().getBytes() ); 181 output.write( EOL ); 182 } 183 } 184 else 185 { 186 throw new IOException ( "Error:Unknown type in content stream:" + o ); 187 } 188 } 189 190 196 public void writeTokens( List tokens ) throws IOException 197 { 198 writeTokens( tokens, 0, tokens.size() ); 199 } 200 } 201 | Popular Tags |