/// <summary>Adds a new tag with given role to the tag structure.</summary>
 /// <remarks>
 /// Adds a new tag with given role to the tag structure.
 /// This method call moves this
 /// <c>TagTreePointer</c>
 /// to the added kid.
 /// <br />
 /// This call is equivalent of calling sequentially
 /// <see cref="SetNextNewKidIndex(int)"/>
 /// and
 /// <see cref="AddTag(System.String)"/>
 /// .
 /// </remarks>
 /// <param name="index">zero-based index in kids array of parent tag at which new tag will be added.</param>
 /// <param name="role">role of the new tag.</param>
 /// <returns>
 /// this
 /// <see cref="TagTreePointer"/>
 /// instance.
 /// </returns>
 public virtual iText.Kernel.Pdf.Tagutils.TagTreePointer AddTag(int index, String role)
 {
     tagStructureContext.ThrowExceptionIfRoleIsInvalid(role, currentNamespace);
     SetNextNewKidIndex(index);
     SetCurrentStructElem(AddNewKid(role));
     return(this);
 }
 /// <summary>Adds a new tag to the tag structure.</summary>
 /// <remarks>
 /// Adds a new tag to the tag structure.
 /// This method call moves this
 /// <c>TagTreePointer</c>
 /// to the added kid.
 /// <br/>
 /// New tag will have a role and attributes defined by the given IAccessibleElement.
 /// <br /><br />
 /// If
 ///
 /// is true then a newly created tag will retain the connection with given
 /// accessible element. See
 /// <see cref="MoveToTag(IAccessibleElement)"/>
 /// for more explanations about tag connections concept.
 /// <br/><br/>
 /// If the same accessible element is connected to the tag and is added twice to the same parent -
 /// this
 /// <c>TagTreePointer</c>
 /// instance would move to connected kid instead of creating tag twice.
 /// But if it is added to some other parent, then connection will be removed.
 /// <p>
 /// <br/><br/>
 /// This call is equivalent of calling sequentially
 /// <see cref="SetNextNewKidIndex(int)"/>
 /// and
 /// <see cref="AddTag(IAccessibleElement, bool)"/>
 /// .
 /// </remarks>
 /// <param name="index">zero-based index in kids array of parent tag at which new tag will be added.</param>
 /// <param name="element">accessible element which represents a new tag.</param>
 /// <param name="keepConnectedToTag">defines if to retain the connection between accessible element and the tag.
 ///     </param>
 /// <returns>
 /// this
 /// <see cref="TagTreePointer"/>
 /// instance.
 /// </returns>
 public virtual iText.Kernel.Pdf.Tagutils.TagTreePointer AddTag(int index, IAccessibleElement element, bool
                                                                keepConnectedToTag)
 {
     tagStructureContext.ThrowExceptionIfRoleIsInvalid(element.GetRole());
     if (!tagStructureContext.IsElementConnectedToTag(element))
     {
         SetNextNewKidIndex(index);
         SetCurrentStructElem(AddNewKid(element, keepConnectedToTag));
     }
     else
     {
         PdfStructElem connectedStruct = tagStructureContext.GetStructConnectedToModel(element);
         if (connectedStruct.GetParent() != null && GetCurrentStructElem().GetPdfObject() == ((PdfStructElem)connectedStruct
                                                                                              .GetParent()).GetPdfObject())
         {
             SetCurrentStructElem(connectedStruct);
         }
         else
         {
             tagStructureContext.RemoveElementConnectionToTag(element);
             SetNextNewKidIndex(index);
             SetCurrentStructElem(AddNewKid(element, keepConnectedToTag));
         }
     }
     return(this);
 }