コード例 #1
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);
        }
コード例 #2
0
        private static PdfDictionary CopyObject(PdfDictionary source, ICollection <PdfObject> objectsToCopy, PdfDocument
                                                toDocument, IDictionary <PdfDictionary, PdfDictionary> page2page, bool copyFromDestDocument)
        {
            PdfDictionary copied;

            if (copyFromDestDocument)
            {
                copied = source.Clone(ignoreKeysForCopy);
                if (source.IsIndirect())
                {
                    copied.MakeIndirect(toDocument);
                }
            }
            else
            {
                copied = source.CopyTo(toDocument, ignoreKeysForCopy, true);
            }
            if (source.ContainsKey(PdfName.Obj))
            {
                PdfDictionary obj = source.GetAsDictionary(PdfName.Obj);
                if (!copyFromDestDocument && obj != null)
                {
                    // Link annotations could be not added to the toDocument, so we need to identify this case.
                    // When obj.copyTo is called, and annotation was already copied, we would get this already created copy.
                    // If it was already copied and added, /P key would be set. Otherwise /P won't be set.
                    obj = obj.CopyTo(toDocument, iText.IO.Util.JavaUtil.ArraysAsList(PdfName.P), false);
                    copied.Put(PdfName.Obj, obj);
                }
            }
            PdfDictionary pg = source.GetAsDictionary(PdfName.Pg);

            if (pg != null)
            {
                //TODO It is possible, that pg will not be present in the page2page map. Consider the situation,
                // that we want to copy structElem because it has marked content dictionary reference, which belongs to the page from page2page,
                // but the structElem itself has /Pg which value could be arbitrary page.
                copied.Put(PdfName.Pg, page2page.Get(pg));
            }
            PdfObject k = source.Get(PdfName.K);

            if (k != null)
            {
                if (k.IsArray())
                {
                    PdfArray kArr   = (PdfArray)k;
                    PdfArray newArr = new PdfArray();
                    for (int i = 0; i < kArr.Size(); i++)
                    {
                        PdfObject copiedKid = CopyObjectKid(kArr.Get(i), copied, objectsToCopy, toDocument, page2page, copyFromDestDocument
                                                            );
                        if (copiedKid != null)
                        {
                            newArr.Add(copiedKid);
                        }
                    }
                    // TODO new array may be empty or with single element
                    copied.Put(PdfName.K, newArr);
                }
                else
                {
                    PdfObject copiedKid = CopyObjectKid(k, copied, objectsToCopy, toDocument, page2page, copyFromDestDocument);
                    if (copiedKid != null)
                    {
                        copied.Put(PdfName.K, copiedKid);
                    }
                }
            }
            return(copied);
        }
コード例 #3
0
        private static PdfDictionary CopyObject(PdfDictionary source, PdfDictionary destPage, bool parentChangePg,
                                                StructureTreeCopier.StructElemCopyingParams copyingParams)
        {
            PdfDictionary copied;

            if (copyingParams.IsCopyFromDestDocument())
            {
                //TODO: detect wether object is needed to be cloned at all
                copied = source.Clone(ignoreKeysForClone);
                if (source.IsIndirect())
                {
                    copied.MakeIndirect(copyingParams.GetToDocument());
                }
                PdfDictionary pg = source.GetAsDictionary(PdfName.Pg);
                if (pg != null)
                {
                    if (copyingParams.IsCopyFromDestDocument())
                    {
                        if (pg != destPage)
                        {
                            copied.Put(PdfName.Pg, destPage);
                            parentChangePg = true;
                        }
                        else
                        {
                            parentChangePg = false;
                        }
                    }
                }
            }
            else
            {
                copied = source.CopyTo(copyingParams.GetToDocument(), ignoreKeysForCopy, true);
                PdfDictionary obj = source.GetAsDictionary(PdfName.Obj);
                if (obj != null)
                {
                    // Link annotations could be not added to the toDocument, so we need to identify this case.
                    // When obj.copyTo is called, and annotation was already copied, we would get this already created copy.
                    // If it was already copied and added, /P key would be set. Otherwise /P won't be set.
                    obj = obj.CopyTo(copyingParams.GetToDocument(), JavaUtil.ArraysAsList(PdfName.P), false);
                    copied.Put(PdfName.Obj, obj);
                }
                PdfDictionary nsDict = source.GetAsDictionary(PdfName.NS);
                if (nsDict != null)
                {
                    PdfDictionary copiedNsDict = CopyNamespaceDict(nsDict, copyingParams);
                    copied.Put(PdfName.NS, copiedNsDict);
                }
                PdfDictionary pg = source.GetAsDictionary(PdfName.Pg);
                if (pg != null)
                {
                    PdfDictionary pageAnalog = copyingParams.GetPage2page().Get(pg);
                    if (pageAnalog == null)
                    {
                        pageAnalog     = destPage;
                        parentChangePg = true;
                    }
                    else
                    {
                        parentChangePg = false;
                    }
                    copied.Put(PdfName.Pg, pageAnalog);
                }
            }
            PdfObject k = source.Get(PdfName.K);

            if (k != null)
            {
                if (k.IsArray())
                {
                    PdfArray kArr   = (PdfArray)k;
                    PdfArray newArr = new PdfArray();
                    for (int i = 0; i < kArr.Size(); i++)
                    {
                        PdfObject copiedKid = CopyObjectKid(kArr.Get(i), copied, destPage, parentChangePg, copyingParams);
                        if (copiedKid != null)
                        {
                            newArr.Add(copiedKid);
                        }
                    }
                    if (!newArr.IsEmpty())
                    {
                        if (newArr.Size() == 1)
                        {
                            copied.Put(PdfName.K, newArr.Get(0));
                        }
                        else
                        {
                            copied.Put(PdfName.K, newArr);
                        }
                    }
                }
                else
                {
                    PdfObject copiedKid = CopyObjectKid(k, copied, destPage, parentChangePg, copyingParams);
                    if (copiedKid != null)
                    {
                        copied.Put(PdfName.K, copiedKid);
                    }
                }
            }
            return(copied);
        }