예제 #1
0
        private void SetNamespaceForNewTagsBasedOnExistingRoot()
        {
            IList <IStructureNode> rootKids = document.GetStructTreeRoot().GetKids();

            if (rootKids.Count > 0)
            {
                PdfStructElem        firstKid        = (PdfStructElem)rootKids[0];
                IRoleMappingResolver resolvedMapping = ResolveMappingToStandardOrDomainSpecificRole(firstKid.GetRole().GetValue
                                                                                                        (), firstKid.GetNamespace());
                if (resolvedMapping == null || !resolvedMapping.CurrentRoleIsStandard())
                {
                    ILog   logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.Tagutils.TagStructureContext));
                    String nsStr;
                    if (firstKid.GetNamespace() != null)
                    {
                        nsStr = firstKid.GetNamespace().GetNamespaceName();
                    }
                    else
                    {
                        nsStr = StandardNamespaces.GetDefault();
                    }
                    logger.Warn(String.Format(iText.IO.LogMessageConstant.EXISTING_TAG_STRUCTURE_ROOT_IS_NOT_STANDARD, firstKid
                                              .GetRole().GetValue(), nsStr));
                }
                if (resolvedMapping == null || !StandardNamespaces.PDF_1_7.Equals(resolvedMapping.GetNamespace().GetNamespaceName
                                                                                      ()))
                {
                    documentDefaultNamespace = FetchNamespace(StandardNamespaces.PDF_2_0);
                }
            }
            else
            {
                documentDefaultNamespace = FetchNamespace(StandardNamespaces.PDF_2_0);
            }
        }
예제 #2
0
        /// <summary>Transforms root tags in a way that complies with the tagged PDF specification.</summary>
        /// <remarks>
        /// Transforms root tags in a way that complies with the tagged PDF specification.
        /// Depending on PDF version behaviour may differ.
        /// <br />
        /// ISO 32000-1 (PDF 1.7 and lower)
        /// 14.8.4.2 Grouping Elements
        /// <br />
        /// "In a tagged PDF document, the structure tree shall contain a single top-level element; that is,
        /// the structure tree root (identified by the StructTreeRoot entry in the document catalogue) shall
        /// have only one child in its K (kids) array. If the PDF file contains a complete document, the structure
        /// type Document should be used for this top-level element in the logical structure hierarchy. If the file
        /// contains a well-formed document fragment, one of the structure types Part, Art, Sect, or Div may be used instead."
        /// <br />
        /// For PDF 2.0 and higher root tag is allowed to have only the Document role.
        /// </remarks>
        public virtual void NormalizeDocumentRootTag()
        {
            // in this method we could deal with existing document, so we don't won't to throw exceptions here
            bool forbid = forbidUnknownRoles;

            forbidUnknownRoles = false;
            IList <IStructureNode> rootKids = document.GetStructTreeRoot().GetKids();
            IRoleMappingResolver   mapping  = null;

            if (rootKids.Count > 0)
            {
                PdfStructElem firstKid = (PdfStructElem)rootKids[0];
                mapping = ResolveMappingToStandardOrDomainSpecificRole(firstKid.GetRole().GetValue(), firstKid.GetNamespace
                                                                           ());
            }
            if (rootKids.Count == 1 && mapping != null && mapping.CurrentRoleIsStandard() && IsRoleAllowedToBeRoot(mapping
                                                                                                                   .GetRole()))
            {
                rootTagElement = (PdfStructElem)rootKids[0];
            }
            else
            {
                document.GetStructTreeRoot().GetPdfObject().Remove(PdfName.K);
                rootTagElement = new RootTagNormalizer(this, rootTagElement, document).MakeSingleStandardRootTag(rootKids);
            }
            forbidUnknownRoles = forbid;
        }