You already have that in PdfReader.getStreamBytes().
 
Best Regards,
Paulo Soares


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Perez Carmona, David
Sent: Wednesday, August 25, 2004 12:24 PM
To: Itext-Questions (E-mail)
Subject: [iText-questions] PRStream.getData() contribution

Hi all,
 
I would like to contribute with a new method I�ve added to the class PRStream, it allows to get the bytes of a stream (decrypted and deflated if necessary).
 
Here is its code:
 
    /**Get the data associated with the stream, deflated and decrypted if necessary.*/
    public byte[] getData() throws IOException, PdfException {
     if (length == 0) {
      return new byte[0];
     }
     ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
        if (length > 0) {
            if (offset < 0) {
             bos.write(bytes);
            } else {
                byte buf[] = new byte[Math.min(length, 4092)];
                RandomAccessFileOrArray file = reader.getSafeFile();
                file.seek(offset);
                int size = length;
               
                PdfEncryption decrypt = reader.getDecrypt();
                if (decrypt != null) {
                    decrypt.setHashKey(objNum, objGen);
                    decrypt.prepareKey();
                }
                while (size > 0) {
                    int r = file.read(buf, 0, Math.min(size, buf.length));
                    size -= r;
                   
                    if (decrypt != null)
                        decrypt.encryptRC4(buf, 0, r); //added by ujihara for decryption
                    bos.write(buf, 0, r);
                }
            }
        }
        boolean compressed = false;
        PdfObject filter = get(PdfName.FILTER);
        if (filter != null) {
            if (filter.isName() && ((PdfName) filter).compareTo(PdfName.FLATEDECODE) == 0) {
             compressed = true;
            }
            else if (filter.isArray() && ((PdfArray) filter).contains(PdfName.FLATEDECODE)) {
             compressed = true;
            }
            else {
                throw new PdfException("Stream could not be compressed: filter is not a name or array.");
            }
        }
        if (compressed) {
         InputStream is = new InflaterInputStream(new ByteArrayInputStream(bos.toByteArray()));
         bos.reset();
         while (true) {
          int c = is.read();
          if (c < 0) {
           break;
          }
          bos.write(c);
         }
        }
     return bos.toByteArray();
    }
************************************************************* Este correo ha sido procesado por el Antivirus del Grupo FCC *************************************************************

Reply via email to