Esempio n. 1
0
        public override void ToPdf(PdfWriter writer, Stream os)
        {
            if (inputStream != null && compressed)
                Put(PdfName.FILTER, PdfName.FLATEDECODE);
            PdfEncryption crypto = null;
            if (writer != null)
                crypto = writer.Encryption;
            if (crypto != null) {
                PdfObject filter = Get(PdfName.FILTER);
                if (filter != null) {
                    if (PdfName.CRYPT.Equals(filter))
                        crypto = null;
                    else if (filter.IsArray()) {
                        PdfArray a = ((PdfArray)filter);
                        if (a.Size > 0 && PdfName.CRYPT.Equals(a[0]))
                            crypto = null;
                    }
                }
            }
            PdfObject nn = Get(PdfName.LENGTH);
            if (crypto != null && nn != null && nn.IsNumber()) {
                int sz = ((PdfNumber)nn).IntValue;
                Put(PdfName.LENGTH, new PdfNumber(crypto.CalculateStreamSize(sz)));
                SuperToPdf(writer, os);
                Put(PdfName.LENGTH, nn);
            }
            else
                SuperToPdf(writer, os);
            os.Write(STARTSTREAM, 0, STARTSTREAM.Length);
            if (inputStream != null) {
                rawLength = 0;
                ZDeflaterOutputStream def = null;
                OutputStreamCounter osc = new OutputStreamCounter(os);
                OutputStreamEncryption ose = null;
                Stream fout = osc;
                if (crypto != null && !crypto.IsEmbeddedFilesOnly())
                    fout = ose = crypto.GetEncryptionStream(fout);
                if (compressed)
                    fout = def = new ZDeflaterOutputStream(fout, compressionLevel);

                byte[] buf = new byte[4192];
                while (true) {
                    int n = inputStream.Read(buf, 0, buf.Length);
                    if (n <= 0)
                        break;
                    fout.Write(buf, 0, n);
                    rawLength += n;
                }
                if (def != null)
                    def.Finish();
                if (ose != null)
                    ose.Finish();
                inputStreamLength = osc.Counter;
            }
            else {
                if (crypto != null && !crypto.IsEmbeddedFilesOnly()) {
                    byte[] b;
                    if (streamBytes != null) {
                        b = crypto.EncryptByteArray(streamBytes.ToArray());
                    }
                    else {
                        b = crypto.EncryptByteArray(bytes);
                    }
                    os.Write(b, 0, b.Length);
                }
                else {
                    if (streamBytes != null)
                        streamBytes.WriteTo(os);
                    else
                        os.Write(bytes, 0, bytes.Length);
                }
            }
            os.Write(ENDSTREAM, 0, ENDSTREAM.Length);
        }
Esempio n. 2
0
        public override void ToPdf(PdfWriter writer, Stream os)
        {
            if (inputStream != null && compressed)
            {
                Put(PdfName.FILTER, PdfName.FLATEDECODE);
            }
            PdfEncryption crypto = null;

            if (writer != null)
            {
                crypto = writer.Encryption;
            }
            if (crypto != null)
            {
                PdfObject filter = Get(PdfName.FILTER);
                if (filter != null)
                {
                    if (PdfName.CRYPT.Equals(filter))
                    {
                        crypto = null;
                    }
                    else if (filter.IsArray())
                    {
                        PdfArray a = ((PdfArray)filter);
                        if (a.Size > 0 && PdfName.CRYPT.Equals(a[0]))
                        {
                            crypto = null;
                        }
                    }
                }
            }
            PdfObject nn = Get(PdfName.LENGTH);

            if (crypto != null && nn != null && nn.IsNumber())
            {
                int sz = ((PdfNumber)nn).IntValue;
                Put(PdfName.LENGTH, new PdfNumber(crypto.CalculateStreamSize(sz)));
                SuperToPdf(writer, os);
                Put(PdfName.LENGTH, nn);
            }
            else
            {
                SuperToPdf(writer, os);
            }
            os.Write(STARTSTREAM, 0, STARTSTREAM.Length);
            if (inputStream != null)
            {
                rawLength = 0;
                ZDeflaterOutputStream  def = null;
                OutputStreamCounter    osc = new OutputStreamCounter(os);
                OutputStreamEncryption ose = null;
                Stream fout = osc;
                if (crypto != null && !crypto.IsEmbeddedFilesOnly())
                {
                    fout = ose = crypto.GetEncryptionStream(fout);
                }
                if (compressed)
                {
                    fout = def = new ZDeflaterOutputStream(fout, compressionLevel);
                }

                byte[] buf = new byte[4192];
                while (true)
                {
                    int n = inputStream.Read(buf, 0, buf.Length);
                    if (n <= 0)
                    {
                        break;
                    }
                    fout.Write(buf, 0, n);
                    rawLength += n;
                }
                if (def != null)
                {
                    def.Finish();
                }
                if (ose != null)
                {
                    ose.Finish();
                }
                inputStreamLength = osc.Counter;
            }
            else
            {
                if (crypto != null && !crypto.IsEmbeddedFilesOnly())
                {
                    byte[] b;
                    if (streamBytes != null)
                    {
                        b = crypto.EncryptByteArray(streamBytes.ToArray());
                    }
                    else
                    {
                        b = crypto.EncryptByteArray(bytes);
                    }
                    os.Write(b, 0, b.Length);
                }
                else
                {
                    if (streamBytes != null)
                    {
                        streamBytes.WriteTo(os);
                    }
                    else
                    {
                        os.Write(bytes, 0, bytes.Length);
                    }
                }
            }
            os.Write(ENDSTREAM, 0, ENDSTREAM.Length);
        }
Esempio n. 3
0
 /**
 * Writes the header to the OutputStreamCounter.
 * @throws IOException
 */
 public void WriteHeader(OutputStreamCounter os)
 {
     if (appendmode) {
         os.Write(HEADER[0], 0, HEADER[0].Length);
     }
     else {
         os.Write(HEADER[1], 0, HEADER[1].Length);
         os.Write(GetVersionAsByteArray(header_version), 0, GetVersionAsByteArray(header_version).Length);
         os.Write(HEADER[2], 0, HEADER[2].Length);
         headerWasWritten = true;
     }
 }
Esempio n. 4
0
 /// <summary>
 /// Constructs a DocWriter.
 /// </summary>
 /// <param name="document">The Document that has to be written</param>
 /// <param name="os">The Stream the writer has to write to.</param>
 protected DocWriter(Document document, Stream os)
 {
     this.document = document;
     this.os = new OutputStreamCounter(os);
 }