iText 7 PDF accessibility: “Table header cell has no associated subcells”

后端 未结 1 681
野趣味
野趣味 2021-01-07 12:12

I am converting HTML to a PDF using iText 7. I need the PDF to be accessible (508 compliant with appropriate tags, etc), but, no matter what markup I put on a table, accessi

相关标签:
1条回答
  • 2021-01-07 12:41

    After working with Jon Reilly on the iText team, this was the final solution that worked for me (column IDs and associated headers aren't needed... just Scope)

    public class ThWithScopeTagWorker : ThTagWorker
    {
        public ThWithScopeTagWorker(IElementNode element, ProcessorContext context) : base(element, context)
        {
        }
    
        public override void ProcessEnd(IElementNode element, ProcessorContext context)
        {
            base.ProcessEnd(element, context);
            IPropertyContainer elementResult = base.GetElementResult();
            if (elementResult is IAccessibleElement)
            {
                ((IAccessibleElement)elementResult).GetAccessibilityProperties().SetRole(StandardRoles.TH);
    
                //Can use this in the future in case we have th elements with different scope than "col"
                string htmlScope = element.GetAttribute("scope"); //This is the scope="XXX" in your HTML
    
                AccessibilityProperties properties = ((IAccessibleElement)elementResult).GetAccessibilityProperties();
                //Could add "Row" if needed based on htmlScope string above. 
                //For my purposes, all th elements were scope="col"
                properties.AddAttributes(new PdfStructureAttributes("Table").AddEnumAttribute("Scope", "Column"));
            }
        }
    
    }
    

    and this:

    public class AccessibilityTagWorkerFactory : DefaultTagWorkerFactory
    {
        public override ITagWorker GetCustomTagWorker(IElementNode tag, ProcessorContext context)
        {    
             //...        
            if (tag.Name() == "th")
            {
                return new ThWithScopeTagWorker(tag, context);
            }
            return base.GetCustomTagWorker(tag, context);
        }
    }
    
    0 讨论(0)
提交回复
热议问题