Exemplo n.º 1
0
        /// <summary>Flushes all modified objects which have not been flushed yet.</summary>
        /// <remarks>Flushes all modified objects which have not been flushed yet. Used in case incremental updates.</remarks>
        /// <param name="forbiddenToFlush">
        /// a
        /// <see cref="Java.Util.Set{E}"/>
        /// of
        /// <see cref="PdfIndirectReference">references</see>
        /// that are forbidden to be flushed automatically.
        /// </param>
        protected internal virtual void FlushModifiedWaitingObjects(ICollection <PdfIndirectReference> forbiddenToFlush
                                                                    )
        {
            PdfXrefTable xref = document.GetXref();

            for (int i = 1; i < xref.Size(); i++)
            {
                PdfIndirectReference indirectReference = xref.Get(i);
                if (null != indirectReference && !indirectReference.IsFree() && !forbiddenToFlush.Contains(indirectReference
                                                                                                           ))
                {
                    bool isModified = indirectReference.CheckState(PdfObject.MODIFIED);
                    if (isModified)
                    {
                        PdfObject obj = indirectReference.GetRefersTo(false);
                        if (obj != null)
                        {
                            if (!obj.Equals(objectStream))
                            {
                                obj.Flush();
                            }
                        }
                    }
                }
            }
            if (objectStream != null && objectStream.GetSize() > 0)
            {
                objectStream.Flush();
                objectStream = null;
            }
        }
Exemplo n.º 2
0
        /// <summary>Flushes all objects which have not been flushed yet.</summary>
        /// <param name="forbiddenToFlush">
        /// a
        /// <see cref="Java.Util.Set{E}"/>
        /// of
        /// <see cref="PdfIndirectReference">references</see>
        /// that are forbidden to be flushed automatically.
        /// </param>
        protected internal virtual void FlushWaitingObjects(ICollection <PdfIndirectReference> forbiddenToFlush)
        {
            PdfXrefTable xref      = document.GetXref();
            bool         needFlush = true;

            while (needFlush)
            {
                needFlush = false;
                for (int i = 1; i < xref.Size(); i++)
                {
                    PdfIndirectReference indirectReference = xref.Get(i);
                    if (indirectReference != null && !indirectReference.IsFree() && indirectReference.CheckState(PdfObject.MUST_BE_FLUSHED
                                                                                                                 ) && !forbiddenToFlush.Contains(indirectReference))
                    {
                        PdfObject obj = indirectReference.GetRefersTo(false);
                        if (obj != null)
                        {
                            obj.Flush();
                            needFlush = true;
                        }
                    }
                }
            }
            if (objectStream != null && objectStream.GetSize() > 0)
            {
                objectStream.Flush();
                objectStream = null;
            }
        }
Exemplo n.º 3
0
        /// <summary>Flushes the object.</summary>
        /// <remarks>Flushes the object. Override this method if you want to define custom behaviour for object flushing.
        ///     </remarks>
        /// <param name="pdfObject">object to flush.</param>
        /// <param name="canBeInObjStm">indicates whether object can be placed into object stream.</param>
        /// <exception cref="System.IO.IOException">on error.</exception>
        protected internal virtual void FlushObject(PdfObject pdfObject, bool canBeInObjStm)
        {
            PdfIndirectReference indirectReference = pdfObject.GetIndirectReference();

            if (IsFullCompression() && canBeInObjStm)
            {
                PdfObjectStream objectStream = GetObjectStream();
                objectStream.AddObject(pdfObject);
            }
            else
            {
                indirectReference.SetOffset(GetCurrentPos());
                WriteToBody(pdfObject);
            }
            ((PdfIndirectReference)indirectReference.SetState(PdfObject.FLUSHED)).ClearState(PdfObject.MUST_BE_FLUSHED
                                                                                             );
            switch (pdfObject.GetObjectType())
            {
            case PdfObject.BOOLEAN:
            case PdfObject.NAME:
            case PdfObject.NULL:
            case PdfObject.NUMBER:
            case PdfObject.STRING: {
                ((PdfPrimitiveObject)pdfObject).content = null;
                break;
            }

            case PdfObject.ARRAY: {
                PdfArray array = ((PdfArray)pdfObject);
                MarkArrayContentToFlush(array);
                array.ReleaseContent();
                break;
            }

            case PdfObject.STREAM:
            case PdfObject.DICTIONARY: {
                PdfDictionary dictionary = ((PdfDictionary)pdfObject);
                MarkDictionaryContentToFlush(dictionary);
                dictionary.ReleaseContent();
                break;
            }

            case PdfObject.INDIRECT_REFERENCE: {
                MarkObjectToFlush(((PdfIndirectReference)pdfObject).GetRefersTo(false));
                break;
            }
            }
        }
Exemplo n.º 4
0
 /// <summary>Gets the current object stream.</summary>
 /// <returns>object stream.</returns>
 /// <exception cref="System.IO.IOException"/>
 internal virtual PdfObjectStream GetObjectStream()
 {
     if (!IsFullCompression())
     {
         return(null);
     }
     if (objectStream == null)
     {
         objectStream = new PdfObjectStream(document);
     }
     else
     {
         if (objectStream.GetSize() == PdfObjectStream.MAX_OBJ_STREAM_SIZE)
         {
             objectStream.Flush();
             objectStream = new PdfObjectStream(objectStream);
         }
     }
     return(objectStream);
 }
Exemplo n.º 5
0
        /// <summary>Flushes all modified objects which have not been flushed yet.</summary>
        /// <remarks>Flushes all modified objects which have not been flushed yet. Used in case incremental updates.</remarks>
        protected internal virtual void FlushModifiedWaitingObjects()
        {
            PdfXrefTable xref = document.GetXref();

            for (int i = 1; i < xref.Size(); i++)
            {
                PdfIndirectReference indirectReference = xref.Get(i);
                if (null != indirectReference)
                {
                    PdfObject obj = indirectReference.GetRefersTo(false);
                    if (obj != null && !obj.Equals(objectStream) && obj.IsModified())
                    {
                        obj.Flush();
                    }
                }
            }
            if (objectStream != null && objectStream.GetSize() > 0)
            {
                objectStream.Flush();
                objectStream = null;
            }
        }
Exemplo n.º 6
0
        private void Write(PdfStream pdfStream)
        {
            try {
                bool userDefinedCompression = pdfStream.GetCompressionLevel() != CompressionConstants.UNDEFINED_COMPRESSION;
                if (!userDefinedCompression)
                {
                    int defaultCompressionLevel = document != null?document.GetWriter().GetCompressionLevel() : CompressionConstants
                                                      .DEFAULT_COMPRESSION;

                    pdfStream.SetCompressionLevel(defaultCompressionLevel);
                }
                bool toCompress       = pdfStream.GetCompressionLevel() != CompressionConstants.NO_COMPRESSION;
                bool allowCompression = !pdfStream.ContainsKey(PdfName.Filter) && IsNotMetadataPdfStream(pdfStream);
                if (pdfStream.GetInputStream() != null)
                {
                    Stream fout = this;
                    DeflaterOutputStream   def = null;
                    OutputStreamEncryption ose = null;
                    if (crypto != null && !crypto.IsEmbeddedFilesOnly())
                    {
                        fout = ose = crypto.GetEncryptionStream(fout);
                    }
                    if (toCompress && (allowCompression || userDefinedCompression))
                    {
                        UpdateCompressionFilter(pdfStream);
                        fout = def = new DeflaterOutputStream(fout, pdfStream.GetCompressionLevel(), 0x8000);
                    }
                    this.Write((PdfDictionary)pdfStream);
                    WriteBytes(iText.Kernel.Pdf.PdfOutputStream.stream);
                    long   beginStreamContent = GetCurrentPos();
                    byte[] buf = new byte[4192];
                    while (true)
                    {
                        int n = pdfStream.GetInputStream().Read(buf);
                        if (n <= 0)
                        {
                            break;
                        }
                        fout.Write(buf, 0, n);
                    }
                    if (def != null)
                    {
                        def.Finish();
                    }
                    if (ose != null)
                    {
                        ose.Finish();
                    }
                    PdfNumber length = pdfStream.GetAsNumber(PdfName.Length);
                    length.SetValue((int)(GetCurrentPos() - beginStreamContent));
                    pdfStream.UpdateLength(length.IntValue());
                    WriteBytes(iText.Kernel.Pdf.PdfOutputStream.endstream);
                }
                else
                {
                    //When document is opened in stamping mode the output stream can be uninitialized.
                    //We have to initialize it and write all data from streams input to streams output.
                    if (pdfStream.GetOutputStream() == null && pdfStream.GetIndirectReference().GetReader() != null)
                    {
                        // If new specific compression is set for stream,
                        // then compressed stream should be decoded and written with new compression settings
                        byte[] bytes = pdfStream.GetIndirectReference().GetReader().ReadStreamBytes(pdfStream, false);
                        if (userDefinedCompression)
                        {
                            bytes = DecodeFlateBytes(pdfStream, bytes);
                        }
                        pdfStream.InitOutputStream(new ByteArrayOutputStream(bytes.Length));
                        pdfStream.GetOutputStream().Write(bytes);
                    }
                    System.Diagnostics.Debug.Assert(pdfStream.GetOutputStream() != null, "PdfStream lost OutputStream");
                    ByteArrayOutputStream byteArrayStream;
                    try {
                        if (toCompress && !ContainsFlateFilter(pdfStream) && (allowCompression || userDefinedCompression))
                        {
                            // compress
                            UpdateCompressionFilter(pdfStream);
                            byteArrayStream = new ByteArrayOutputStream();
                            DeflaterOutputStream zip = new DeflaterOutputStream(byteArrayStream, pdfStream.GetCompressionLevel());
                            if (pdfStream is PdfObjectStream)
                            {
                                PdfObjectStream objectStream = (PdfObjectStream)pdfStream;
                                ((ByteArrayOutputStream)objectStream.GetIndexStream().GetOutputStream()).WriteTo(zip);
                                ((ByteArrayOutputStream)objectStream.GetOutputStream().GetOutputStream()).WriteTo(zip);
                            }
                            else
                            {
                                System.Diagnostics.Debug.Assert(pdfStream.GetOutputStream() != null, "Error in outputStream");
                                ((ByteArrayOutputStream)pdfStream.GetOutputStream().GetOutputStream()).WriteTo(zip);
                            }
                            zip.Finish();
                        }
                        else
                        {
                            if (pdfStream is PdfObjectStream)
                            {
                                PdfObjectStream objectStream = (PdfObjectStream)pdfStream;
                                byteArrayStream = new ByteArrayOutputStream();
                                ((ByteArrayOutputStream)objectStream.GetIndexStream().GetOutputStream()).WriteTo(byteArrayStream);
                                ((ByteArrayOutputStream)objectStream.GetOutputStream().GetOutputStream()).WriteTo(byteArrayStream);
                            }
                            else
                            {
                                System.Diagnostics.Debug.Assert(pdfStream.GetOutputStream() != null, "Error in outputStream");
                                byteArrayStream = (ByteArrayOutputStream)pdfStream.GetOutputStream().GetOutputStream();
                            }
                        }
                        if (CheckEncryption(pdfStream))
                        {
                            ByteArrayOutputStream  encodedStream = new ByteArrayOutputStream();
                            OutputStreamEncryption ose           = crypto.GetEncryptionStream(encodedStream);
                            byteArrayStream.WriteTo(ose);
                            ose.Finish();
                            byteArrayStream = encodedStream;
                        }
                    }
                    catch (System.IO.IOException ioe) {
                        throw new PdfException(PdfException.IoException, ioe);
                    }
                    pdfStream.Put(PdfName.Length, new PdfNumber(byteArrayStream.Length));
                    pdfStream.UpdateLength((int)byteArrayStream.Length);
                    this.Write((PdfDictionary)pdfStream);
                    WriteBytes(iText.Kernel.Pdf.PdfOutputStream.stream);
                    byteArrayStream.WriteTo(this);
                    byteArrayStream.Dispose();
                    WriteBytes(iText.Kernel.Pdf.PdfOutputStream.endstream);
                }
            }
            catch (System.IO.IOException e) {
                throw new PdfException(PdfException.CannotWriteToPdfStream, e, pdfStream);
            }
        }