我正在尝试将文本添加到我的 pdf 中,该文本只能由屏幕阅读器查看。该 pdf 包含一个将负数(即 -4.565)转换为 (4.565) - 红色的表格。当我使用屏幕阅读器时,它只读取 4.565,我想预先添加负值。
我尝试过使用 aria-label、aria-live 并创建一个由于文本颜色等原因而不可见的 html 属性
我知道怎么做了。我必须使用convertToElements(),而不是我最初使用的convertToDocument()。
然后您可以递归地遍历元素列表,直到找到需要更改的描述,并在元素上调用 .GetAccessibilityProperties().SetAlternativeDescription("Screen reader only") 。您还必须首先将元素转换为其类型。 (段落)element.GetAccess...
private string recurse(IList<iText.Layout.Element.IElement> elements, string previous, Document document)
{
string result = "";
// variable that get sent back that is the string of the content so that .SetAlternateDescription() can be called with the content
foreach (iText.Layout.Element.IElement element in elements)
{
var elementType = element.GetType().Name;
// I am only going through tables but it could be anything
if (elementType == "Table")
{
Table table = (Table)element;
IList<iText.Layout.Element.IElement> children = ((IAbstractElement)element).GetChildren();
recurse(children, "Table", document);
document.Add(table);
// Add the most complex form, dont add children
}
else if (elementType == "Cell")
{
string content = recurse(((IAbstractElement)element).GetChildren(), "Cell", document);
if (content.Length > 0) // check to see that I need to set Description
((Cell)element).GetAccessibilityProperties().SetAlternateDescription("SCREEN READER ONLY READS THIS STRING, NOT VISIBLE IN PDF:" + content);
}
else if (elementType == "Paragraph" && previous == "Cell")
{
result += recurse(((IAbstractElement)element).GetChildren(), "Cell-Paragraph", document);
// cell-paragraph to show the element came from a table -> paragraph and not div -> paragraph
}
else if (elementType == "Text" && previous == "Cell-Paragraph")
{
Text textElement = (Text)element;
string content = textElement.GetText();
return content;
// This is where the actual string content is
}
else if(previous.Length == 0) // Add other elements, only when they are the parent element
{
Paragraph paragraphElement = element as Paragraph;
if (paragraphElement != null)
document.Add(paragraphElement);
Div divElement = element as Div;
if (divElement != null)
document.Add(divElement);
}
}
return result;
}