Exemplo n.º 1
0
        /**
         * <summary>Adds the <see cref="DataObject">data object</see> to the specified object stream
         * [PDF:1.6:3.4.6].</summary>
         * <param name="objectStreamIndirectObject">Target object stream.</param>
         */
        public void Compress(
            PdfIndirectObject objectStreamIndirectObject
            )
        {
            if (objectStreamIndirectObject == null)
            {
                Uncompress();
            }
            else
            {
                PdfDataObject objectStreamDataObject = objectStreamIndirectObject.DataObject;
                if (!(objectStreamDataObject is ObjectStream))
                {
                    throw new ArgumentException("MUST contain an ObjectStream instance.", "objectStreamIndirectObject");
                }

                // Ensure removal from previous object stream!
                Uncompress();

                // Add to the object stream!
                ObjectStream objectStream = (ObjectStream)objectStreamDataObject;
                objectStream[xrefEntry.Number] = DataObject;
                // Update its xref entry!
                xrefEntry.Usage        = XRefEntry.UsageEnum.InUseCompressed;
                xrefEntry.StreamNumber = objectStreamIndirectObject.Reference.ObjectNumber;
                xrefEntry.Offset       = -1; // Internal object index unknown (to set on object stream serialization -- see ObjectStream).
            }
        }
Exemplo n.º 2
0
 public PdfObject Visit(
     PdfDataObject obj,
     object data
     )
 {
     return(obj.Accept(this, data));
 }
Exemplo n.º 3
0
 public PdfObject Visit(
     PdfDataObject obj,
     object data
     )
 {
     return obj.Accept(this, data);
 }
Exemplo n.º 4
0
 /**
  * <summary>Gets the stream body.</summary>
  * <param name="decode">Defines whether the body has to be decoded.</param>
  */
 public IBuffer GetBody(
     bool decode
     )
 {
     if (!bodyResolved)
     {
         /*
          * NOTE: In case of stream data from external file, a copy to the local buffer has to be done.
          */
         FileSpecification dataFile = DataFile;
         if (dataFile != null)
         {
             Updateable = false;
             body.SetLength(0);
             body.Write(dataFile.GetInputStream());
             body.Dirty = false;
             Updateable = true;
         }
         bodyResolved = true;
     }
     if (decode)
     {
         PdfDataObject filter = Filter;
         if (filter != null) // Stream encoded.
         {
             header.Updateable = false;
             PdfDataObject parameters = Parameters;
             if (filter is PdfName) // Single filter.
             {
                 body.Decode(
                     bytes.filters.Filter.Get((PdfName)filter),
                     (PdfDictionary)parameters
                     );
             }
             else // Multiple filters.
             {
                 IEnumerator <PdfDirectObject> filterIterator     = ((PdfArray)filter).GetEnumerator();
                 IEnumerator <PdfDirectObject> parametersIterator = (parameters != null ? ((PdfArray)parameters).GetEnumerator() : null);
                 while (filterIterator.MoveNext())
                 {
                     PdfDictionary filterParameters;
                     if (parametersIterator == null)
                     {
                         filterParameters = null;
                     }
                     else
                     {
                         parametersIterator.MoveNext();
                         filterParameters = (PdfDictionary)Resolve(parametersIterator.Current);
                     }
                     body.Decode(bytes.filters.Filter.Get((PdfName)Resolve(filterIterator.Current)), filterParameters);
                 }
             }
             Filter            = null; // The stream is free from encodings.
             header.Updateable = true;
         }
     }
     return(body);
 }
Exemplo n.º 5
0
 /**
  * <summary>Ensures that the specified object is decontextualized from this object.</summary>
  * <param name="obj">Object to decontextualize from this object.</param>
  * <seealso cref="Include(PdfDataObject)"/>
  */
 internal void Exclude(
     PdfDataObject obj
     )
 {
     if (obj != null)
     {
         obj.Parent = null;
     }
 }
Exemplo n.º 6
0
        /**
         * <param name="file">Associated file.</param>
         * <param name="dataObject">
         *  <para>Data object associated to the indirect object. It MUST be</para>
         *  <list type="bullet">
         *    <item><code>null</code>, if the indirect object is original or free.</item>
         *    <item>NOT <code>null</code>, if the indirect object is new and in-use.</item>
         *  </list>
         * </param>
         * <param name="xrefEntry">Cross-reference entry associated to the indirect object. If the
         *  indirect object is new, its offset field MUST be set to 0.</param>
         */
        internal PdfIndirectObject(
            File file,
            PdfDataObject dataObject,
            XRefEntry xrefEntry
            )
        {
            this.file       = file;
            this.dataObject = Include(dataObject);
            this.xrefEntry  = xrefEntry;

            this.original  = (xrefEntry.Offset >= 0);
            this.reference = new PdfReference(this);
        }
Exemplo n.º 7
0
        public override PdfObject Swap(
            PdfObject other
            )
        {
            PdfIndirectObject otherObject     = (PdfIndirectObject)other;
            PdfDataObject     otherDataObject = otherObject.dataObject;

            // Update the other!
            otherObject.DataObject = dataObject;
            // Update this one!
            this.DataObject = otherDataObject;
            return(this);
        }
Exemplo n.º 8
0
        public virtual PdfObject Visit(
            PdfIndirectObject obj,
            object data
            )
        {
            PdfDataObject dataObject = obj.DataObject;

            if (dataObject != null)
            {
                dataObject.Accept(this, data);
            }
            return(obj);
        }
Exemplo n.º 9
0
        /**
          <param name="file">Associated file.</param>
          <param name="dataObject">
        <para>Data object associated to the indirect object. It MUST be</para>
        <list type="bullet">
          <item><code>null</code>, if the indirect object is original or free.</item>
          <item>NOT <code>null</code>, if the indirect object is new and in-use.</item>
        </list>
          </param>
          <param name="xrefEntry">Cross-reference entry associated to the indirect object. If the
        indirect object is new, its offset field MUST be set to 0.</param>
        */
        internal PdfIndirectObject(
      File file,
      PdfDataObject dataObject,
      XRefEntry xrefEntry
      )
        {
            this.file = file;
              this.dataObject = Include(dataObject);
              this.xrefEntry = xrefEntry;

              this.original = (xrefEntry.Offset >= 0);
              this.reference = new PdfReference(this);
        }
Exemplo n.º 10
0
 /**
  * <summary>Ensures that the specified object is contextualized into this object.</summary>
  * <param name="obj">Object to contextualize into this object; if it is already contextualized
  *  into another object, it will be cloned to preserve its previous association.</param>
  * <returns>Contextualized object.</returns>
  * <seealso cref="Exclude(PdfDataObject)"/>
  */
 internal PdfDataObject Include(
     PdfDataObject obj
     )
 {
     if (obj != null)
     {
         if (obj.Parent != null)
         {
             obj = (PdfDataObject)obj.Clone();
         }
         obj.Parent = this;
     }
     return(obj);
 }
Exemplo n.º 11
0
Arquivo: CMap.cs Projeto: n9/pdfclown
        /**
          <summary>Gets the character map extracted from the given encoding object.</summary>
          <param name="encodingObject">Encoding object.</param>
        */
        public static IDictionary<ByteArray, int> Get(
            PdfDataObject encodingObject
            )
        {
            if(encodingObject == null)
            return null;

              if(encodingObject is PdfName) // Predefined CMap.
            return Get((PdfName)encodingObject);
              else if(encodingObject is PdfStream) // Embedded CMap file.
            return Get((PdfStream)encodingObject);
              else
            throw new NotSupportedException("Unknown encoding object type: " + encodingObject.GetType().Name);
        }
Exemplo n.º 12
0
        /**
         * <summary>Gets the stream body.</summary>
         * <param name="decode">Defines whether the body has to be decoded.</param>
         */
        public IBuffer GetBody(
            bool decode
            )
        {
            if (decode)
            {
                // Get 'Filter' entry!

                /*
                 * NOTE: It defines possible encodings applied to the stream.
                 */
                PdfDirectObject filterObj = header[PdfName.Filter];
                if (filterObj != null) // Stream encoded.
                {
                    /*
                     * NOTE: If the stream is encoded, we must decode it before continuing.
                     */
                    PdfDataObject filterDataObj = files.File.Resolve(filterObj);
                    PdfDataObject decodeParms   = header.Resolve(PdfName.DecodeParms);
                    if (filterDataObj is PdfName) // PdfName.
                    {
                        PdfDictionary filterDecodeParms = (PdfDictionary)decodeParms;
                        body.Decode(Filter.Get((PdfName)filterDataObj), filterDecodeParms);
                    }
                    else // MUST be PdfArray.
                    {
                        IEnumerator <PdfDirectObject> filterObjIterator   = ((PdfArray)filterDataObj).GetEnumerator();
                        IEnumerator <PdfDirectObject> decodeParmsIterator = (decodeParms != null ? ((PdfArray)decodeParms).GetEnumerator() : null);
                        while (filterObjIterator.MoveNext())
                        {
                            PdfDictionary filterDecodeParms;
                            if (decodeParmsIterator == null)
                            {
                                filterDecodeParms = null;
                            }
                            else
                            {
                                decodeParmsIterator.MoveNext();
                                filterDecodeParms = (PdfDictionary)files.File.Resolve(decodeParmsIterator.Current);
                            }
                            body.Decode(Filter.Get((PdfName)files.File.Resolve(filterObjIterator.Current)), filterDecodeParms);
                        }
                    }
                    // Update 'Filter' entry!
                    header[PdfName.Filter] = null; // The stream is free from encodings.
                }
            }
            return(body);
        }
Exemplo n.º 13
0
        /**
         * <param name="file">Associated file.</param>
         * <param name="dataObject">
         *  <para>Data object associated to the indirect object.</para>
         *  <list type="bullet">
         *    <item>It MUST be null if the indirect object is original (i.e. coming from an existing file)
         *    or free.</item>
         *    <item>It MUST be NOT null if the indirect object is new and in-use.</item>
         *  </list>
         * </param>
         * <param name="xrefEntry">
         *  <para>Cross-reference entry associated to the indirect object.</para>
         *  <list type="bullet">
         *    <item>If the indirect object is new, its offset field MUST be set to 0 (zero).</item>
         *  </list>
         * </param>
         */
        internal PdfIndirectObject(
            File file,
            PdfDataObject dataObject,
            XRefEntry xrefEntry
            )
        {
            this.file       = file;
            this.dataObject = dataObject;
            this.xrefEntry  = xrefEntry;

            this.original  = (xrefEntry.Offset != 0);
            this.reference = new PdfReference(
                this,
                xrefEntry.Number,
                xrefEntry.Generation
                );
        }
Exemplo n.º 14
0
        /**
         * <summary>Adds an entry into the tree.</summary>
         * <param name="key">New entry's key.</param>
         * <param name="value">New entry's value.</param>
         * <param name="overwrite">Whether the entry is allowed to replace an existing one having the same key.</param>
         */
        private void Add(
            PdfString key,
            TValue value,
            bool overwrite
            )
        {
            // Get the root node!
            PdfReference  rootReference = (PdfReference)BaseObject; // NOTE: Nodes MUST be indirect objects.
            PdfDictionary root          = (PdfDictionary)rootReference.DataObject;

            // Ensuring the root node isn't full...
            {
                PdfName  rootChildrenTypeName;
                int      rootChildrenOrder;
                PdfArray rootChildren = GetChildren(root, out rootChildrenTypeName, out rootChildrenOrder);
                if (rootChildren.Count >= rootChildrenOrder) // Root node full.
                {
                    // Insert the old root under the new one!
                    PdfDataObject oldRootDataObject = rootReference.DataObject;
                    rootReference.DataObject = root = new PdfDictionary(
                        new PdfName[]
                        { PdfName.Kids },
                        new PdfDirectObject[]
                    {
                        new PdfArray(
                            new PdfDirectObject[] { File.Register(oldRootDataObject) }
                            )
                    }
                        );
                    // Split the old root!
                    SplitFullNode(
                        (PdfArray)root[PdfName.Kids],
                        0, // Old root's position within new root's kids.
                        rootChildrenTypeName
                        );
                }
            }

            // Set the entry under the root node!
            Add(
                key,
                value,
                overwrite,
                root
                );
        }
Exemplo n.º 15
0
 public override bool Matches(
     Cloner cloner,
     PdfObject source
     )
 {
     if (source is PdfArray)
     {
         PdfArray array = (PdfArray)source;
         if (array.Count > 0)
         {
             PdfDataObject arrayItem = array.Resolve(0);
             if (arrayItem is PdfDictionary)
             {
                 PdfDictionary arrayItemDictionary = (PdfDictionary)arrayItem;
                 return(arrayItemDictionary.ContainsKey(PdfName.Subtype) &&
                        arrayItemDictionary.ContainsKey(PdfName.Rect));
             }
         }
     }
     return(false);
 }
Exemplo n.º 16
0
   private void FillObjects(
 PdfDataObject objectObject,
 ICollection<PdfObjectWrapper> objects
 )
   {
       PdfDataObject objectDataObject = PdfObject.Resolve(objectObject);
         if(objectDataObject is PdfArray) // Multiple objects.
         {
       foreach(PdfDirectObject itemObject in (PdfArray)objectDataObject)
       {FillObjects(itemObject,objects);}
         }
         else // Single object.
         {
       if(objectDataObject is PdfDictionary) // Annotation.
       {
         objects.Add(
       Annotation.Wrap((PdfReference)objectObject)
       );
       }
       else if(objectDataObject is PdfTextString) // Form field (associated to widget annotations).
       {
         objects.Add(
       Document.Form.Fields[
         (string)((PdfTextString)objectDataObject).Value
         ]
       );
       }
       else // Invalid object type.
       {
         throw new Exception(
       "Invalid 'Hide' action target type (" + objectDataObject.GetType().Name + ").\n"
         + "It should be either an annotation or a form field."
       );
       }
         }
   }
Exemplo n.º 17
0
     public ImplicitContainer(
 File file,
 PdfDataObject dataObject
 )
         : base(file, dataObject, new XRefEntry(int.MinValue, int.MinValue))
     {
     }
Exemplo n.º 18
0
 /**
  * <summary>Ensures a data object to be unresolved into its corresponding indirect reference, if
  * available.</summary>
  * <param name="object">Object to unresolve.</param>
  * <returns><see cref="PdfReference"/>, if available; <code>object</code>, otherwise.</returns>
  */
 public static PdfDirectObject Unresolve(
     PdfDataObject @object
     )
 {
     return(@object == null ? null : @object.Unresolve());
 }
Exemplo n.º 19
0
   /**
     <summary>Registers an <b>internal data object</b>.</summary>
   */
   public PdfReference Register(
 PdfDataObject obj
 )
   {
       return indirectObjects.Add(obj).Reference;
   }
Exemplo n.º 20
0
   /**
     <summary>Gets a pattern's dictionary.</summary>
     <param name="patternDataObject">Pattern data object.</param>
   */
   private static PdfDictionary GetDictionary(
 PdfDataObject patternDataObject
 )
   {
       if(patternDataObject is PdfDictionary)
       return (PdfDictionary)patternDataObject;
         else // MUST be PdfStream.
       return ((PdfStream)patternDataObject).Header;
   }
Exemplo n.º 21
0
   /**
     <summary>Ensures a data object to be unresolved into its corresponding indirect reference, if
     available.</summary>
     <param name="object">Object to unresolve.</param>
     <returns><see cref="PdfReference"/>, if available; <code>object</code>, otherwise.</returns>
   */
   public static PdfDirectObject Unresolve(
 PdfDataObject @object
 )
   {
       return @object == null ? null : @object.Unresolve();
   }
Exemplo n.º 22
0
   /**
     <summary>Ensures that the specified object is contextualized into this object.</summary>
     <param name="obj">Object to contextualize into this object; if it is already contextualized
   into another object, it will be cloned to preserve its previous association.</param>
     <returns>Contextualized object.</returns>
     <seealso cref="Exclude(PdfDataObject)"/>
   */
   internal PdfDataObject Include(
 PdfDataObject obj
 )
   {
       if(obj != null)
         {
       if(obj.Parent != null)
       {obj = (PdfDataObject)obj.Clone();}
       obj.Parent = this;
         }
         return obj;
   }
Exemplo n.º 23
0
   /**
     <summary>Ensures that the specified object is decontextualized from this object.</summary>
     <param name="obj">Object to decontextualize from this object.</param>
     <seealso cref="Include(PdfDataObject)"/>
   */
   internal void Exclude(
 PdfDataObject obj
 )
   {
       if(obj != null)
         {obj.Parent = null;}
   }