Пример #1
0
 private static PdfObject CopyObjectKid(PdfObject kid, PdfDictionary copiedParent, PdfDictionary destPage,
                                        bool parentChangePg, StructureTreeCopier.StructElemCopyingParams copyingParams)
 {
     if (kid.IsNumber())
     {
         if (!parentChangePg)
         {
             copyingParams.GetToDocument().GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(new PdfMcrNumber((PdfNumber
                                                                                                                    )kid, new PdfStructElem(copiedParent)));
             return(kid);
         }
     }
     else
     {
         if (kid.IsDictionary())
         {
             PdfDictionary kidAsDict = (PdfDictionary)kid;
             if (copyingParams.GetObjectsToCopy().Contains(kidAsDict))
             {
                 bool          hasParent = kidAsDict.ContainsKey(PdfName.P);
                 PdfDictionary copiedKid = CopyObject(kidAsDict, destPage, parentChangePg, copyingParams);
                 if (hasParent)
                 {
                     copiedKid.Put(PdfName.P, copiedParent);
                 }
                 else
                 {
                     PdfMcr mcr;
                     if (copiedKid.ContainsKey(PdfName.Obj))
                     {
                         mcr = new PdfObjRef(copiedKid, new PdfStructElem(copiedParent));
                         PdfDictionary contentItemObject = copiedKid.GetAsDictionary(PdfName.Obj);
                         if (PdfName.Link.Equals(contentItemObject.GetAsName(PdfName.Subtype)) && !contentItemObject.ContainsKey(PdfName
                                                                                                                                 .P))
                         {
                             // Some link annotations may be not copied, because their destination page is not copied.
                             return(null);
                         }
                         contentItemObject.Put(PdfName.StructParent, new PdfNumber((int)copyingParams.GetToDocument().GetNextStructParentIndex
                                                                                       ()));
                     }
                     else
                     {
                         mcr = new PdfMcrDictionary(copiedKid, new PdfStructElem(copiedParent));
                     }
                     copyingParams.GetToDocument().GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(mcr);
                 }
                 return(copiedKid);
             }
         }
     }
     return(null);
 }
Пример #2
0
 private static PdfObject CopyObjectKid(PdfObject kid, PdfDictionary copiedParent, ICollection <PdfObject> objectsToCopy
                                        , PdfDocument toDocument, IDictionary <PdfDictionary, PdfDictionary> page2page, bool copyFromDestDocument
                                        )
 {
     if (kid.IsNumber())
     {
         toDocument.GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(new PdfMcrNumber((PdfNumber)kid, new PdfStructElem
                                                                                                (copiedParent)));
         return(kid);
     }
     else
     {
         // TODO do we always copy numbers? don't we need to check if it is supposed to be copied like objs in objectsToCopy?
         if (kid.IsDictionary())
         {
             PdfDictionary kidAsDict = (PdfDictionary)kid;
             if (objectsToCopy.Contains(kidAsDict))
             {
                 bool          hasParent = kidAsDict.ContainsKey(PdfName.P);
                 PdfDictionary copiedKid = CopyObject(kidAsDict, objectsToCopy, toDocument, page2page, copyFromDestDocument
                                                      );
                 if (hasParent)
                 {
                     copiedKid.Put(PdfName.P, copiedParent);
                 }
                 else
                 {
                     PdfMcr mcr;
                     if (copiedKid.ContainsKey(PdfName.Obj))
                     {
                         mcr = new PdfObjRef(copiedKid, new PdfStructElem(copiedParent));
                         PdfDictionary contentItemObject = copiedKid.GetAsDictionary(PdfName.Obj);
                         if (PdfName.Link.Equals(contentItemObject.GetAsName(PdfName.Subtype)) && !contentItemObject.ContainsKey(PdfName
                                                                                                                                 .P))
                         {
                             // Some link annotations may be not copied, because their destination page is not copied.
                             return(null);
                         }
                         contentItemObject.Put(PdfName.StructParent, new PdfNumber((int)toDocument.GetNextStructParentIndex()));
                     }
                     else
                     {
                         mcr = new PdfMcrDictionary(copiedKid, new PdfStructElem(copiedParent));
                     }
                     toDocument.GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(mcr);
                 }
                 return(copiedKid);
             }
         }
     }
     return(null);
 }
Пример #3
0
        private IPdfStructElem ConvertPdfObjectToIPdfStructElem(PdfObject obj)
        {
            if (obj.IsIndirectReference())
            {
                obj = ((PdfIndirectReference)obj).GetRefersTo();
            }
            IPdfStructElem elem = null;

            switch (obj.GetObjectType())
            {
            case PdfObject.DICTIONARY: {
                PdfDictionary d = (PdfDictionary)obj;
                if (IsStructElem(d))
                {
                    elem = new iText.Kernel.Pdf.Tagging.PdfStructElem(d);
                }
                else
                {
                    if (PdfName.MCR.Equals(d.GetAsName(PdfName.Type)))
                    {
                        elem = new PdfMcrDictionary(d, this);
                    }
                    else
                    {
                        if (PdfName.OBJR.Equals(d.GetAsName(PdfName.Type)))
                        {
                            elem = new PdfObjRef(d, this);
                        }
                    }
                }
                break;
            }

            case PdfObject.NUMBER: {
                elem = new PdfMcrNumber((PdfNumber)obj, this);
                break;
            }

            default: {
                break;
            }
            }
            return(elem);
        }
Пример #4
0
        private static void SeparateKids(PdfDictionary structElem, ICollection <PdfObject> firstPartElems, StructureTreeCopier.LastClonedAncestor
                                         lastCloned)
        {
            PdfObject k = structElem.Get(PdfName.K);

            // If /K entry is not a PdfArray - it would be a kid which we won't clone at the moment, because it won't contain
            // kids from both parts at the same time. It would either be cloned as an ancestor later, or not cloned at all.
            // If it's kid is struct elem - it would definitely be structElem from the first part, so we simply call separateKids for it.
            if (!k.IsArray())
            {
                if (k.IsDictionary() && PdfStructElem.IsStructElem((PdfDictionary)k))
                {
                    SeparateKids((PdfDictionary)k, firstPartElems, lastCloned);
                }
            }
            else
            {
                PdfDocument document = structElem.GetIndirectReference().GetDocument();
                PdfArray    kids     = (PdfArray)k;
                for (int i = 0; i < kids.Size(); ++i)
                {
                    PdfObject     kid     = kids.Get(i);
                    PdfDictionary dictKid = null;
                    if (kid.IsDictionary())
                    {
                        dictKid = (PdfDictionary)kid;
                    }
                    if (dictKid != null && PdfStructElem.IsStructElem(dictKid))
                    {
                        if (firstPartElems.Contains(kid))
                        {
                            SeparateKids((PdfDictionary)kid, firstPartElems, lastCloned);
                        }
                        else
                        {
                            if (dictKid.IsFlushed())
                            {
                                throw new PdfException(PdfException.TagFromTheExistingTagStructureIsFlushedCannotAddCopiedPageTags);
                            }
                            // elems with no kids will not be marked as from the first part,
                            // but nonetheless we don't want to move all of them to the second part; we just leave them as is
                            if (dictKid.ContainsKey(PdfName.K))
                            {
                                CloneParents(structElem, lastCloned, document);
                                kids.Remove(i--);
                                PdfStructElem.AddKidObject(lastCloned.clone, -1, kid);
                            }
                        }
                    }
                    else
                    {
                        if (!firstPartElems.Contains(kid))
                        {
                            CloneParents(structElem, lastCloned, document);
                            PdfMcr mcr;
                            if (dictKid != null)
                            {
                                if (dictKid.Get(PdfName.Type).Equals(PdfName.MCR))
                                {
                                    mcr = new PdfMcrDictionary(dictKid, new PdfStructElem(lastCloned.clone));
                                }
                                else
                                {
                                    mcr = new PdfObjRef(dictKid, new PdfStructElem(lastCloned.clone));
                                }
                            }
                            else
                            {
                                mcr = new PdfMcrNumber((PdfNumber)kid, new PdfStructElem(lastCloned.clone));
                            }
                            kids.Remove(i--);
                            PdfStructElem.AddKidObject(lastCloned.clone, -1, kid);
                            document.GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(mcr);
                        }
                    }
                }
            }
            // re-register mcr
            if (lastCloned.ancestor == structElem)
            {
                lastCloned.ancestor = lastCloned.ancestor.GetAsDictionary(PdfName.P);
                lastCloned.clone    = lastCloned.clone.GetAsDictionary(PdfName.P);
            }
        }