简体   繁体   中英

iText7 Table Summary

I added table to the PDF using iText7 and pdfHTML add-on. To set the accessibility of the table, I want to set summary of the table. I found the same question in below one. But there isn't clear answer for this. How can I set table summary. Please advice.

iText7 508 Table Summary

I've just tried this:

PdfDictionary attr = new PdfDictionary();
attr.put(new PdfName("Summary"), new PdfString("Info about the table"));
table.getAccessibilityProperties().addAttributes(new PdfStructureAttributes(attr));

This seems to do the trick. Now you'd need to adapt the tag worker to make sure that this code is executed when a table tag is encountered.

Update 1:

I have taken the following HTML file:

<body>
<table summary="some keys and values">
<thead>
<tr><th scope="col">KEY</th><th scope="col">VALUE</th></tr>
</thead>
<tbody>
<tr><td>Color</td><td>Blue</td></tr>
<tr><td>Shape</td><td>Rectangle</td></tr>
<tr><td>Description</td><td>Blue rectangle</td></tr>
</tbody>
</table>
</body>

I have converted it to an accessible PDF document like this:

public void createPdf(String src, String dest) throws IOException {
    PdfWriter writer = new PdfWriter(dest,
        new WriterProperties().addUAXmpMetadata());
    PdfDocument pdf = new PdfDocument(writer);
    pdf.setTagged();
    pdf.getCatalog().setLang(new PdfString("en-US"));
    pdf.getCatalog().setViewerPreferences(
            new PdfViewerPreferences().setDisplayDocTitle(true));
    PdfDocumentInfo info = pdf.getDocumentInfo();
    info.setTitle("iText7 accessible tables");
    ConverterProperties properties = new ConverterProperties();
    FontProvider fontProvider = new DefaultFontProvider(false, true, false);
    properties.setFontProvider(fontProvider);
    HtmlConverter.convertToPdf(new FileInputStream(src), pdf, properties);
}

When checking the result with PAC3, I get the following result:

在此处输入图片说明

So far, so good, the PDF is considered an accessible PDF/UA file from a technical point of view.

Then I did the "human" check: is the table summary present? Unfortunately, it wasn't, so I looked into the code of the pdfHTML add-on, and I didn't find any reference to the summary attribute of the table tag. I think it was forgotten when pdfHTML was implemented.

In a first instance, I'll write a custom tag worker that takes care of adding the summary. Once this is done, I'll ask iText Group to implement the summary attribute so that it's added to one of the next releases.

Update 2:

I adapted my example like this:

public void createPdf(String src, String dest) throws IOException {
    PdfWriter writer = new PdfWriter(dest,
        new WriterProperties().addUAXmpMetadata());
    PdfDocument pdf = new PdfDocument(writer);
    pdf.setTagged();
    pdf.getCatalog().setLang(new PdfString("en-US"));
    pdf.getCatalog().setViewerPreferences(
            new PdfViewerPreferences().setDisplayDocTitle(true));
    PdfDocumentInfo info = pdf.getDocumentInfo();
    info.setTitle("iText7 accessible tables");
    ConverterProperties properties = new ConverterProperties();
    properties.setTagWorkerFactory(new AdaptedTagWorkerFactory());
    FontProvider fontProvider = new DefaultFontProvider(false, true, false);
    properties.setFontProvider(fontProvider);
    HtmlConverter.convertToPdf(new FileInputStream(src), pdf, properties);
}

class AdaptedTagWorkerFactory extends DefaultTagWorkerFactory {
    @Override
    public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
        if(tag.name().equals("table")){
            return new TableWithSummaryTagWorker(tag, context);
        }
        return null;
     }
}

class TableWithSummaryTagWorker extends TableTagWorker {

    private String summary = null;

    public TableWithSummaryTagWorker(IElementNode element, ProcessorContext context) {
        super(element, context);
    }

    @Override
    public void processEnd(IElementNode element, ProcessorContext context) {
        super.processEnd(element, context);
        summary = element.getAttribute("summary");
        if (summary != null) {
            Table table = (Table) super.getElementResult();
            PdfDictionary attr = new PdfDictionary();
            attr.put(new PdfName("Summary"), new PdfString(summary));
            table.getAccessibilityProperties().addAttributes(new PdfStructureAttributes(attr));
        }
    }
}

I ran it through PAC3, and it still validates as PDF/UA, but it doesn't mention the table summary anywhere. When I look inside the PDF, I can now see the summary:

在此处输入图片说明

I will now share this info with iText Group and ask them to check if my solution is correct (please add a comment if this didn't solve your problem). If it does, there's a high chance that this will be implemented starting with iText 7.1.4.

Update 3:

I have adapted my code based on the answer provided by the OP. There was one mistake in the OP's code. In that code, the /Summary is added as a PDF name, whereas it should be a PDF string.

public void createPdf(String src, String dest) throws IOException {
    PdfWriter writer = new PdfWriter(dest,
        new WriterProperties().addUAXmpMetadata());
    PdfDocument pdf = new PdfDocument(writer);
    pdf.setTagged();
    pdf.getCatalog().setLang(new PdfString("en-US"));
    pdf.getCatalog().setViewerPreferences(
            new PdfViewerPreferences().setDisplayDocTitle(true));
    PdfDocumentInfo info = pdf.getDocumentInfo();
    info.setTitle("iText7 accessibility example");
    ConverterProperties properties = new ConverterProperties();
    properties.setTagWorkerFactory(new AdaptedTagWorkerFactory());
    FontProvider fontProvider = new DefaultFontProvider(false, true, false);
    properties.setFontProvider(fontProvider);
    HtmlConverter.convertToPdf(new FileInputStream(src), pdf, properties);
}

class AdaptedTagWorkerFactory extends DefaultTagWorkerFactory {
    @Override
    public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
        if(tag.name().equals("table")){
            return new TableWithSummaryTagWorker(tag, context);
        }
        return null;
     }
}

class TableWithSummaryTagWorker extends TableTagWorker {

    private String summary = null;

    public TableWithSummaryTagWorker(IElementNode element, ProcessorContext context) {
        super(element, context);
    }

    @Override
    public void processEnd(IElementNode element, ProcessorContext context) {
        super.processEnd(element, context);
        IPropertyContainer elementResult = super.getElementResult();
        summary = element.getAttribute("summary");
        if (summary != null && elementResult instanceof IAccessibleElement) {
            AccessibilityProperties properties = ((IAccessibleElement)elementResult).getAccessibilityProperties();
            properties.addAttributes(new PdfStructureAttributes("Table").addTextAttribute("Summary", summary));
        }
    }
}

Now when you check the result, you get this report:

在此处输入图片说明

As you can see, the Summary test passes.

Thanks Bruno. I used C#. And ProcessEnd method should be changed as below.

public override void ProcessEnd(IElementNode element, ProcessorContext context)
    {
    base.ProcessEnd(element, context);
    IPropertyContainer elementResult = base.GetElementResult();
    if (elementResult is IAccessibleElement)
    {
    string summary= element.GetAttribute("summary"); //This is the summary="tbl summary" in HTML
    AccessibilityProperties properties = ((IAccessibleElement)elementResult).GetAccessibilityProperties();
    properties.AddAttributes(new PdfStructureAttributes("Table").AddEnumAttribute("Summary", summary));
    }
    }

To see the table titles , you have to open the pdf using Adobe Acrobat Professional. Right click on table and select "Edit table Summary". Title will display here.

需要了解如何使用Itext5.5.13解决相同的“摘要”问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM