private static void CopyTo(PdfDocument destDocument, IDictionary<PdfPage, PdfPage> page2page, PdfDocument 
     callingDocument, bool copyFromDestDocument, int insertIndex) {
     StructureTreeCopier.CopyStructureResult copiedStructure = CopyStructure(destDocument, page2page, callingDocument
         , copyFromDestDocument);
     PdfStructTreeRoot destStructTreeRoot = destDocument.GetStructTreeRoot();
     destStructTreeRoot.MakeIndirect(destDocument);
     foreach (PdfDictionary copied in copiedStructure.GetTopsList()) {
         destStructTreeRoot.AddKidObject(insertIndex, copied);
         if (insertIndex > -1) {
             ++insertIndex;
         }
     }
     if (!copyFromDestDocument) {
         if (!copiedStructure.GetCopiedNamespaces().IsEmpty()) {
             destStructTreeRoot.GetNamespacesObject().AddAll(copiedStructure.GetCopiedNamespaces());
         }
         PdfDictionary srcRoleMap = callingDocument.GetStructTreeRoot().GetRoleMap();
         PdfDictionary destRoleMap = destStructTreeRoot.GetRoleMap();
         foreach (KeyValuePair<PdfName, PdfObject> mappingEntry in srcRoleMap.EntrySet()) {
             if (!destRoleMap.ContainsKey(mappingEntry.Key)) {
                 destRoleMap.Put(mappingEntry.Key, mappingEntry.Value);
             }
             else {
                 if (!mappingEntry.Value.Equals(destRoleMap.Get(mappingEntry.Key))) {
                     String srcMapping = mappingEntry.Key + " -> " + mappingEntry.Value;
                     String destMapping = mappingEntry.Key + " -> " + destRoleMap.Get(mappingEntry.Key);
                     ILog logger = LogManager.GetLogger(typeof(StructureTreeCopier));
                     logger.Warn(String.Format(iText.IO.LogMessageConstant.ROLE_MAPPING_FROM_SOURCE_IS_NOT_COPIED_ALREADY_EXIST
                         , srcMapping, destMapping));
                 }
             }
         }
     }
 }
 private static StructureTreeCopier.CopyStructureResult CopyStructure(PdfDocument destDocument, IDictionary
     <PdfPage, PdfPage> page2page, PdfDocument callingDocument, bool copyFromDestDocument) {
     PdfDocument fromDocument = copyFromDestDocument ? destDocument : callingDocument;
     IDictionary<PdfDictionary, PdfDictionary> topsToFirstDestPage = new Dictionary<PdfDictionary, PdfDictionary
         >();
     ICollection<PdfObject> objectsToCopy = new HashSet<PdfObject>();
     IDictionary<PdfDictionary, PdfDictionary> page2pageDictionaries = new Dictionary<PdfDictionary, PdfDictionary
         >();
     foreach (KeyValuePair<PdfPage, PdfPage> page in page2page) {
         page2pageDictionaries.Put(page.Key.GetPdfObject(), page.Value.GetPdfObject());
         ICollection<PdfMcr> mcrs = fromDocument.GetStructTreeRoot().GetPageMarkedContentReferences(page.Key);
         if (mcrs != null) {
             foreach (PdfMcr mcr in mcrs) {
                 if (mcr is PdfMcrDictionary || mcr is PdfObjRef) {
                     objectsToCopy.Add(mcr.GetPdfObject());
                 }
                 PdfDictionary top = AddAllParentsToSet(mcr, objectsToCopy);
                 if (top != null) {
                     if (top.IsFlushed()) {
                         throw new PdfException(PdfException.CannotCopyFlushedTag);
                     }
                     if (!topsToFirstDestPage.ContainsKey(top)) {
                         topsToFirstDestPage.Put(top, page.Value.GetPdfObject());
                     }
                 }
             }
         }
     }
     IList<PdfDictionary> topsInOriginalOrder = new List<PdfDictionary>();
     foreach (IStructureNode kid in fromDocument.GetStructTreeRoot().GetKids()) {
         if (kid == null) {
             continue;
         }
         PdfDictionary kidObject = ((PdfStructElem)kid).GetPdfObject();
         if (topsToFirstDestPage.ContainsKey(kidObject)) {
             topsInOriginalOrder.Add(kidObject);
         }
     }
     StructureTreeCopier.StructElemCopyingParams structElemCopyingParams = new StructureTreeCopier.StructElemCopyingParams
         (objectsToCopy, destDocument, page2pageDictionaries, copyFromDestDocument);
     PdfStructTreeRoot destStructTreeRoot = destDocument.GetStructTreeRoot();
     destStructTreeRoot.MakeIndirect(destDocument);
     IList<PdfDictionary> copiedTops = new List<PdfDictionary>();
     foreach (PdfDictionary top in topsInOriginalOrder) {
         PdfDictionary copied = CopyObject(top, topsToFirstDestPage.Get(top), false, structElemCopyingParams);
         copiedTops.Add(copied);
     }
     return new StructureTreeCopier.CopyStructureResult(copiedTops, structElemCopyingParams.GetCopiedNamespaces
         ());
 }