예제 #1
0
 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));
                 }
             }
         }
     }
 }
예제 #2
0
        private static PdfDictionary CopyNamespaceDict(PdfDictionary srcNsDict, StructureTreeCopier.StructElemCopyingParams
                                                       copyingParams)
        {
            IList <PdfName> excludeKeys  = JavaCollectionsUtil.SingletonList <PdfName>(PdfName.RoleMapNS);
            PdfDocument     toDocument   = copyingParams.GetToDocument();
            PdfDictionary   copiedNsDict = srcNsDict.CopyTo(toDocument, excludeKeys, false);

            copyingParams.AddCopiedNamespace(copiedNsDict);
            PdfDictionary srcRoleMapNs = srcNsDict.GetAsDictionary(PdfName.RoleMapNS);
            // if this src namespace was already copied (or in the process of copying) it will contain role map already
            PdfDictionary copiedRoleMap = copiedNsDict.GetAsDictionary(PdfName.RoleMapNS);

            if (srcRoleMapNs != null && copiedRoleMap == null)
            {
                copiedRoleMap = new PdfDictionary();
                copiedNsDict.Put(PdfName.RoleMapNS, copiedRoleMap);
                foreach (KeyValuePair <PdfName, PdfObject> entry in srcRoleMapNs.EntrySet())
                {
                    PdfObject copiedMapping;
                    if (entry.Value.IsArray())
                    {
                        PdfArray srcMappingArray = (PdfArray)entry.Value;
                        if (srcMappingArray.Size() > 1 && srcMappingArray.Get(1).IsDictionary())
                        {
                            PdfArray copiedMappingArray = new PdfArray();
                            copiedMappingArray.Add(srcMappingArray.Get(0).CopyTo(toDocument));
                            PdfDictionary copiedNamespace = CopyNamespaceDict(srcMappingArray.GetAsDictionary(1), copyingParams);
                            copiedMappingArray.Add(copiedNamespace);
                            copiedMapping = copiedMappingArray;
                        }
                        else
                        {
                            ILog logger = LogManager.GetLogger(typeof(StructureTreeCopier));
                            logger.Warn(String.Format(iText.IO.LogMessageConstant.ROLE_MAPPING_FROM_SOURCE_IS_NOT_COPIED_INVALID, entry
                                                      .Key.ToString()));
                            continue;
                        }
                    }
                    else
                    {
                        copiedMapping = entry.Value.CopyTo(toDocument);
                    }
                    PdfName copiedRoleFrom = (PdfName)entry.Key.CopyTo(toDocument);
                    copiedRoleMap.Put(copiedRoleFrom, copiedMapping);
                }
            }
            return(copiedNsDict);
        }