Esempio n. 1
0
        /// <summary>Removes the current tag.</summary>
        /// <remarks>
        /// Removes the current tag. If it has kids, they will become kids of the current tag parent.
        /// This method call moves this
        /// <c>TagTreePointer</c>
        /// to the current tag parent.
        /// <br/><br/>
        /// You cannot remove root tag, and also you cannot remove any tag if document's tag structure was partially flushed;
        /// in this two cases an exception will be thrown.
        /// </remarks>
        /// <returns>
        /// this
        /// <see cref="TagStructureContext"/>
        /// instance.
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagutils.TagTreePointer RemoveTag()
        {
            IPdfStructElem parentElem = GetCurrentStructElem().GetParent();

            if (parentElem is PdfStructTreeRoot)
            {
                throw new PdfException(PdfException.CannotRemoveDocumentRootTag);
            }
            IList <IPdfStructElem> kids   = GetCurrentStructElem().GetKids();
            PdfStructElem          parent = (PdfStructElem)parentElem;

            if (parent.IsFlushed())
            {
                throw new PdfException(PdfException.CannotRemoveTagBecauseItsParentIsFlushed);
            }
            int removedKidIndex = parent.RemoveKid(GetCurrentStructElem());

            GetCurrentStructElem().GetPdfObject().GetIndirectReference().SetFree();
            foreach (IPdfStructElem kid in kids)
            {
                if (kid is PdfStructElem)
                {
                    parent.AddKid(removedKidIndex++, (PdfStructElem)kid);
                }
                else
                {
                    PdfMcr mcr = PrepareMcrForMovingToNewParent((PdfMcr)kid, parent);
                    parent.AddKid(removedKidIndex++, mcr);
                }
            }
            SetCurrentStructElem(parent);
            return(this);
        }
Esempio n. 2
0
        public virtual IPdfStructElem RemoveKid(int index)
        {
            PdfObject k = GetK();

            if (k == null || !k.IsArray() && index != 0)
            {
                throw new IndexOutOfRangeException();
            }
            if (k.IsArray())
            {
                PdfArray kidsArray = (PdfArray)k;
                k = kidsArray.Get(index);
                kidsArray.Remove(index);
                if (kidsArray.IsEmpty())
                {
                    GetPdfObject().Remove(PdfName.K);
                }
            }
            else
            {
                GetPdfObject().Remove(PdfName.K);
            }
            IPdfStructElem removedKid = ConvertPdfObjectToIPdfStructElem(k);

            if (removedKid is PdfMcr)
            {
                GetDocument().GetStructTreeRoot().GetParentTreeHandler().UnregisterMcr((PdfMcr)removedKid);
            }
            return(removedKid);
        }
Esempio n. 3
0
 private void RemovePageTagFromParent(IPdfStructElem pageTag, IPdfStructElem parent)
 {
     if (parent is PdfStructElem)
     {
         PdfStructElem structParent = (PdfStructElem)parent;
         if (!structParent.IsFlushed())
         {
             structParent.RemoveKid(pageTag);
             PdfDictionary parentObject = structParent.GetPdfObject();
             if (!connectedStructToModel.ContainsKey(parentObject) && parent.GetKids().Count == 0 && parentObject != rootTagElement
                 .GetPdfObject())
             {
                 RemovePageTagFromParent(structParent, parent.GetParent());
                 parentObject.GetIndirectReference().SetFree();
             }
         }
         else
         {
             if (pageTag is PdfMcr)
             {
                 throw new PdfException(PdfException.CannotRemoveTagBecauseItsParentIsFlushed);
             }
         }
     }
 }
Esempio n. 4
0
 private void FlushAllKids(IPdfStructElem elem)
 {
     foreach (IPdfStructElem kid in elem.GetKids())
     {
         if (kid is PdfStructElem)
         {
             FlushAllKids(kid);
             ((PdfStructElem)kid).Flush();
         }
     }
 }
Esempio n. 5
0
        /// <returns>parent of the flushed tag</returns>
        internal virtual IPdfStructElem FlushTag(PdfStructElem tagStruct)
        {
            IAccessibleElement modelElement = connectedStructToModel.JRemove(tagStruct.GetPdfObject());

            if (modelElement != null)
            {
                connectedModelToStruct.JRemove(modelElement);
            }
            IPdfStructElem parent = tagStruct.GetParent();

            FlushStructElementAndItKids(tagStruct);
            return(parent);
        }
Esempio n. 6
0
 public virtual int RemoveKid(IPdfStructElem kid)
 {
     if (kid is PdfMcr)
     {
         PdfMcr mcr = (PdfMcr)kid;
         GetDocument().GetStructTreeRoot().GetParentTreeHandler().UnregisterMcr(mcr);
         return(RemoveKidObject(mcr.GetPdfObject()));
     }
     else
     {
         if (kid is iText.Kernel.Pdf.Tagging.PdfStructElem)
         {
             return(RemoveKidObject(((iText.Kernel.Pdf.Tagging.PdfStructElem)kid).GetPdfObject()));
         }
     }
     return(-1);
 }
Esempio n. 7
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);
        }
Esempio n. 8
0
        // it is StructTreeRoot
        // should never happen as we always should have only one root tag and we don't remove it
        private void FlushParentIfBelongsToPage(PdfStructElem parent, PdfPage currentPage)
        {
            if (parent.IsFlushed() || connectedStructToModel.ContainsKey(parent.GetPdfObject()) || parent.GetPdfObject
                    () == rootTagElement.GetPdfObject())
            {
                return;
            }
            IList <IPdfStructElem> kids = parent.GetKids();
            bool allKidsBelongToPage    = true;

            foreach (IPdfStructElem kid in kids)
            {
                if (kid is PdfMcr)
                {
                    PdfDictionary kidPage = ((PdfMcr)kid).GetPageObject();
                    if (!kidPage.IsFlushed() && !kidPage.Equals(currentPage.GetPdfObject()))
                    {
                        allKidsBelongToPage = false;
                        break;
                    }
                }
                else
                {
                    if (kid is PdfStructElem)
                    {
                        // If kid is structElem and was already flushed then in kids list there will be null for it instead of
                        // PdfStructElem. And therefore if we get into this if clause it means that some StructElem wasn't flushed.
                        allKidsBelongToPage = false;
                        break;
                    }
                }
            }
            if (allKidsBelongToPage)
            {
                IPdfStructElem parentsParent = parent.GetParent();
                parent.Flush();
                if (parentsParent is PdfStructElem)
                {
                    FlushParentIfBelongsToPage((PdfStructElem)parentsParent, currentPage);
                }
            }
            return;
        }
Esempio n. 9
0
        /// <summary>Flushes the current tag and all it's descenders.</summary>
        /// <remarks>
        /// Flushes the current tag and all it's descenders.
        /// This method call moves this
        /// <c>TagTreePointer</c>
        /// to the current tag parent.
        /// <p>
        /// <br /><br />
        /// If some of the tags to be flushed are still connected to the accessible elements, then these tags are considered
        /// as not yet finished ones, and they won't be flushed immediately, but they will be flushed, when the connection
        /// is removed.
        /// </remarks>
        /// <returns>
        /// this
        /// <see cref="TagStructureContext"/>
        /// instance.
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagutils.TagTreePointer FlushTag()
        {
            if (GetCurrentStructElem().GetPdfObject() == tagStructureContext.GetRootTag().GetPdfObject())
            {
                throw new PdfException(PdfException.CannotFlushDocumentRootTagBeforeDocumentIsClosed);
            }
            IPdfStructElem parent = tagStructureContext.FlushTag(GetCurrentStructElem());

            if (parent != null)
            {
                // parent is not flushed
                SetCurrentStructElem((PdfStructElem)parent);
            }
            else
            {
                SetCurrentStructElem(tagStructureContext.GetRootTag());
            }
            return(this);
        }
Esempio n. 10
0
        /// <summary>
        /// Moves this
        /// <c>TagTreePointer</c>
        /// instance to the parent of the current tag.
        /// </summary>
        /// <returns>
        /// this
        /// <see cref="TagStructureContext"/>
        /// instance.
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagutils.TagTreePointer MoveToParent()
        {
            if (GetCurrentStructElem().GetPdfObject() == tagStructureContext.GetRootTag().GetPdfObject())
            {
                throw new PdfException(PdfException.CannotMoveToParentCurrentElementIsRoot);
            }
            IPdfStructElem parent = GetCurrentStructElem().GetParent();

            if (parent == null)
            {
                //TODO log that parent is flushed
                MoveToRoot();
            }
            else
            {
                SetCurrentStructElem((PdfStructElem)parent);
            }
            return(this);
        }
Esempio n. 11
0
 protected internal virtual void InspectKid(IPdfStructElem kid)
 {
     try {
         if (kid is PdfStructElem)
         {
             PdfStructElem structElemKid = (PdfStructElem)kid;
             PdfName       s             = structElemKid.GetRole();
             String        tagN          = s.GetValue();
             String        tag           = FixTagName(tagN);
             @out.Write("<");
             @out.Write(tag);
             InspectAttributes(structElemKid);
             @out.Write(">" + Environment.NewLine);
             PdfString alt = (structElemKid).GetAlt();
             if (alt != null)
             {
                 @out.Write("<alt><![CDATA[");
                 @out.Write(iText.IO.Util.StringUtil.ReplaceAll(alt.GetValue(), "[\\000]*", ""));
                 @out.Write("]]></alt>" + Environment.NewLine);
             }
             InspectKids(structElemKid.GetKids());
             @out.Write("</");
             @out.Write(tag);
             @out.Write(">" + Environment.NewLine);
         }
         else
         {
             if (kid is PdfMcr)
             {
                 ParseTag((PdfMcr)kid);
             }
             else
             {
                 @out.Write(" <flushedKid/> ");
             }
         }
     }
     catch (System.IO.IOException e) {
         throw new iText.IO.IOException(iText.IO.IOException.UnknownIOException, e);
     }
 }
Esempio n. 12
0
        /// <summary>
        /// Moves this
        /// <c>TagTreePointer</c>
        /// instance to the kid of the current tag.
        /// </summary>
        /// <param name="kidIndex">zero-based index of the current tag kid to which pointer will be moved.</param>
        /// <returns>
        /// this
        /// <see cref="TagStructureContext"/>
        /// instance.
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagutils.TagTreePointer MoveToKid(int kidIndex)
        {
            IPdfStructElem kid = GetCurrentStructElem().GetKids()[kidIndex];

            if (kid is PdfStructElem)
            {
                SetCurrentStructElem((PdfStructElem)kid);
            }
            else
            {
                if (kid is PdfMcr)
                {
                    throw new PdfException(PdfException.CannotMoveToMarkedContentReference);
                }
                else
                {
                    throw new PdfException(PdfException.CannotMoveToFlushedKid);
                }
            }
            return(this);
        }
Esempio n. 13
0
        /// <summary>
        /// Moves kid of the current tag to the tag at which given
        /// <c>TagTreePointer</c>
        /// points.
        /// This method doesn't change pointerToNewParent position.
        /// </summary>
        /// <param name="kidIndex">zero-based index of the current tag's kid to be relocated.</param>
        /// <param name="pointerToNewParent">
        /// the
        /// <c>TagTreePointer</c>
        /// which is positioned at the tag which will become kid's new parent.
        /// </param>
        /// <returns>
        /// this
        /// <see cref="TagStructureContext"/>
        /// instance.
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagutils.TagTreePointer RelocateKid(int kidIndex, iText.Kernel.Pdf.Tagutils.TagTreePointer
                                                                            pointerToNewParent)
        {
            if (GetDocument() != pointerToNewParent.GetDocument())
            {
                throw new PdfException(PdfException.TagCannotBeMovedToTheAnotherDocumentsTagStructure);
            }
            IPdfStructElem removedKid = GetCurrentStructElem().RemoveKid(kidIndex);

            if (removedKid is PdfStructElem)
            {
                pointerToNewParent.AddNewKid((PdfStructElem)removedKid);
            }
            else
            {
                if (removedKid is PdfMcr)
                {
                    PdfMcr mcrKid = PrepareMcrForMovingToNewParent((PdfMcr)removedKid, pointerToNewParent.GetCurrentStructElem
                                                                       ());
                    pointerToNewParent.AddNewKid(mcrKid);
                }
            }
            return(this);
        }