Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iText7表格摘要_C#_Itext7 - Fatal编程技术网

C# iText7表格摘要

C# iText7表格摘要,c#,itext7,C#,Itext7,我使用iText7和pdfHTML附加组件将表添加到PDF中。要设置表的可访问性,我想设置表的摘要。我在下面的一个问题中发现了同样的问题。但这并没有明确的答案。如何设置表格摘要。请给我一些建议 我刚刚试过: PdfDictionary attr = new PdfDictionary(); attr.put(new PdfName("Summary"), new PdfString("Info about the table")); table.getAccessibilityPropertie

我使用iText7和pdfHTML附加组件将表添加到PDF中。要设置表的可访问性,我想设置表的摘要。我在下面的一个问题中发现了同样的问题。但这并没有明确的答案。如何设置表格摘要。请给我一些建议

我刚刚试过:

PdfDictionary attr = new PdfDictionary();
attr.put(new PdfName("Summary"), new PdfString("Info about the table"));
table.getAccessibilityProperties().addAttributes(new PdfStructureAttributes(attr));
这似乎奏效了。现在需要调整标记工作程序,以确保在遇到
标记时执行此代码

更新1:

我已获取以下HTML文件:

<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>
使用PAC3检查结果时,我得到以下结果:

到目前为止,从技术角度来看,PDF被认为是一个可访问的PDF/UA文件

然后我做了“人类”检查:表格摘要是否存在?不幸的是,它不是,所以我查看了pdfHTML附加组件的代码,没有找到任何对
标记的
摘要
属性的引用。我想当pdfHTML被实现时,它被遗忘了

在第一个实例中,我将编写一个定制标记工作程序,负责添加摘要。完成后,我将要求iText Group实现
summary
属性,以便将其添加到下一个版本中

更新2:

我将我的例子改编如下:

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);
}
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));
        }
    }
}
我通过PAC3运行了它,它仍然作为PDF/UA进行验证,但它没有在任何地方提到表摘要。当我查看PDF时,我现在可以看到摘要:

我现在将与iText Group共享此信息,并请他们检查我的解决方案是否正确(如果这不能解决您的问题,请添加注释)。如果是这样,很有可能从iText 7.1.4开始实施

更新3:

我已经根据OP提供的答案修改了我的代码。OP的代码中有一个错误。在该代码中,
/Summary
作为PDF名称添加,而它应该是PDF字符串

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));
        }
    }
}
现在,当您检查结果时,您将得到以下报告:

如您所见,总结测试通过。

谢谢布鲁诺。我用的是C。和ProcessEnd方法应更改如下

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));
    }
    }

要查看表格标题,必须使用Adobe Acrobat Professional打开pdf。右键单击表格并选择“编辑表格摘要”。标题将显示在此处。

在打印表格之前,是否手动添加一行文本?然后你可以把你想要的任何东西放进去,然后得到文本的高度,并将你的表格偏移那么多。谢谢你提供了适合你的解决方案。Hello@bruno,我正在使用itext 7.1.14。提供的TableSummaryTagWorker是否包含在最新版本的更高版本中?