/// <summary> /// Processes two cases of object copying: /// <list type="number"> /// <item><description>copying to the other document /// </description></item> /// <item><description>cloning inside of the current document /// </description></item> /// </list> /// </summary> /// <remarks> /// Processes two cases of object copying: /// <list type="number"> /// <item><description>copying to the other document /// </description></item> /// <item><description>cloning inside of the current document /// </description></item> /// </list> /// <para /> /// This two cases are distinguished by the state of /// <c>document</c> /// parameter: /// the second case is processed if /// <c>document</c> /// is /// <see langword="null"/>. /// </remarks> /// <param name="documentTo">if not null: document to copy object to; otherwise indicates that object is to be cloned. /// </param> /// <param name="allowDuplicating"> /// indicates if to allow copy objects which already have been copied. /// If object is associated with any indirect reference and allowDuplicating is false then already existing reference will be returned instead of copying object. /// If allowDuplicating is true then object will be copied and new indirect reference will be assigned. /// </param> /// <returns>copied object.</returns> internal virtual PdfObject ProcessCopying(PdfDocument documentTo, bool allowDuplicating) { if (documentTo != null) { //copyTo case PdfWriter writer = documentTo.GetWriter(); if (writer == null) { throw new PdfException(PdfException.CannotCopyToDocumentOpenedInReadingMode); } return(writer.CopyObject(this, documentTo, allowDuplicating)); } else { //clone case PdfObject obj = this; if (obj.IsIndirectReference()) { PdfObject refTo = ((PdfIndirectReference)obj).GetRefersTo(); obj = refTo != null ? refTo : obj; } if (obj.IsIndirect() && !allowDuplicating) { return(obj); } return(obj.Clone()); } }