public virtual float?GetAttributeAsFloat(String attributeName)
        {
            PdfName   name    = PdfStructTreeRoot.ConvertRoleToPdfName(attributeName);
            PdfNumber attrVal = GetPdfObject().GetAsNumber(name);

            return(attrVal != null ? (float?)attrVal.FloatValue() : (float?)null);
        }
        public virtual String GetAttributeAsText(String attributeName)
        {
            PdfName   name    = PdfStructTreeRoot.ConvertRoleToPdfName(attributeName);
            PdfString attrVal = GetPdfObject().GetAsString(name);

            return(attrVal != null?attrVal.ToUnicodeString() : null);
        }
        public virtual String GetAttributeAsEnum(String attributeName)
        {
            PdfName name    = PdfStructTreeRoot.ConvertRoleToPdfName(attributeName);
            PdfName attrVal = GetPdfObject().GetAsName(name);

            return(attrVal != null?attrVal.GetValue() : null);
        }
Example #4
0
 /// <summary>Init ParentTreeHandler.</summary>
 /// <remarks>Init ParentTreeHandler. On init the parent tree is read and stored in this instance.</remarks>
 internal ParentTreeHandler(PdfStructTreeRoot structTreeRoot)
 {
     this.structTreeRoot = structTreeRoot;
     parentTree          = new PdfNumTree(structTreeRoot.GetDocument().GetCatalog(), PdfName.ParentTree);
     RegisterAllMcrs();
     pageToStructParentsInd = new Dictionary <PdfIndirectReference, int?>();
 }
 private static void CopyTo(PdfDocument destDocument, IDictionary<PdfPage, PdfPage> page2page, PdfDocument 
     callingDocument, bool copyFromDestDocument, int insertIndex) {
     StructureTreeCopier.CopyStructureResult copiedStructure = CopyStructure(destDocument, page2page, callingDocument
         , copyFromDestDocument);
     PdfStructTreeRoot destStructTreeRoot = destDocument.GetStructTreeRoot();
     destStructTreeRoot.MakeIndirect(destDocument);
     foreach (PdfDictionary copied in copiedStructure.GetTopsList()) {
         destStructTreeRoot.AddKidObject(insertIndex, copied);
         if (insertIndex > -1) {
             ++insertIndex;
         }
     }
     if (!copyFromDestDocument) {
         if (!copiedStructure.GetCopiedNamespaces().IsEmpty()) {
             destStructTreeRoot.GetNamespacesObject().AddAll(copiedStructure.GetCopiedNamespaces());
         }
         PdfDictionary srcRoleMap = callingDocument.GetStructTreeRoot().GetRoleMap();
         PdfDictionary destRoleMap = destStructTreeRoot.GetRoleMap();
         foreach (KeyValuePair<PdfName, PdfObject> mappingEntry in srcRoleMap.EntrySet()) {
             if (!destRoleMap.ContainsKey(mappingEntry.Key)) {
                 destRoleMap.Put(mappingEntry.Key, mappingEntry.Value);
             }
             else {
                 if (!mappingEntry.Value.Equals(destRoleMap.Get(mappingEntry.Key))) {
                     String srcMapping = mappingEntry.Key + " -> " + mappingEntry.Value;
                     String destMapping = mappingEntry.Key + " -> " + destRoleMap.Get(mappingEntry.Key);
                     ILog logger = LogManager.GetLogger(typeof(StructureTreeCopier));
                     logger.Warn(String.Format(iText.IO.LogMessageConstant.ROLE_MAPPING_FROM_SOURCE_IS_NOT_COPIED_ALREADY_EXIST
                         , srcMapping, destMapping));
                 }
             }
         }
     }
 }
        public virtual iText.Kernel.Pdf.Tagging.PdfStructureAttributes RemoveAttribute(String attributeName)
        {
            PdfName name = PdfStructTreeRoot.ConvertRoleToPdfName(attributeName);

            GetPdfObject().Remove(name);
            SetModified();
            return(this);
        }
        public virtual iText.Kernel.Pdf.Tagging.PdfStructureAttributes AddFloatAttribute(String attributeName, float
                                                                                         attributeValue)
        {
            PdfName name = PdfStructTreeRoot.ConvertRoleToPdfName(attributeName);

            GetPdfObject().Put(name, new PdfNumber(attributeValue));
            SetModified();
            return(this);
        }
        public virtual iText.Kernel.Pdf.Tagging.PdfStructureAttributes AddTextAttribute(String attributeName, String
                                                                                        attributeValue)
        {
            PdfName name = PdfStructTreeRoot.ConvertRoleToPdfName(attributeName);

            GetPdfObject().Put(name, new PdfString(attributeValue, PdfEncodings.UNICODE_BIG));
            SetModified();
            return(this);
        }
Example #9
0
        /// <summary>
        /// Adds to the namespace role map (see
        /// <see cref="SetNamespaceRoleMap(iText.Kernel.Pdf.PdfDictionary)"/>
        /// ) a single role mapping to the
        /// default standard structure namespace.
        /// </summary>
        /// <param name="thisNsRole">
        /// a
        /// <see cref="System.String"/>
        /// identifying structure element type in this namespace.
        /// </param>
        /// <param name="defaultNsRole">
        /// a
        /// <see cref="System.String"/>
        /// identifying a structure element type in the default standard structure namespace.
        /// </param>
        /// <returns>
        /// this
        /// <see cref="PdfNamespace"/>
        /// instance.
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagging.PdfNamespace AddNamespaceRoleMapping(String thisNsRole, String defaultNsRole
                                                                                     )
        {
            PdfObject prevVal = GetNamespaceRoleMap(true).Put(PdfStructTreeRoot.ConvertRoleToPdfName(thisNsRole), PdfStructTreeRoot
                                                              .ConvertRoleToPdfName(defaultNsRole));

            LogOverwritingOfMappingIfNeeded(thisNsRole, prevVal);
            SetModified();
            return(this);
        }
Example #10
0
        /// <summary>
        /// Adds to the namespace role map (see
        /// <see cref="SetNamespaceRoleMap(iText.Kernel.Pdf.PdfDictionary)"/>
        /// ) a single role mapping to the
        /// target namespace.
        /// </summary>
        /// <param name="thisNsRole">
        /// a
        /// <see cref="System.String"/>
        /// identifying structure element type in this namespace.
        /// </param>
        /// <param name="targetNsRole">
        /// a
        /// <see cref="System.String"/>
        /// identifying a structure element type in the target namespace.
        /// </param>
        /// <param name="targetNs">
        /// a
        /// <see cref="PdfNamespace"/>
        /// identifying the target namespace.
        /// </param>
        /// <returns>
        /// this
        /// <see cref="PdfNamespace"/>
        /// instance.
        /// </returns>
        public virtual iText.Kernel.Pdf.Tagging.PdfNamespace AddNamespaceRoleMapping(String thisNsRole, String targetNsRole
                                                                                     , iText.Kernel.Pdf.Tagging.PdfNamespace targetNs)
        {
            PdfArray targetMapping = new PdfArray();

            targetMapping.Add(PdfStructTreeRoot.ConvertRoleToPdfName(targetNsRole));
            targetMapping.Add(targetNs.GetPdfObject());
            PdfObject prevVal = GetNamespaceRoleMap(true).Put(PdfStructTreeRoot.ConvertRoleToPdfName(thisNsRole), targetMapping
                                                              );

            LogOverwritingOfMappingIfNeeded(thisNsRole, prevVal);
            SetModified();
            return(this);
        }
 private static StructureTreeCopier.CopyStructureResult CopyStructure(PdfDocument destDocument, IDictionary
     <PdfPage, PdfPage> page2page, PdfDocument callingDocument, bool copyFromDestDocument) {
     PdfDocument fromDocument = copyFromDestDocument ? destDocument : callingDocument;
     IDictionary<PdfDictionary, PdfDictionary> topsToFirstDestPage = new Dictionary<PdfDictionary, PdfDictionary
         >();
     ICollection<PdfObject> objectsToCopy = new HashSet<PdfObject>();
     IDictionary<PdfDictionary, PdfDictionary> page2pageDictionaries = new Dictionary<PdfDictionary, PdfDictionary
         >();
     foreach (KeyValuePair<PdfPage, PdfPage> page in page2page) {
         page2pageDictionaries.Put(page.Key.GetPdfObject(), page.Value.GetPdfObject());
         ICollection<PdfMcr> mcrs = fromDocument.GetStructTreeRoot().GetPageMarkedContentReferences(page.Key);
         if (mcrs != null) {
             foreach (PdfMcr mcr in mcrs) {
                 if (mcr is PdfMcrDictionary || mcr is PdfObjRef) {
                     objectsToCopy.Add(mcr.GetPdfObject());
                 }
                 PdfDictionary top = AddAllParentsToSet(mcr, objectsToCopy);
                 if (top != null) {
                     if (top.IsFlushed()) {
                         throw new PdfException(PdfException.CannotCopyFlushedTag);
                     }
                     if (!topsToFirstDestPage.ContainsKey(top)) {
                         topsToFirstDestPage.Put(top, page.Value.GetPdfObject());
                     }
                 }
             }
         }
     }
     IList<PdfDictionary> topsInOriginalOrder = new List<PdfDictionary>();
     foreach (IStructureNode kid in fromDocument.GetStructTreeRoot().GetKids()) {
         if (kid == null) {
             continue;
         }
         PdfDictionary kidObject = ((PdfStructElem)kid).GetPdfObject();
         if (topsToFirstDestPage.ContainsKey(kidObject)) {
             topsInOriginalOrder.Add(kidObject);
         }
     }
     StructureTreeCopier.StructElemCopyingParams structElemCopyingParams = new StructureTreeCopier.StructElemCopyingParams
         (objectsToCopy, destDocument, page2pageDictionaries, copyFromDestDocument);
     PdfStructTreeRoot destStructTreeRoot = destDocument.GetStructTreeRoot();
     destStructTreeRoot.MakeIndirect(destDocument);
     IList<PdfDictionary> copiedTops = new List<PdfDictionary>();
     foreach (PdfDictionary top in topsInOriginalOrder) {
         PdfDictionary copied = CopyObject(top, topsToFirstDestPage.Get(top), false, structElemCopyingParams);
         copiedTops.Add(copied);
     }
     return new StructureTreeCopier.CopyStructureResult(copiedTops, structElemCopyingParams.GetCopiedNamespaces
         ());
 }
Example #12
0
        /// <returns>parent of the current structure element. Returns null if parent isn't set or if either current element or parent are invalid.
        ///     </returns>
        public virtual IStructureNode GetParent()
        {
            PdfDictionary parent = GetPdfObject().GetAsDictionary(PdfName.P);

            if (parent == null)
            {
                return(null);
            }
            if (parent.IsFlushed())
            {
                PdfDocument pdfDoc = GetDocument();
                if (pdfDoc == null)
                {
                    return(null);
                }
                PdfStructTreeRoot structTreeRoot = pdfDoc.GetStructTreeRoot();
                return(structTreeRoot.GetPdfObject() == parent ? (IStructureNode)structTreeRoot : new iText.Kernel.Pdf.Tagging.PdfStructElem
                           (parent));
            }
            if (IsStructElem(parent))
            {
                return(new iText.Kernel.Pdf.Tagging.PdfStructElem(parent));
            }
            else
            {
                PdfDocument pdfDoc       = GetDocument();
                bool        parentIsRoot = pdfDoc != null && PdfName.StructTreeRoot.Equals(parent.GetAsName(PdfName.Type));
                parentIsRoot = parentIsRoot || pdfDoc != null && pdfDoc.GetStructTreeRoot().GetPdfObject() == parent;
                if (parentIsRoot)
                {
                    return(pdfDoc.GetStructTreeRoot());
                }
                else
                {
                    return(null);
                }
            }
        }
Example #13
0
        /// <summary>
        /// Copies structure to a
        /// <paramref name="destDocument"/>
        /// and insert it in a specified position in the document.
        /// <br/><br/>
        /// NOTE: Works only for
        /// <c>PdfStructTreeRoot</c>
        /// that is read from the document opened in reading mode,
        /// otherwise an exception is thrown.
        /// <br/>
        /// Also, to insert a tagged page into existing tag structure, existing tag structure shouldn't be flushed, otherwise
        /// an exception may be raised.
        /// </summary>
        /// <param name="destDocument">document to copy structure to.</param>
        /// <param name="insertBeforePage">indicates where the structure to be inserted.</param>
        /// <param name="page2page">association between original page and copied page.</param>
        public static void CopyTo(PdfDocument destDocument, int insertBeforePage, IDictionary <PdfPage, PdfPage> page2page
                                  , PdfDocument callingDocument)
        {
            if (!destDocument.IsTagged())
            {
                return;
            }
            // Here we separate the structure tree in two parts: struct elems that belong to the pages which indexes are
            // less then insertBeforePage and those struct elems that belong to other pages. Some elems might belong
            // to both parts and actually these are the ones that we are looking for.
            ICollection <PdfObject> firstPartElems     = new HashSet <PdfObject>();
            PdfStructTreeRoot       destStructTreeRoot = destDocument.GetStructTreeRoot();

            for (int i = 1; i < insertBeforePage; ++i)
            {
                PdfPage pageOfFirstHalf       = destDocument.GetPage(i);
                ICollection <PdfMcr> pageMcrs = destStructTreeRoot.GetPageMarkedContentReferences(pageOfFirstHalf);
                if (pageMcrs != null)
                {
                    foreach (PdfMcr mcr in pageMcrs)
                    {
                        firstPartElems.Add(mcr.GetPdfObject());
                        PdfDictionary top = AddAllParentsToSet(mcr, firstPartElems);
                        if (top.IsFlushed())
                        {
                            throw new PdfException(PdfException.TagFromTheExistingTagStructureIsFlushedCannotAddCopiedPageTags);
                        }
                    }
                }
            }
            IList <PdfDictionary> clonedTops = new List <PdfDictionary>();
            PdfArray tops = destStructTreeRoot.GetKidsObject();
            // Now we "walk" through all the elems which belong to the first part, and look for the ones that contain both
            // kids from first and second part. We clone found elements and move kids from the second part to cloned elems.
            int lastTopBefore = 0;

            for (int i_1 = 0; i_1 < tops.Size(); ++i_1)
            {
                PdfDictionary top = tops.GetAsDictionary(i_1);
                if (firstPartElems.Contains(top))
                {
                    lastTopBefore = i_1;
                    StructureTreeCopier.LastClonedAncestor lastCloned = new StructureTreeCopier.LastClonedAncestor();
                    lastCloned.ancestor = top;
                    PdfDictionary topClone = top.Clone(ignoreKeysForClone);
                    topClone.Put(PdfName.P, destStructTreeRoot.GetPdfObject());
                    lastCloned.clone = topClone;
                    SeparateKids(top, firstPartElems, lastCloned);
                    if (topClone.ContainsKey(PdfName.K))
                    {
                        topClone.MakeIndirect(destDocument);
                        clonedTops.Add(topClone);
                    }
                }
            }
            for (int i_2 = 0; i_2 < clonedTops.Count; ++i_2)
            {
                destStructTreeRoot.AddKidObject(lastTopBefore + 1 + i_2, clonedTops[i_2]);
            }
            CopyTo(destDocument, page2page, callingDocument, false, lastTopBefore + 1);
        }
Example #14
0
 /// <summary>Init ParentTreeHandler.</summary>
 /// <remarks>Init ParentTreeHandler. On init the parent tree is read and stored in this instance.</remarks>
 internal ParentTreeHandler(PdfStructTreeRoot structTreeRoot)
 {
     this.structTreeRoot = structTreeRoot;
     parentTree          = new PdfNumTree(structTreeRoot.GetDocument().GetCatalog(), PdfName.ParentTree);
     RegisterAllMcrs();
 }
 public PdfStructureAttributes(String owner)
     : base(new PdfDictionary())
 {
     GetPdfObject().Put(PdfName.O, PdfStructTreeRoot.ConvertRoleToPdfName(owner));
 }