/// <summary>Creates clone of the object which belongs to the same document as original object.</summary> /// <remarks> /// Creates clone of the object which belongs to the same document as original object. /// New object shall not be used in other documents. /// </remarks> /// <returns>cloned object.</returns> public virtual PdfObject Clone() { PdfObject newObject = NewInstance(); if (indirectReference != null || CheckState(MUST_BE_INDIRECT)) { newObject.SetState(MUST_BE_INDIRECT); } newObject.CopyContent(this, null); return(newObject); }
protected internal virtual PdfObject CopyObject(PdfObject obj, PdfDocument document, bool allowDuplicating ) { if (obj is PdfIndirectReference) { obj = ((PdfIndirectReference)obj).GetRefersTo(); } if (obj == null) { obj = PdfNull.PDF_NULL; } if (CheckTypeOfPdfDictionary(obj, PdfName.Catalog)) { ILogger logger = LoggerFactory.GetLogger(typeof(PdfReader)); logger.Warn(LogMessageConstant.MAKE_COPY_OF_CATALOG_DICTIONARY_IS_FORBIDDEN); obj = PdfNull.PDF_NULL; } PdfIndirectReference indirectReference = obj.GetIndirectReference(); PdfIndirectReference copiedIndirectReference; int copyObjectKey = 0; if (!allowDuplicating && indirectReference != null) { copyObjectKey = GetCopyObjectKey(obj); copiedIndirectReference = copiedObjects.Get(copyObjectKey); if (copiedIndirectReference != null) { return(copiedIndirectReference.GetRefersTo()); } } if (properties.smartMode && !CheckTypeOfPdfDictionary(obj, PdfName.Page)) { PdfObject copiedObject = SmartCopyObject(obj); if (copiedObject != null) { return(copiedObjects.Get(GetCopyObjectKey(copiedObject)).GetRefersTo()); } } PdfObject newObject = obj.NewInstance(); if (indirectReference != null) { if (copyObjectKey == 0) { copyObjectKey = GetCopyObjectKey(obj); } PdfIndirectReference reference = newObject.MakeIndirect(document).GetIndirectReference(); copiedObjects[copyObjectKey] = reference; } newObject.CopyContent(obj, document); return(newObject); }
protected internal virtual PdfObject CopyObject(PdfObject obj, PdfDocument documentTo, bool allowDuplicating ) { if (obj is PdfIndirectReference) { obj = ((PdfIndirectReference)obj).GetRefersTo(); } if (obj == null) { obj = PdfNull.PDF_NULL; } if (CheckTypeOfPdfDictionary(obj, PdfName.Catalog)) { ILog logger = LogManager.GetLogger(typeof(PdfReader)); logger.Warn(iText.IO.LogMessageConstant.MAKE_COPY_OF_CATALOG_DICTIONARY_IS_FORBIDDEN); obj = PdfNull.PDF_NULL; } PdfIndirectReference indirectReference = obj.GetIndirectReference(); PdfDocument.IndirectRefDescription copiedObjectKey = null; bool tryToFindDuplicate = !allowDuplicating && indirectReference != null; if (tryToFindDuplicate) { copiedObjectKey = new PdfDocument.IndirectRefDescription(indirectReference); PdfIndirectReference copiedIndirectReference = copiedObjects.Get(copiedObjectKey); if (copiedIndirectReference != null) { return(copiedIndirectReference.GetRefersTo()); } } SerializedObjectContent serializedContent = null; if (properties.smartMode && tryToFindDuplicate && !CheckTypeOfPdfDictionary(obj, PdfName.Page)) { serializedContent = smartModeSerializer.SerializeObject(obj); PdfIndirectReference objectRef = smartModeSerializer.GetSavedSerializedObject(serializedContent); if (objectRef != null) { copiedObjects.Put(copiedObjectKey, objectRef); return(objectRef.refersTo); } } PdfObject newObject = obj.NewInstance(); if (indirectReference != null) { if (copiedObjectKey == null) { copiedObjectKey = new PdfDocument.IndirectRefDescription(indirectReference); } PdfIndirectReference indRef = newObject.MakeIndirect(documentTo).GetIndirectReference(); if (serializedContent != null) { smartModeSerializer.SaveSerializedObject(serializedContent, indRef); } copiedObjects.Put(copiedObjectKey, indRef); } newObject.CopyContent(obj, documentTo); return(newObject); }
protected internal virtual PdfObject CopyObject(PdfObject obj, PdfDocument document, bool allowDuplicating ) { if (obj is PdfIndirectReference) { obj = ((PdfIndirectReference)obj).GetRefersTo(); } if (obj == null) { obj = PdfNull.PDF_NULL; } if (CheckTypeOfPdfDictionary(obj, PdfName.Catalog)) { ILogger logger = LoggerFactory.GetLogger(typeof(PdfReader)); logger.Warn(LogMessageConstant.MAKE_COPY_OF_CATALOG_DICTIONARY_IS_FORBIDDEN); obj = PdfNull.PDF_NULL; } PdfIndirectReference indirectReference = obj.GetIndirectReference(); int copyObjectKey = 0; bool tryToFindDuplicate = !allowDuplicating && indirectReference != null; if (tryToFindDuplicate) { copyObjectKey = CalculateIndRefKey(indirectReference); PdfIndirectReference copiedIndirectReference = copiedObjects.Get(copyObjectKey); if (copiedIndirectReference != null) { return(copiedIndirectReference.GetRefersTo()); } } if (obj.IsDictionary()) { PdfName subtype = ((PdfDictionary)obj).GetAsName(PdfName.Subtype); if (subtype != null && subtype.Equals(PdfName.Widget)) { tryToFindDuplicate = false; } } if (properties.smartMode && tryToFindDuplicate && !CheckTypeOfPdfDictionary(obj, PdfName.Page)) { PdfIndirectReference copiedObjectRef = TryToFindPreviouslyCopiedEqualObject(obj); if (copiedObjectRef != null) { PdfIndirectReference copiedIndirectReference = copiedObjects.Get(CalculateIndRefKey(copiedObjectRef)); copiedObjects[copyObjectKey] = copiedIndirectReference; return(copiedIndirectReference.GetRefersTo()); } } PdfObject newObject = obj.NewInstance(); if (indirectReference != null) { if (copyObjectKey == 0) { copyObjectKey = CalculateIndRefKey(indirectReference); } PdfIndirectReference indRef = newObject.MakeIndirect(document).GetIndirectReference(); copiedObjects[copyObjectKey] = indRef; } newObject.CopyContent(obj, document); return(newObject); }