//    internal virtual void UpdateDocument()
//    {
//    }
//
//    internal virtual void SetObject(PdfItem value)
//    {
//    }

    public void ActivatePage(PdfObject obj)
    {
      if (this.currentPage != null)
      {
        this.Controls.Remove(this.currentPage);
        this.currentPage = null;
      }

      if (obj is PdfDictionary)
      {
        if (this.dictPage == null)
          this.dictPage = new DictionaryPage(this.explorer);
        this.currentPage = this.dictPage;
        this.currentPage.SetObject(obj);
      }
      else if (obj is PdfArray)
      {
        if (this.arrayPage == null)
          this.arrayPage = new ArrayPage(this.explorer);
        this.currentPage = this.arrayPage;
        this.currentPage.SetObject(obj);
      }
      else
      {
        if (this.simplePage == null)
          this.simplePage = new SimpleObjectPage(this.explorer);
        this.currentPage = this.simplePage;
        this.currentPage.SetObject(obj);
      }
      this.Controls.Add(this.currentPage);
      this.currentPage.Dock = DockStyle.Fill;
      currentPage.UpdateDocument();
    }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance from an existing object. Used for object type transformation.
 /// </summary>
 protected PdfObject(PdfObject obj)
 {
   this.Document = obj.Owner;
   // If the object that was transformed to an instance of a derived class was an indirect object
   // set the value of the reference to this.
   if (obj.iref != null)
     obj.iref.Value = this;
 }
Exemplo n.º 3
0
    /// <summary>
    /// Adds a PdfObject to the table.
    /// </summary>
    public void Add(PdfObject value)
    {
      if (value.Owner == null)
        value.Document = this.document;
      else
        Debug.Assert(value.Owner == this.document);

      if (value.ObjectID.IsEmpty)
        value.SetObjectID(GetNewObjectNumber(), 0);

      if (this.objectTable.ContainsKey(value.ObjectID))
        throw new InvalidOperationException("Object already in table.");

      this.objectTable.Add(value.ObjectID, value.Reference);
    }
Exemplo n.º 4
0
    /// <summary>
    /// Initializes a new instance from an existing object. Used for object type transformation.
    /// </summary>
    protected PdfObject(PdfObject obj)
    {
      Document = obj.Owner;
      // If the object that was transformed to an instance of a derived class was an indirect object
      // set the value of the reference to this.
      if (obj.iref != null)
        obj.iref.Value = this;
#if DEBUG_
      else
      {
        // If this occurs it is an internal error
        Debug.Assert(false, "Object type transformation must not be done with direct objects");
      }
#endif
    }
Exemplo n.º 5
0
    void ActivateTab(PdfObject value)
    {
      switch (this.tbcMain.SelectedIndex)
      {
          // Data
        case 0:
          if (this.tbcMain.TabPages[0].Controls[0] is MainObjectsPageBase)
            ((MainObjectsPageBase)this.tbcMain.TabPages[0].Controls[0]).ActivatePage(value);
          break;

          // PDF
        case 1:
          ((PdfTextPage)this.tbcMain.TabPages[1].Controls[0]).SetObject(value);
          break;
      }
    }
Exemplo n.º 6
0
    /// <summary>
    /// Calculates the transitive closure of the specified PdfObject with the specified depth, i.e. all indirect objects
    /// recursively reachable from the specified object in up to maximally depth steps.
    /// </summary>
    public PdfReference[] TransitiveClosure(PdfObject pdfObject, int depth)
    {
      CheckConsistence();
      Dictionary<PdfItem, object> objects = new Dictionary<PdfItem, object>();
      this.overflow = new Dictionary<PdfItem, object>();
      TransitiveClosureImplementation(objects, pdfObject, ref depth);
    TryAgain:
      if (this.overflow.Count > 0)
      {
        PdfObject[] array = new PdfObject[this.overflow.Count];
        this.overflow.Keys.CopyTo(array, 0);
        this.overflow = new Dictionary<PdfItem, object>();
        for (int idx = 0; idx < array.Length; idx++)
        {
          //PdfObject o = array[idx];
          //o.GetType();
          PdfObject obj = array[idx];
          //if (!objects.Contains(obj))
          //  objects.Add(obj, null);
          TransitiveClosureImplementation(objects, obj, ref depth);
        }
        goto TryAgain;
      }

      CheckConsistence();

      ICollection collection = objects.Keys;
      int count = collection.Count;
      PdfReference[] irefs = new PdfReference[count];
      collection.CopyTo(irefs, 0);

#if true_
      for (int i = 0; i < count; i++)
        for (int j = 0; j < count; j++)
          if (i != j)
          {
            Debug.Assert(Object.ReferenceEquals(irefs[i].Document, this.document));
            Debug.Assert(irefs[i] != irefs[j]);
            Debug.Assert(!Object.ReferenceEquals(irefs[i], irefs[j]));
            Debug.Assert(!Object.ReferenceEquals(irefs[i].Value, irefs[j].Value));
            Debug.Assert(!Object.ReferenceEquals(irefs[i].ObjectID, irefs[j].Value.ObjectID));
            Debug.Assert(irefs[i].ObjectNumber != irefs[j].Value.ObjectNumber);
            Debug.Assert(Object.ReferenceEquals(irefs[i].Document, irefs[j].Document));
            GetType();
          }
#endif
      return irefs;
    }
Exemplo n.º 7
0
    public static string GetTypeName(PdfObject obj)
    {
      if (obj is PdfDictionary)
        return "dictionary";
      if (obj is PdfArray)
        return "array";
      if (obj is PdfBooleanObject)
        return "boolean";
      if (obj is PdfIntegerObject)
        return "integer";
      if (obj is PdfRealObject)
        return "real";
      if (obj is PdfStringObject)
        return "string";
      if (obj is PdfNameObject)
        return "name";
      if (obj is PdfNullObject)
        return "null";

      throw new NotImplementedException("TODO: " + obj.GetType().FullName);
    }
Exemplo n.º 8
0
    //protected Symbol ScanNextToken(out string token, bool testReference)
    //{
    //  Symbol symbol = this.lexer.ScanNextToken(testReference);
    //  token = this.lexer.Token;
    //  return symbol;
    //}

    //    internal object ReadObject(int position)
    //    {
    //      this.lexer.Position = position;
    //      return ReadObject(false);
    //    }
    //
    //    internal virtual object ReadObject(bool directObject)
    //    {
    //      throw new InvalidOperationException("PdfParser.ReadObject() base class called");
    //    }

    /// <summary>
    /// Reads the object ID and the generation and sets it into the specified object.
    /// </summary>
    void ReadObjectID(PdfObject obj)
    {
      int objectNubmer = ReadInteger();
      int generationNumber = ReadInteger();
      ReadSymbol(Symbol.Obj);
      if (obj != null)
        obj.SetObjectID(objectNubmer, generationNumber);
    }
    /// <summary>
    /// Encrypts an indirect object.
    /// </summary>
    internal void EncryptObject(PdfObject value)
    {
      Debug.Assert(value.Reference != null);

      SetHashKey(value.ObjectID);
#if DEBUG
      if (value.ObjectID.ObjectNumber == 10)
        GetType();
#endif

      PdfDictionary dict;
      PdfArray array;
      PdfStringObject str;
      if ((dict = value as PdfDictionary) != null)
        EncryptDictionary(dict);
      else if ((array = value as PdfArray) != null)
        EncryptArray(array);
      else if ((str = value as PdfStringObject) != null)
      {
        if (str.Length != 0)
        {
          byte[] bytes = str.EncryptionValue;
          PrepareKey();
          EncryptRC4(bytes);
          str.EncryptionValue = bytes;
        }
      }
    }
    PdfShading BuildShading2(RadialGradientBrush brush, double scaleX, double scaleY, int repeatcount, PdfObject function)
    {
      // Setup shading
      //<<
      //  /AntiAlias false
      //  /ColorSpace 12 0 R
      //  /Coords [120 120 120 120 120 0]
      //  /Domain [0 1]
      //  /Extend [false false]
      //  /Function 14 0 R
      //  /ShadingType 3
      //>>
      PdfShading shading = Context.PdfDocument.Internals.CreateIndirectObject<PdfShading>();

      shading.Elements.SetInteger(PdfShading.Keys.ShadingType, 3); // Radial shading
      PdfColorMode colorMode = PdfColorMode.Rgb; //this.document.Options.ColorMode;
      if (colorMode != PdfColorMode.Cmyk)
        shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceRGB");
      else
        shading.Elements[PdfShading.Keys.ColorSpace] = new PdfName("/DeviceCMYK");

#if DEBUG_
      if (DevHelper.RenderComments)
        function.Elements.SetString("/@comment", "This is the shading function of a RadialGradientBrushPattern");
#endif
      shading.Elements[PdfShading.Keys.Function] = function;
      shading.Elements[PdfShading.Keys.AntiAlias] = new PdfLiteral("false"); // redundant
      shading.Elements[PdfShading.Keys.Extend] = new PdfLiteral("[false false]");

      double r = Math.Max(brush.RadiusX, brush.RadiusY);
      double x0 = brush.Center.X / scaleX;
      double y0 = brush.Center.Y / scaleY;
      double r0 = r * (repeatcount + 1);
      double x1 = brush.GradientOrigin.X / scaleX;
      double y1 = brush.GradientOrigin.Y / scaleY;
      double r1 = r * repeatcount;

      shading.Elements[PdfShading.Keys.Coords] =
        new PdfLiteral("[{0:0.###} {1:0.###} {2:0.###} {3:0.###} {4:0.###} {5:0.###}]", x0, y0, r0, x1, y1, r1);

      return shading;
    }
Exemplo n.º 11
0
 internal PdfObjectInternals(PdfObject obj)
 {
   this.obj = obj;
 }
Exemplo n.º 12
0
        /// <summary>
        /// Replace all indirect references to external objects by their cloned counterparts
        /// owned by the importer document.
        /// </summary>
        static void FixUpObject(PdfImportedObjectTable iot, PdfDocument owner, PdfObject value)
        {
            Debug.Assert(ReferenceEquals(iot.Owner, owner));

            PdfDictionary dict;
            PdfArray array;
            if ((dict = value as PdfDictionary) != null)
            {
                // Case: The object is a dictionary.
                // Set document for cloned direct objects.
                if (dict.Owner == null)
                {
                    // If the dictionary has not yet an owner set the owner to the importing document.
                    dict.Document = owner;
                }
                else
                {
                    // If the dictionary already has an owner it must be the importing document.
                    Debug.Assert(dict.Owner == owner);
                }

                // Search for indirect references in all dictionary elements.
                PdfName[] names = dict.Elements.KeyNames;
                foreach (PdfName name in names)
                {
                    PdfItem item = dict.Elements[name];
                    Debug.Assert(item != null, "A dictionary element cannot be null.");

                    // Is item an iref?
                    PdfReference iref = item as PdfReference;
                    if (iref != null)
                    {
                        // Case: The item is a reference.
                        // Does the iref already belongs to the new owner?
                        if (iref.Document == owner)
                        {
                            // Yes: fine. Happens when an already cloned object is reused.
                            continue;
                        }

                        //Debug.Assert(iref.Document == iot.Document);
                        // No: Replace with iref of cloned object.
                        PdfReference newXRef = iot[iref.ObjectID];  // TODO: Explain this line of code in all details.
                        Debug.Assert(newXRef != null);
                        Debug.Assert(newXRef.Document == owner);
                        dict.Elements[name] = newXRef;
                    }
                    else
                    {
                        // Case: The item is not a reference.
                        // If item is an object recursively fix its inner items.
                        PdfObject pdfObject = item as PdfObject;
                        if (pdfObject != null)
                        {
                            // Fix up inner objects, i.e. recursively walk down the object tree.
                            FixUpObject(iot, owner, pdfObject);
                        }
                        else
                        {
                            // The item is something else, e.g. a name.
                            // Nothing to do.

                            // ...but let's double check this case in DEBUG build.
                            DebugCheckNonObjects(item);
                        }
                    }
                }
            }
            else if ((array = value as PdfArray) != null)
            {
                // Case: The object is an array.
                // Set document for cloned direct objects.
                if (array.Owner == null)
                {
                    // If the array has not yet an owner set the owner to the importing document.
                    array.Document = owner;
                }
                else
                {
                    // If the array already has an owner it must be the importing document.
                    Debug.Assert(array.Owner == owner);
                }

                // Search for indirect references in all array elements.
                int count = array.Elements.Count;
                for (int idx = 0; idx < count; idx++)
                {
                    PdfItem item = array.Elements[idx];
                    Debug.Assert(item != null, "An array element cannot be null.");

                    // Is item an iref?
                    PdfReference iref = item as PdfReference;
                    if (iref != null)
                    {
                        // Case: The item is a reference.
                        // Does the iref already belongs to the owner?
                        if (iref.Document == owner)
                        {
                            // Yes: fine. Happens when an already cloned object is reused.
                            continue;
                        }

                        // No: replace with iref of cloned object.
                        Debug.Assert(iref.Document == iot.ExternalDocument);
                        PdfReference newXRef = iot[iref.ObjectID];
                        Debug.Assert(newXRef != null);
                        Debug.Assert(newXRef.Document == owner);
                        array.Elements[idx] = newXRef;
                    }
                    else
                    {
                        // Case: The item is not a reference.
                        // If item is an object recursively fix its inner items.
                        PdfObject pdfObject = item as PdfObject;
                        if (pdfObject != null)
                        {
                            // Fix up inner objects, i.e. recursively walk down the object tree.
                            FixUpObject(iot, owner, pdfObject);
                        }
                        else
                        {
                            // The item is something else, e.g. a name.
                            // Nothing to do.

                            // ...but let's double check this case in DEBUG build.
                            DebugCheckNonObjects(item);
                        }
                    }
                }
            }
            else
            {
                // Case: The item is some other indirect object.
                // Indirect integers, booleans, etc. are allowed, but PDFsharp do not create them.
                // If such objects occur in imported PDF files from other producers, nothing more is to do.
                // The owner was already set, which is double checked by the assertions below.
                if (value is PdfNameObject || value is PdfStringObject || value is PdfBooleanObject || value is PdfIntegerObject || value is PdfNumberObject)
                {
                    Debug.Assert(value.IsIndirect);
                    Debug.Assert(value.Owner == owner);
                }
                else
                    Debug.Assert(false, "Should not come here. Object is neither a dictionary nor an array.");
            }
        }
Exemplo n.º 13
0
        void TransitiveClosureImplementation(Dictionary <PdfItem, object> objects, PdfObject pdfObject /*, ref int depth*/)
        {
            try
            {
                _nestingLevel++;
                if (_nestingLevel >= 1000)
                {
                    if (!_overflow.ContainsKey(pdfObject))
                    {
                        _overflow.Add(pdfObject, null);
                    }
                    return;
                }
#if DEBUG_
                //enterCount++;
                if (enterCount == 5400)
                {
                    GetType();
                }
                //if (!Object.ReferenceEquals(pdfObject.Owner, _document))
                //  GetType();
                //////Debug.Assert(Object.ReferenceEquals(pdfObject27.Document, _document));
                //      if (item is PdfObject && ((PdfObject)item).ObjectID.ObjectNumber == 5)
                //        Debug.WriteLine("items: " + ((PdfObject)item).ObjectID.ToString());
                //if (pdfObject.ObjectNumber == 5)
                //  GetType();
#endif

                IEnumerable   enumerable = null; //(IEnumerator)pdfObject;
                PdfDictionary dict;
                PdfArray      array;
                if ((dict = pdfObject as PdfDictionary) != null)
                {
                    enumerable = dict.Elements.Values;
                }
                else if ((array = pdfObject as PdfArray) != null)
                {
                    enumerable = array.Elements;
                }
                else
                {
                    Debug.Assert(false, "Should not come here.");
                }

                if (enumerable != null)
                {
                    foreach (PdfItem item in enumerable)
                    {
                        PdfReference iref = item as PdfReference;
                        if (iref != null)
                        {
                            // Is this an indirect reference to an object that does not exist?
                            //if (iref.Document == null)
                            //{
                            //    Debug.WriteLine("Dead object detected: " + iref.ObjectID.ToString());
                            //    PdfReference dead = DeadObject;
                            //    iref.ObjectID = dead.ObjectID;
                            //    iref.Document = _document;
                            //    iref.SetObject(dead.Value);
                            //    PdfDictionary dict = (PdfDictionary)dead.Value;

                            //    dict.Elements["/DeadObjectCount"] =
                            //      new PdfInteger(dict.Elements.GetInteger("/DeadObjectCount") + 1);

                            //    iref = dead;
                            //}

                            if (!ReferenceEquals(iref.Document, _document))
                            {
                                GetType();
                                Debug.WriteLine(String.Format("Bad iref: {0}", iref.ObjectID.ToString()));
                            }
                            Debug.Assert(ReferenceEquals(iref.Document, _document) || iref.Document == null, "External object detected!");
#if DEBUG_
                            if (iref.ObjectID.ObjectNumber == 23)
                            {
                                GetType();
                            }
#endif
                            if (!objects.ContainsKey(iref))
                            {
                                PdfObject value = iref.Value;

                                // Ignore unreachable objects.
                                if (iref.Document != null)
                                {
                                    // ... from trailer hack
                                    if (value == null)
                                    {
                                        iref = ObjectTable[iref.ObjectID];
                                        Debug.Assert(iref.Value != null);
                                        value = iref.Value;
                                    }
                                    Debug.Assert(ReferenceEquals(iref.Document, _document));
                                    objects.Add(iref, null);
                                    //Debug.WriteLine(String.Format("objects.Add('{0}', null);", iref.ObjectID.ToString()));
                                    if (value is PdfArray || value is PdfDictionary)
                                    {
                                        TransitiveClosureImplementation(objects, value /*, ref depth*/);
                                    }
                                }
                                //else
                                //{
                                //  objects2.Add(this[iref.ObjectID], null);
                                //}
                            }
                        }
                        else
                        {
                            PdfObject pdfObject28 = item as PdfObject;
                            //if (pdfObject28 != null)
                            //  Debug.Assert(Object.ReferenceEquals(pdfObject28.Document, _document));
                            if (pdfObject28 != null && (pdfObject28 is PdfDictionary || pdfObject28 is PdfArray))
                            {
                                TransitiveClosureImplementation(objects, pdfObject28 /*, ref depth*/);
                            }
                        }
                    }
                }
            }
            finally
            {
                _nestingLevel--;
            }
        }
Exemplo n.º 14
0
        ///// <summary>
        ///// The garbage collector for PDF objects.
        ///// </summary>
        //public sealed class GC
        //{
        //  PdfXRefTable xrefTable;
        //
        //  internal GC(PdfXRefTable xrefTable)
        //  {
        //    _xrefTable = xrefTable;
        //  }
        //
        //  public void Collect()
        //  { }
        //
        //  public PdfReference[] ReachableObjects()
        //  {
        //    Hash_table objects = new Hash_table();
        //    TransitiveClosure(objects, _xrefTable.document.trailer);
        //  }

        /// <summary>
        /// Calculates the transitive closure of the specified PdfObject, i.e. all indirect objects
        /// recursively reachable from the specified object.
        /// </summary>
        public PdfReference[] TransitiveClosure(PdfObject pdfObject)
        {
            return(TransitiveClosure(pdfObject, Int16.MaxValue));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Replace all indirect references to external objects by their cloned counterparts
        /// owned by the importer document.
        /// </summary>
        internal static void FixUpObject(PdfImportedObjectTable iot, PdfDocument owner, PdfObject value)
        {
            Debug.Assert(ReferenceEquals(iot.Owner, owner));

            PdfDictionary dict;
            PdfArray      array;

            if ((dict = value as PdfDictionary) != null)
            {
                // Set document for cloned direct objects
                if (dict.Owner == null)
                {
                    dict.Document = owner;
                }
                else
                {
                    Debug.Assert(dict.Owner == owner);
                }

                // Search for indirect references in all keys
                PdfName[] names = dict.Elements.KeyNames;
                foreach (PdfName name in names)
                {
                    PdfItem item = dict.Elements[name];
                    // Is item an iref?
                    PdfReference iref = item as PdfReference;
                    if (iref != null)
                    {
                        // Does the iref already belongs to the owner?
                        if (iref.Document == owner)
                        {
                            // Yes: fine. Happens when an already cloned object is reused.
                            continue;
                        }
                        else
                        {
                            //Debug.Assert(iref.Document == iot.Document);
                            // No: replace with iref of cloned object
                            PdfReference newXRef = iot[iref.ObjectID];
                            Debug.Assert(newXRef != null);
                            Debug.Assert(newXRef.Document == owner);
                            dict.Elements[name] = newXRef;
                        }
                    }
                    else if (item is PdfObject)
                    {
                        // Fix up inner objects
                        FixUpObject(iot, owner, (PdfObject)item);
                    }
                }
            }
            else if ((array = value as PdfArray) != null)
            {
                // Set document for cloned direct objects
                if (array.Owner == null)
                {
                    array.Document = owner;
                }
                else
                {
                    Debug.Assert(array.Owner == owner);
                }

                // Search for indirect references in all array elements
                int count = array.Elements.Count;
                for (int idx = 0; idx < count; idx++)
                {
                    PdfItem item = array.Elements[idx];
                    // Is item an iref?
                    PdfReference iref = item as PdfReference;
                    if (iref != null)
                    {
                        // Does the iref already belongs to the owner?
                        if (iref.Document == owner)
                        {
                            // Yes: fine. Happens when an already cloned object is reused.
                            continue;
                        }
                        else
                        {
                            Debug.Assert(iref.Document == iot.ExternalDocument);
                            // No: replace with iref of cloned object
                            PdfReference newXRef = iot[iref.ObjectID];
                            Debug.Assert(newXRef != null);
                            Debug.Assert(newXRef.Document == owner);
                            array.Elements[idx] = newXRef;
                        }
                    }
                    else if (item is PdfObject)
                    {
                        // Fix up inner objects
                        FixUpObject(iot, owner, (PdfObject)item);
                    }
                }
            }
        }
Exemplo n.º 16
0
 void WriteObjectAddress(PdfObject value)
 {
   if (this.layout == PdfWriterLayout.Verbose)
     this.WriteRaw(String.Format("{0} {1} obj   % {2}\n",
       value.ObjectID.ObjectNumber, value.ObjectID.GenerationNumber,
       value.GetType().FullName));
   else
     this.WriteRaw(String.Format("{0} {1} obj\n", value.ObjectID.ObjectNumber, value.ObjectID.GenerationNumber));
 }
Exemplo n.º 17
0
        ///// <summary>
        ///// Imports an object and its transitive closure to the specified document.
        ///// </summary>
        /// <param name="importedObjectTable">The imported object table of the owner for the external document.</param>
        /// <param name="owner">The document that owns the cloned objects.</param>
        /// <param name="externalObject">The root object to be cloned.</param>
        /// <returns>The clone of the root object</returns>
        internal static PdfObject ImportClosure(PdfImportedObjectTable importedObjectTable, PdfDocument owner, PdfObject externalObject)
        {
            Debug.Assert(ReferenceEquals(importedObjectTable.Owner, owner), "importedObjectTable does not belong to the owner.");
            Debug.Assert(ReferenceEquals(importedObjectTable.ExternalDocument, externalObject.Owner),
                         "The ExternalDocument of the importedObjectTable does not belong to the owner of object to be imported.");

            // Get transitive closure of external object
            PdfObject[] elements = externalObject.Owner.Internals.GetClosure(externalObject);
            int         count    = elements.Length;

#if DEBUG_
            for (int idx = 0; idx < count; idx++)
            {
                Debug.Assert(elements[idx].XRef != null);
                Debug.Assert(elements[idx].XRef.Document != null);
                Debug.Assert(elements[idx].Document != null);
                if (elements[idx].ObjectID.ObjectNumber == 12)
                {
                    GetType();
                }
            }
#endif
            // 1st loop. Already imported objects are reused and new ones are cloned.
            for (int idx = 0; idx < count; idx++)
            {
                PdfObject obj = elements[idx];
                Debug.Assert(!ReferenceEquals(obj.Owner, owner));

                if (importedObjectTable.Contains(obj.ObjectID))
                {
                    // External object was already imported
                    PdfReference iref = importedObjectTable[obj.ObjectID];
                    Debug.Assert(iref != null);
                    Debug.Assert(iref.Value != null);
                    Debug.Assert(iref.Document == owner);
                    // replace external object by the already cloned counterpart
                    elements[idx] = iref.Value;
                }
                else
                {
                    // External object was not imported ealier and must be cloned
                    PdfObject clone = obj.Clone();
                    Debug.Assert(clone.Reference == null);
                    clone.Document = owner;
                    if (obj.Reference != null)
                    {
                        // Case: The cloned object was an indirect object.
                        // add clone to new owner document
                        owner.irefTable.Add(clone);
                        Debug.Assert(clone.Reference != null);
                        // save an association from old object identifier to new iref
                        importedObjectTable.Add(obj.ObjectID, clone.Reference);
                    }
                    else
                    {
                        // Case: The cloned object was a direct object.
                        // only the root object can be a direct object
                        Debug.Assert(idx == 0);
                        //// add it to this (the importer) document
                        //owner.irefTable.Add(clone);
                        //Debug.Assert(clone.Reference != null);
                    }
                    // replace external object by its clone
                    elements[idx] = clone;
                }
            }
#if DEBUG_
            for (int idx = 0; idx < count; idx++)
            {
                Debug.Assert(elements[idx].XRef != null);
                Debug.Assert(elements[idx].XRef.Document != null);
                Debug.Assert(elements[idx].Document != null);
                if (resources[idx].ObjectID.ObjectNumber == 12)
                {
                    GetType();
                }
            }
#endif

            // 2nd loop. Fix up indirect references that still refers to the external document.
            for (int idx = 0; idx < count; idx++)
            {
                PdfObject obj = elements[idx];
                Debug.Assert(owner != null);
                FixUpObject(importedObjectTable, importedObjectTable.Owner, obj);
            }

            // return the imported root object
            return(elements[0]);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Returns the PdfReference of the specified object, or null, if the object is not in the
 /// document's object table.
 /// </summary>
 public static PdfReference GetReference(PdfObject obj)
 {
   if (obj == null)
     throw new ArgumentNullException("obj");
   return obj.Reference;
 }
Exemplo n.º 19
0
 /// <summary>
 /// Sets the entry as a reference to the specified object. The object must be an indirect object,
 /// otherwise an exception is raised.
 /// </summary>
 public void SetReference(string key, PdfObject obj)
 {
   Debug.Assert(obj.Reference != null, "PdfObject must be an indirect object.");
   this[key] = obj.Reference;
 }
Exemplo n.º 20
0
 /// <summary>
 /// Gets the object identifier of the specified object.
 /// </summary>
 public static PdfObjectID GetObjectID(PdfObject obj)
 {
   if (obj == null)
     throw new ArgumentNullException("obj");
   return obj.ObjectID;
 }
Exemplo n.º 21
0
    /// <summary>
    /// Reads PDF object from input stream.
    /// </summary>
    /// <param name="pdfObject">Either the instance of a derived type or null. If it is null
    /// an appropriate object is created.</param>
    /// <param name="objectID">The address of the object.</param>
    /// <param name="includeReferences">If true, specifies that all indirect objects
    /// are included recursively.</param>
    public PdfObject ReadObject(PdfObject pdfObject, PdfObjectID objectID, bool includeReferences)
    {
      MoveToObject(objectID);
      int objectNumber = ReadInteger();
      int generationNumber = ReadInteger();
#if DEBUG
      // The following assertion sometime failed (see below)
      //Debug.Assert(objectID == new PdfObjectID(objectNumber, generationNumber));
      if (objectID != new PdfObjectID(objectNumber, generationNumber))
      {
        // A special kind of bug? Or is this an undocumented PDF feature?
        // PDF4NET 2.6 provides a sample called 'Unicode', which produces a file 'unicode.pdf'
        // The iref table of this file contains the following entries:
        //    iref
        //    0 148
        //    0000000000 65535 f 
        //    0000000015 00000 n 
        //    0000000346 00000 n 
        //    ....
        //    0000083236 00000 n 
        //    0000083045 00000 n 
        //    0000083045 00000 n 
        //    0000083045 00000 n 
        //    0000083045 00000 n 
        //    0000080334 00000 n 
        //    ....
        // Object 84, 85, 86, and 87 maps to the same dictionary, but all PDF readers I tested
        // ignores this mismatch! The following assertion failed about 50 times with this file.
#if true_
        string message = String.Format("xref entry {0} {1} maps to object {2} {3}.",
          objectID.ObjectNumber, objectID.GenerationNumber, objectNumber, generationNumber);
        Debug.Assert(false, message);
#endif
      }
#endif
      // Always use object ID from iref table (see above)
      objectNumber = objectID.ObjectNumber;
      generationNumber = objectID.GenerationNumber;
#if true_
      Debug.WriteLine(String.Format("obj: {0} {1}", objectNumber, generationNumber));
#endif
      ReadSymbol(Symbol.Obj);

      bool checkForStream = false;
      Symbol symbol = ScanNextToken();
      switch (symbol)
      {
        case Symbol.BeginArray:
          PdfArray array;
          if (pdfObject == null)
            array = new PdfArray(this.document);
          else
            array = (PdfArray)pdfObject;
          //PdfObject.RegisterObject(array, objectID, generation);
          pdfObject = ReadArray(array, includeReferences);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          break;

        case Symbol.BeginDictionary:
          PdfDictionary dict;
          if (pdfObject == null)
            dict = new PdfDictionary(this.document);
          else
            dict = (PdfDictionary)pdfObject;
          //PdfObject.RegisterObject(dict, objectID, generation);
          checkForStream = true;
          pdfObject = ReadDictionary(dict, includeReferences);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          break;

        // Acrobat 6 Professional proudly presents: The Null object!
        // Even with a one-digit object number an indirect reference «x 0 R» to this object is
        // one character larger than the direct use of «null». Probable this is the reason why
        // it is true that Acrobat Web Capture 6.0 creates this object, but obviously never 
        // creates a reference to it!
        case Symbol.Null:
          pdfObject = new PdfNullObject(this.document);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          ReadSymbol(Symbol.EndObj);
          return pdfObject;

        case Symbol.Boolean:
          pdfObject = new PdfBooleanObject(this.document, this.lexer.Token == Boolean.TrueString);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          ReadSymbol(Symbol.EndObj);
          return pdfObject;

        case Symbol.Integer:
          pdfObject = new PdfIntegerObject(this.document, this.lexer.TokenToInteger);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          ReadSymbol(Symbol.EndObj);
          return pdfObject;

        case Symbol.UInteger:
          pdfObject = new PdfUIntegerObject(this.document, this.lexer.TokenToUInteger);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          ReadSymbol(Symbol.EndObj);
          return pdfObject;

        case Symbol.Real:
          pdfObject = new PdfRealObject(this.document, this.lexer.TokenToReal);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          ReadSymbol(Symbol.EndObj);
          return pdfObject;

        case Symbol.String:
          pdfObject = new PdfStringObject(this.document, this.lexer.Token);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          ReadSymbol(Symbol.EndObj);
          return pdfObject;

        case Symbol.Name:
          pdfObject = new PdfNameObject(this.document, this.lexer.Token);
          pdfObject.SetObjectID(objectNumber, generationNumber);
          ReadSymbol(Symbol.EndObj);
          return pdfObject;

        case Symbol.Keyword:
          // Should not come here anymore
          throw new NotImplementedException("Keyword");

        default:
          // Should not come here anymore
          throw new NotImplementedException("unknown token");
      }
      symbol = ScanNextToken();
      if (symbol == Symbol.BeginStream)
      {
        PdfDictionary dict = (PdfDictionary)pdfObject;
        Debug.Assert(checkForStream, "Unexpected stream...");
        int length = GetStreamLength(dict);
        byte[] bytes = this.lexer.ReadStream(length);
#if true_
        if (dict.Elements.GetString("/Filter") == "/FlateDecode")
        {
          if (dict.Elements["/Subtype"] == null)
          {
            try
            {
              byte[] decoded = Filtering.FlateDecode.Decode(bytes);
              if (decoded.Length == 0)
                goto End;
              string pageContent = Filtering.FlateDecode.DecodeToString(bytes);
              if (pageContent.Length > 100)
                pageContent = pageContent.Substring(pageContent.Length - 100);
              pageContent.GetType();
              bytes = decoded;
              dict.Elements.Remove("/Filter");
              dict.Elements.SetInteger("/Length", bytes.Length);
            }
            catch
            {
            }
          }
        End:;
        }
#endif
        PdfDictionary.PdfStream stream = new PdfDictionary.PdfStream(bytes, dict);
        dict.Stream = stream;
        ReadSymbol(Symbol.EndStream);
        symbol = ScanNextToken();
      }
      if (symbol != Symbol.EndObj)
        throw new PdfReaderException(PSSR.UnexpectedToken(this.lexer.Token));
      return pdfObject;
    }
Exemplo n.º 22
0
 /// <summary>
 /// Gets the generation number of the specified object.
 /// </summary>
 public static int GenerationNumber(PdfObject obj)
 {
   if (obj == null)
     throw new ArgumentNullException("obj");
   return obj.GenerationNumber;
 }
Exemplo n.º 23
0
 /// <summary>
 /// Sets the entry as a reference to the specified object. The object must be an indirect object,
 /// otherwise an exception is raised.
 /// </summary>
 public void SetReference(string key, PdfObject obj)
 {
   if (obj.Reference == null)
     throw new ArgumentException("PdfObject must be an indirect object.", "obj");
   this[key] = obj.Reference;
 }
Exemplo n.º 24
0
 /// <summary>
 /// Adds an object to the PDF document. This operation and only this operation makes the object 
 /// an indirect object owned by this document.
 /// </summary>
 public void AddObject(PdfObject obj)
 {
   if (obj == null)
     throw new ArgumentNullException("obj");
   if (obj.Owner == null)
     obj.Document = this.document;
   else if (obj.Owner != this.document)
     throw new InvalidOperationException("Object does not belong to this document.");
   this.document.irefTable.Add(obj);
 }
Exemplo n.º 25
0
    ///// <summary>
    ///// The garbage collector for PDF objects.
    ///// </summary>
    //public sealed class GC
    //{
    //  PdfXRefTable xrefTable;
    //
    //  internal GC(PdfXRefTable xrefTable)
    //  {
    //    this.xrefTable = xrefTable;
    //  }
    //
    //  public void Collect()
    //  {
    //  }
    //
    //  public PdfReference[] ReachableObjects()
    //  {
    //    Hashtable objects = new Hashtable();
    //    TransitiveClosure(objects, this.xrefTable.document.trailer);
    //  }

    /// <summary>
    /// Calculates the transitive closure of the specified PdfObject, i.e. all indirect objects
    /// recursively reachable from the specified object.
    /// </summary>
    public PdfReference[] TransitiveClosure(PdfObject pdfObject)
    {
      return TransitiveClosure(pdfObject, Int16.MaxValue);
    }
Exemplo n.º 26
0
    /// <summary>
    /// Removes an object from the PDF document.
    /// </summary>
    public void RemoveObject(PdfObject obj)
    {
      if (obj == null)
        throw new ArgumentNullException("obj");
      if (obj.Reference == null)
        throw new InvalidOperationException("Only indirect objects can be removed.");
      if (obj.Owner != this.document)
        throw new InvalidOperationException("Object does not belong to this document.");

      this.document.irefTable.Remove(obj.Reference);
    }
Exemplo n.º 27
0
    void TransitiveClosureImplementation(Dictionary<PdfItem, object> objects, PdfObject pdfObject, ref int depth)
    {
      if (depth-- == 0)
        return;
      try
      {
        nestingLevel++;
        if (nestingLevel >= 1000)
        {
          //Debug.WriteLine(String.Format("Nestinglevel={0}", nestingLevel));
          //GetType();
          if (!this.overflow.ContainsKey(pdfObject))
            this.overflow.Add(pdfObject, null);
          return;
        }
#if DEBUG_
        //enterCount++;
        if (enterCount == 5400)
          GetType();
        //if (!Object.ReferenceEquals(pdfObject.Owner, this.document))
        //  GetType();
        //////Debug.Assert(Object.ReferenceEquals(pdfObject27.Document, this.document));
        //      if (item is PdfObject && ((PdfObject)item).ObjectID.ObjectNumber == 5)
        //        Debug.WriteLine("items: " + ((PdfObject)item).ObjectID.ToString());
        //if (pdfObject.ObjectNumber == 5)
        //  GetType();
#endif

        IEnumerable enumerable = null; //(IEnumerator)pdfObject;
        if (pdfObject is PdfDictionary)
          enumerable = ((PdfDictionary)pdfObject).Elements.Values;
        else if (pdfObject is PdfArray)
          enumerable = ((PdfArray)pdfObject).Elements;
        if (enumerable != null)
        {
          foreach (PdfItem item in enumerable)
          {
            PdfReference iref = item as PdfReference;
            if (iref != null)
            {
              // Is this an indirect reference to an object that not exists?
              //if (iref.Document == null)
              //{
              //  Debug.WriteLine("Dead object dedected: " + iref.ObjectID.ToString());
              //  PdfReference dead = DeadObject;
              //  iref.ObjectID = dead.ObjectID;
              //  iref.Document = this.document;
              //  iref.SetObject(dead.Value);
              //  PdfDictionary dict = (PdfDictionary)dead.Value;
              //
              //  dict.Elements["/DeadObjectCount"] = 
              //    new PdfInteger(dict.Elements.GetInteger("/DeadObjectCount") + 1);
              //
              //  iref = dead;
              //}

              if (!Object.ReferenceEquals(iref.Document, this.document))
              {
                GetType();
                Debug.WriteLine(String.Format("Bad iref: {0}", iref.ObjectID.ToString()));
              }
              Debug.Assert(Object.ReferenceEquals(iref.Document, this.document) || iref.Document == null, "External object detected!");
#if DEBUG
              if (iref.ObjectID.ObjectNumber == 23)
                GetType();
#endif
              if (!objects.ContainsKey(iref))
              {
                PdfObject value = iref.Value;

                // Ignore unreachable objets
                if (iref.Document != null)
                {
                  // ... from trailer hack
                  if (value == null)
                  {
                    iref = this.objectTable[iref.ObjectID];
                    Debug.Assert(iref.Value != null);
                    value = iref.Value;
                  }
                  Debug.Assert(Object.ReferenceEquals(iref.Document, this.document));
                  objects.Add(iref, null);
                  //Debug.WriteLine(String.Format("objects.Add('{0}', null);", iref.ObjectID.ToString()));
                  if (value is PdfArray || value is PdfDictionary)
                    TransitiveClosureImplementation(objects, value, ref depth);
                }
                //else
                //{
                //  objects2.Add(this[iref.ObjectID], null);
                //}
              }
            }
            else
            {
              PdfObject pdfObject28 = item as PdfObject;
              //if (pdfObject28 != null)
              //  Debug.Assert(Object.ReferenceEquals(pdfObject28.Document, this.document));
              if (pdfObject28 != null && (pdfObject28 is PdfDictionary || pdfObject28 is PdfArray))
                TransitiveClosureImplementation(objects, pdfObject28, ref depth);
            }
          }
        }
      }
      finally
      {
        nestingLevel--;
      }
    }
Exemplo n.º 28
0
 /// <summary>
 /// Returns an array containing the specified object as first element follows by its transitive
 /// closure. The closure of an object are all objects that can be reached by indirect references. 
 /// The transitive closure is the result of applying the calculation of the closure to a closure
 /// as long as no new objects came along. This is e.g. useful for getting all objects belonging 
 /// to the resources of a page.
 /// </summary>
 public PdfObject[] GetClosure(PdfObject obj) // TODO: "..., bool transitive)"
 {
   PdfReference[] references = this.document.irefTable.TransitiveClosure(obj);
   int count = references.Length + 1;
   PdfObject[] objects = new PdfObject[count];
   objects[0] = obj;
   for (int idx = 1; idx < count; idx++)
     objects[idx] = references[idx - 1].Value;
   return objects;
 }
Exemplo n.º 29
0
    ///// <summary>
    ///// Creates a deep copy of the specified value and its transitive closure and adds the
    ///// new objects to the specified owner document.
    ///// </summary>
    /// <param name="owner">The document that owns the cloned objects.</param>
    /// <param name="externalObject">The root object to be cloned.</param>
    /// <returns>The clone of the root object</returns>
    internal static PdfObject DeepCopyClosure(PdfDocument owner, PdfObject externalObject)
    {
      // Get transitive closure
      PdfObject[] elements = externalObject.Owner.Internals.GetClosure(externalObject);
      int count = elements.Length;
#if DEBUG_
        for (int idx = 0; idx < count; idx++)
        {
          Debug.Assert(elements[idx].XRef != null);
          Debug.Assert(elements[idx].XRef.Document != null);
          Debug.Assert(elements[idx].Document != null);
          if (elements[idx].ObjectID.ObjectNumber == 12)
            GetType();
        }
#endif
      // 1st loop. Replace all objects by their clones.
      PdfImportedObjectTable iot = new PdfImportedObjectTable(owner, externalObject.Owner);
      for (int idx = 0; idx < count; idx++)
      {
        PdfObject obj = elements[idx];
        PdfObject clone = obj.Clone();
        Debug.Assert(clone.Reference == null);
        clone.Document = owner;
        if (obj.Reference != null)
        {
          // Case: The cloned object was an indirect object.
          // add clone to new owner document
          owner.irefTable.Add(clone);
          // the clone gets an iref by adding it to its new owner
          Debug.Assert(clone.Reference != null);
          // save an association from old object identifier to new iref
          iot.Add(obj.ObjectID, clone.Reference);
        }
        else
        {
          // Case: The cloned object was an direct object.
          // only the root object can be a direct object
          Debug.Assert(idx == 0);
        }
        // replace external object by its clone
        elements[idx] = clone;
      }
#if DEBUG_
        for (int idx = 0; idx < count; idx++)
        {
          Debug.Assert(elements[idx].XRef != null);
          Debug.Assert(elements[idx].XRef.Document != null);
          Debug.Assert(resources[idx].Document != null);
          if (elements[idx].ObjectID.ObjectNumber == 12)
            GetType();
        }
#endif

      // 2nd loop. Fix up all indirect references that still refers to the import document.
      for (int idx = 0; idx < count; idx++)
      {
        PdfObject obj = elements[idx];
        Debug.Assert(obj.Owner == owner);
        FixUpObject(iot, owner, obj);
      }

      // return the clone of the former root object
      return elements[0];
    }
Exemplo n.º 30
0
    ///// <summary>
    ///// Imports an object and its transitive closure to the specified document.
    ///// </summary>
    /// <param name="importedObjectTable">The imported object table of the owner for the external document.</param>
    /// <param name="owner">The document that owns the cloned objects.</param>
    /// <param name="externalObject">The root object to be cloned.</param>
    /// <returns>The clone of the root object</returns>
    internal static PdfObject ImportClosure(PdfImportedObjectTable importedObjectTable, PdfDocument owner, PdfObject externalObject)
    {
      Debug.Assert(ReferenceEquals(importedObjectTable.Owner, owner), "importedObjectTable does not belong to the owner.");
      Debug.Assert(ReferenceEquals(importedObjectTable.ExternalDocument, externalObject.Owner),
        "The ExternalDocument of the importedObjectTable does not belong to the owner of object to be imported.");

      // Get transitive closure of external object
      PdfObject[] elements = externalObject.Owner.Internals.GetClosure(externalObject);
      int count = elements.Length;
#if DEBUG_
        for (int idx = 0; idx < count; idx++)
        {
          Debug.Assert(elements[idx].XRef != null);
          Debug.Assert(elements[idx].XRef.Document != null);
          Debug.Assert(elements[idx].Document != null);
          if (elements[idx].ObjectID.ObjectNumber == 12)
            GetType();
        }
#endif
      // 1st loop. Already imported objects are reused and new ones are cloned.
      for (int idx = 0; idx < count; idx++)
      {
        PdfObject obj = elements[idx];
        Debug.Assert(!ReferenceEquals(obj.Owner, owner));

        if (importedObjectTable.Contains(obj.ObjectID))
        {
          // External object was already imported
          PdfReference iref = importedObjectTable[obj.ObjectID];
          Debug.Assert(iref != null);
          Debug.Assert(iref.Value != null);
          Debug.Assert(iref.Document == owner);
          // replace external object by the already cloned counterpart
          elements[idx] = iref.Value;
        }
        else
        {
          // External object was not imported ealier and must be cloned
          PdfObject clone = obj.Clone();
          Debug.Assert(clone.Reference == null);
          clone.Document = owner;
          if (obj.Reference != null)
          {
            // Case: The cloned object was an indirect object.
            // add clone to new owner document
            owner.irefTable.Add(clone);
            Debug.Assert(clone.Reference != null);
            // save an association from old object identifier to new iref
            importedObjectTable.Add(obj.ObjectID, clone.Reference);
          }
          else
          {
            // Case: The cloned object was a direct object.
            // only the root object can be a direct object
            Debug.Assert(idx == 0);
            //// add it to this (the importer) document
            //owner.irefTable.Add(clone);
            //Debug.Assert(clone.Reference != null);
          }
          // replace external object by its clone
          elements[idx] = clone;
        }
      }
#if DEBUG_
      for (int idx = 0; idx < count; idx++)
      {
        Debug.Assert(elements[idx].XRef != null);
        Debug.Assert(elements[idx].XRef.Document != null);
        Debug.Assert(elements[idx].Document != null);
        if (resources[idx].ObjectID.ObjectNumber == 12)
          GetType();
      }
#endif

      // 2nd loop. Fix up indirect references that still refers to the external document.
      for (int idx = 0; idx < count; idx++)
      {
        PdfObject obj = elements[idx];
        Debug.Assert(owner != null);
        FixUpObject(importedObjectTable, importedObjectTable.Owner, obj);
      }

      // return the imported root object
      return elements[0];
    }
Exemplo n.º 31
0
 /// <summary>
 /// Begins a direct or indirect dictionary or array.
 /// </summary>
 public void WriteBeginObject(PdfObject value)
 {
   bool indirect = value.IsIndirect;
   if (indirect)
   {
     WriteObjectAddress(value);
     if (this.securityHandler != null)
       this.securityHandler.SetHashKey(value.ObjectID);
   }
   this.stack.Add(new StackItem(value));
   if (indirect)
   {
     if (value is PdfArray)
       WriteRaw("[\n");
     else if (value is PdfDictionary)
       WriteRaw("<<\n");
     this.lastCat = CharCat.NewLine;
   }
   else
   {
     if (value is PdfArray)
     {
       WriteSeparator(CharCat.Delimiter);
       WriteRaw('[');
       this.lastCat = CharCat.Delimiter;
     }
     else if (value is PdfDictionary)
     {
       NewLine();
       WriteSeparator(CharCat.Delimiter);
       WriteRaw("<<\n");
       this.lastCat = CharCat.NewLine;
     }
   }
   if (this.layout == PdfWriterLayout.Verbose)
     IncreaseIndent();
 }
Exemplo n.º 32
0
    /// <summary>
    /// Replace all indirect references to external objects by their cloned counterparts
    /// owned by the importer document.
    /// </summary>
    internal static void FixUpObject(PdfImportedObjectTable iot, PdfDocument owner, PdfObject value)
    {
      Debug.Assert(ReferenceEquals(iot.Owner, owner));

      PdfDictionary dict;
      PdfArray array;
      if ((dict = value as PdfDictionary) != null)
      {
        // Set document for cloned direct objects
        if (dict.Owner == null)
          dict.Document = owner;
        else
          Debug.Assert(dict.Owner == owner);

        // Search for indirect references in all keys
        PdfName[] names = dict.Elements.KeyNames;
        foreach (PdfName name in names)
        {
          PdfItem item = dict.Elements[name];
          // Is item an iref?
          PdfReference iref = item as PdfReference;
          if (iref != null)
          {
            // Does the iref already belongs to the owner?
            if (iref.Document == owner)
            {
              // Yes: fine. Happens when an already cloned object is reused.
              continue;
            }
            else
            {
              //Debug.Assert(iref.Document == iot.Document);
              // No: replace with iref of cloned object
              PdfReference newXRef = iot[iref.ObjectID];
              Debug.Assert(newXRef != null);
              Debug.Assert(newXRef.Document == owner);
              dict.Elements[name] = newXRef;
            }
          }
          else if (item is PdfObject)
          {
            // Fix up inner objects
            FixUpObject(iot, owner, (PdfObject)item);
          }
        }
      }
      else if ((array = value as PdfArray) != null)
      {
        // Set document for cloned direct objects
        if (array.Owner == null)
          array.Document = owner;
        else
          Debug.Assert(array.Owner == owner);

        // Search for indirect references in all array elements
        int count = array.Elements.Count;
        for (int idx = 0; idx < count; idx++)
        {
          PdfItem item = array.Elements[idx];
          // Is item an iref?
          PdfReference iref = item as PdfReference;
          if (iref != null)
          {
            // Does the iref already belongs to the owner?
            if (iref.Document == owner)
            {
              // Yes: fine. Happens when an already cloned object is reused.
              continue;
            }
            else
            {
              Debug.Assert(iref.Document == iot.ExternalDocument);
              // No: replace with iref of cloned object
              PdfReference newXRef = iot[iref.ObjectID];
              Debug.Assert(newXRef != null);
              Debug.Assert(newXRef.Document == owner);
              array.Elements[idx] = newXRef;
            }
          }
          else if (item is PdfObject)
          {
            // Fix up inner objects
            FixUpObject(iot, owner, (PdfObject)item);
          }
        }
      }
    }
Exemplo n.º 33
0
 public StackItem(PdfObject value)
 {
   Object = value;
 }
Exemplo n.º 34
0
        ///// <summary>
        ///// Creates a deep copy of the specified value and its transitive closure and adds the
        ///// new objects to the specified owner document.
        ///// </summary>
        /// <param name="owner">The document that owns the cloned objects.</param>
        /// <param name="externalObject">The root object to be cloned.</param>
        /// <returns>The clone of the root object</returns>
        internal static PdfObject DeepCopyClosure(PdfDocument owner, PdfObject externalObject)
        {
            // Get transitive closure
            PdfObject[] elements = externalObject.Owner.Internals.GetClosure(externalObject);
            int         count    = elements.Length;

#if DEBUG_
            for (int idx = 0; idx < count; idx++)
            {
                Debug.Assert(elements[idx].XRef != null);
                Debug.Assert(elements[idx].XRef.Document != null);
                Debug.Assert(elements[idx].Document != null);
                if (elements[idx].ObjectID.ObjectNumber == 12)
                {
                    GetType();
                }
            }
#endif
            // 1st loop. Replace all objects by their clones.
            PdfImportedObjectTable iot = new PdfImportedObjectTable(owner, externalObject.Owner);
            for (int idx = 0; idx < count; idx++)
            {
                PdfObject obj   = elements[idx];
                PdfObject clone = obj.Clone();
                Debug.Assert(clone.Reference == null);
                clone.Document = owner;
                if (obj.Reference != null)
                {
                    // Case: The cloned object was an indirect object.
                    // add clone to new owner document
                    owner.irefTable.Add(clone);
                    // the clone gets an iref by adding it to its new owner
                    Debug.Assert(clone.Reference != null);
                    // save an association from old object identifier to new iref
                    iot.Add(obj.ObjectID, clone.Reference);
                }
                else
                {
                    // Case: The cloned object was an direct object.
                    // only the root object can be a direct object
                    Debug.Assert(idx == 0);
                }
                // replace external object by its clone
                elements[idx] = clone;
            }
#if DEBUG_
            for (int idx = 0; idx < count; idx++)
            {
                Debug.Assert(elements[idx].XRef != null);
                Debug.Assert(elements[idx].XRef.Document != null);
                Debug.Assert(resources[idx].Document != null);
                if (elements[idx].ObjectID.ObjectNumber == 12)
                {
                    GetType();
                }
            }
#endif

            // 2nd loop. Fix up all indirect references that still refers to the import document.
            for (int idx = 0; idx < count; idx++)
            {
                PdfObject obj = elements[idx];
                Debug.Assert(obj.Owner == owner);
                FixUpObject(iot, owner, obj);
            }

            // return the clone of the former root object
            return(elements[0]);
        }