/// <exception cref="System.IO.IOException"/> protected internal virtual void ParseTag(PdfMcr kid) { int mcid = kid.GetMcid(); PdfDictionary pageDic = kid.GetPageObject(); String tagContent = ""; if (mcid != -1) { if (!parsedTags.ContainsKey(pageDic)) { TaggedPdfReaderTool.MarkedContentEventListener listener = new TaggedPdfReaderTool.MarkedContentEventListener (this); PdfCanvasProcessor processor = new PdfCanvasProcessor(listener); PdfPage page = document.GetPage(pageDic); processor.ProcessContent(page.GetContentBytes(), page.GetResources()); parsedTags[pageDic] = listener.GetMcidContent(); } if (parsedTags.Get(pageDic).ContainsKey(mcid)) { tagContent = parsedTags.Get(pageDic).Get(mcid); } } else { PdfObjRef objRef = (PdfObjRef)kid; PdfObject @object = objRef.GetReferencedObject(); if (@object.IsDictionary()) { PdfName subtype = ((PdfDictionary)@object).GetAsName(PdfName.Subtype); tagContent = subtype.ToString(); } } @out.Write(EscapeXML(tagContent, true)); }
/// <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); }
/// <summary>Removes content item from the tag structure.</summary> /// <remarks> /// Removes content item from the tag structure. /// <br /> /// Nothing happens if there is no such mcid on given page. /// </remarks> /// <param name="page">page, which contains this content item</param> /// <param name="mcid">marked content id of this content item</param> /// <returns> /// /// <c>TagTreePointer</c> /// which points at the parent of the removed content item, or null if there is no /// such mcid on given page. /// </returns> public virtual TagTreePointer RemoveContentItem(PdfPage page, int mcid) { PdfMcr mcr = document.GetStructTreeRoot().FindMcrByMcid(page.GetPdfObject(), mcid); if (mcr == null) { return(null); } PdfStructElem parent = (PdfStructElem)mcr.GetParent(); parent.RemoveKid(mcr); return(new TagTreePointer(document).SetCurrentStructElem(parent)); }
/// <summary> /// Moves kid of the current tag to the tag at which given /// <c>TagTreePointer</c> /// points. /// This method doesn't change neither this instance nor 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="TagTreePointer"/> /// 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); } if (GetCurrentStructElem().IsFlushed()) { throw new PdfException(PdfException.CannotRelocateTagWhichParentIsAlreadyFlushed); } if (IsPointingToSameTag(pointerToNewParent)) { if (kidIndex == pointerToNewParent.nextNewKidIndex) { return(this); } else { if (kidIndex < pointerToNewParent.nextNewKidIndex) { pointerToNewParent.SetNextNewKidIndex(pointerToNewParent.nextNewKidIndex - 1); } } } if (GetCurrentStructElem().GetKids()[kidIndex] == null) { throw new PdfException(PdfException.CannotRelocateTagWhichIsAlreadyFlushed); } IStructureNode removedKid = GetCurrentStructElem().RemoveKid(kidIndex, true); 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); }
/// <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 the tag if it's parent is already 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() { PdfStructElem currentStructElem = GetCurrentStructElem(); IStructureNode parentElem = currentStructElem.GetParent(); if (parentElem is PdfStructTreeRoot) { throw new PdfException(PdfException.CannotRemoveDocumentRootTag); } IList <IStructureNode> kids = currentStructElem.GetKids(); PdfStructElem parent = (PdfStructElem)parentElem; if (parent.IsFlushed()) { throw new PdfException(PdfException.CannotRemoveTagBecauseItsParentIsFlushed); } // remove waiting tag state if tag is removed Object objForStructDict = tagStructureContext.GetWaitingTagsManager().GetObjForStructDict(currentStructElem .GetPdfObject()); tagStructureContext.GetWaitingTagsManager().RemoveWaitingState(objForStructDict); int removedKidIndex = parent.RemoveKid(currentStructElem); PdfIndirectReference indRef = currentStructElem.GetPdfObject().GetIndirectReference(); if (indRef != null) { // TODO how about possible references to structure element from refs or structure destination for instance? indRef.SetFree(); } foreach (IStructureNode kid in kids) { if (kid is PdfStructElem) { parent.AddKid(removedKidIndex++, (PdfStructElem)kid); } else { PdfMcr mcr = PrepareMcrForMovingToNewParent((PdfMcr)kid, parent); parent.AddKid(removedKidIndex++, mcr); } } currentStructElem.GetPdfObject().Clear(); SetCurrentStructElem(parent); return(this); }
private PdfMcr PrepareMcrForMovingToNewParent(PdfMcr mcrKid, PdfStructElem newParent) { PdfObject mcrObject = mcrKid.GetPdfObject(); PdfDictionary mcrPage = mcrKid.GetPageObject(); PdfDictionary mcrDict = null; if (!mcrObject.IsNumber()) { mcrDict = (PdfDictionary)mcrObject; } if (mcrDict == null || !mcrDict.ContainsKey(PdfName.Pg)) { if (!EnsureElementPageEqualsKidPage(newParent, mcrPage)) { if (mcrDict == null) { mcrDict = new PdfDictionary(); mcrDict.Put(PdfName.Type, PdfName.MCR); mcrDict.Put(PdfName.MCID, mcrKid.GetPdfObject()); } // Explicitly using object indirect reference here in order to correctly process released objects. mcrDict.Put(PdfName.Pg, mcrPage.GetIndirectReference()); } } if (mcrDict != null) { if (PdfName.MCR.Equals(mcrDict.Get(PdfName.Type))) { mcrKid = new PdfMcrDictionary(mcrDict, newParent); } else { if (PdfName.OBJR.Equals(mcrDict.Get(PdfName.Type))) { mcrKid = new PdfObjRef(mcrDict, newParent); } } } else { mcrKid = new PdfMcrNumber((PdfNumber)mcrObject, newParent); } return(mcrKid); }
private PdfMcr PrepareMcrForMovingToNewParent(PdfMcr mcrKid, PdfStructElem newParent) { PdfObject mcrObject = mcrKid.GetPdfObject(); PdfDictionary mcrPage = mcrKid.GetPageObject(); PdfDictionary mcrDict = null; if (!mcrObject.IsNumber()) { mcrDict = (PdfDictionary)mcrObject; } if (mcrDict == null || !mcrDict.ContainsKey(PdfName.Pg)) { if (!EnsureElementPageEqualsKidPage(newParent, mcrPage)) { if (mcrDict == null) { mcrDict = new PdfDictionary(); mcrDict.Put(PdfName.Type, PdfName.MCR); mcrDict.Put(PdfName.MCID, mcrKid.GetPdfObject()); } mcrDict.Put(PdfName.Pg, mcrPage); } } if (mcrDict != null) { if (PdfName.MCR.Equals(mcrDict.Get(PdfName.Type))) { mcrKid = new PdfMcrDictionary(mcrDict, newParent); } else { if (PdfName.OBJR.Equals(mcrDict.Get(PdfName.Type))) { mcrKid = new PdfObjRef(mcrDict, newParent); } } } else { mcrKid = new PdfMcrNumber((PdfNumber)mcrObject, newParent); } return(mcrKid); }
/// <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); }
private PdfMcr AddNewKid(PdfMcr kid) { return(GetCurrentElemEnsureIndirect().AddKid(GetNextNewKidPosition(), kid)); }
/// <summary>Creates a tag that is referenced to the document's tag structure (i.e.</summary> /// <remarks> /// Creates a tag that is referenced to the document's tag structure (i.e. /// logical structure). /// </remarks> /// <param name="mcr"> /// the /// <see cref="iText.Kernel.Pdf.Tagging.PdfMcr">Marked Content Reference</see> /// wrapper object /// </param> public CanvasTag(PdfMcr mcr) : this(mcr.GetRole(), mcr.GetMcid()) { }
private PdfMcr AddNewKid(PdfMcr kid) { return(GetCurrentStructElem().AddKid(GetNextNewKidPosition(), kid)); }