0
votes

Generated accessible pdf using pdfHTMl add-on in iText 7. To add the link to the pdf used below code in HTML file,

<a href="www.google.com" title="web">www.google.com</a>

C# code as below:

IList<IElement> elements = HtmlConverter.ConvertToElements(htmlFile,converterProperties);
foreach(IElement element in elements){
    doc.Add((IBlockElement)element);
}

link was appeared in the pdf as expected. PAC tool gives error saying "Alternative description missing for annotation". I saw same issue already raised here. Fixing a PDF Accessibility Issue (Alternative description missing for an annotation) when converting an HTML Page to PDF and

Fixing link error, pdfHTML

But there are not mentioned what is the extract answer for this. That is why I'm raised new one. I tired out to create custom tag using aTagWorker. But element is appeared as JSoupElementNode in ProcessEnd method. How to set accessible properties for JSoupElementNode type of elements? Please help me to resolve this issue. Thanks

1

1 Answers

1
votes

You're on the right track. Typically the getElementResult() function can be overridden to get an object that you can add accessibility attributes to. Links are a bit of a special case because there are both objects like Paragraphs and annotations (a clickable box that overlaps, but isn't directly related to the Text). This means you have to go through the processsEnd() function. At that point getAllElements() will return the sub-elements of the link.

Here is the solution I came up with when working with someone else. Note that it assumes sub-elements of the link are Text elements which is true in the typical case, but not necessarily in every case.

Set the HtmlWorker to use your new ATagWorker

ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setTagWorkerFactory(new DefaultTagWorkerFactory() {
    @Override
    public ITagWorker getCustomTagWorker(
            IElementNode tag, ProcessorContext context) {
        if ("a".equalsIgnoreCase(tag.name())) {
            return new AccessibleATagWorker(tag, context);
        }
        return null;
    }
});

...
HtmlConverter.convertToPdf( ... , ... , converterProperties);

And the custom A tag Worker:

class AccessibleATagWorker extends ATagWorker {

    private String ALTERNATE_DESCRIPTION;

    public AccessibleATagWorker(IElementNode element, ProcessorContext context) {
        super(element, context);
        ALTERNATE_DESCRIPTION = element.getAttribute("title");
    }

    @Override
    public void processEnd(IElementNode element, ProcessorContext context) {
        super.processEnd(element, context);
        List < IPropertyContainer > containedElements = this.getAllElements();
        for (int x = 0; x < containedElements.size(); x++) {
            if (containedElements.get(x) instanceof Text) {
                ((Text) containedElements.get(x)).getAccessibilityProperties().setAlternateDescription(ALTERNATE_DESCRIPTION);
            }
        }


    }

}