使用 iText 为标记的 pdf (PDF/UA) 中的图像添加替代文本 [英] Add alternative text for an image in tagged pdf (PDF/UA) using iText
问题描述
我在
代码无法识别狐狸或狗,因此我们创建了一个带有 Alt 属性的新文档,上面写着没有 Alt 描述的图形":
我们通过遍历结构树来添加这个描述,寻找标记为/Figure
元素的结构元素:
public void handlePdf(String src, String dest)抛出 IOException,文档异常 {PdfReader 阅读器 = 新 PdfReader(src);PdfDictionary catalog = reader.getCatalog();PdfDictionary structTreeRoot =catalog.getAsDict(PdfName.STRUCTTREEROOT);操作(structTreeRoot);PdfStamper 压模 = 新 PdfStamper(阅读器,新的 FileOutputStream(dest));压模.关闭();}公共无效操作(PdfDictionary 元素){如果(元素==空)返回;if (PdfName.FIGURE.equals(element.get(PdfName.S))) {element.put(PdfName.ALT,new PdfString("没有Alt描述的图"));}PdfArray kids = element.getAsArray(PdfName.K);if (kids == null) 返回;for (int i = 0; i < kids.size(); i++)操纵(kids.getAsDict(i));}
您可以轻松地将此 Java 示例移植到 C#:
- 从
PdfReader
对象中获取根字典, - 获取结构树的根(一个字典),
- 遍历那棵树的每个分支的所有孩子,
- 如果线索是人物,请添加
/Alt
条目.
完成后,使用 PdfStamper
保存更改后的文件.
I've looked up some documentations and examples under the http://developers.itextpdf.com/examples.
I know iText is able to generate tagged pdf from scratch, but is it possible to insert alternative text to images in an existing tagged pdf (without changing anything else)? I need to implement this feature in a program without using GUI applications (such as Adobe Acrobat Pro). Thanks.
Please take a look at the AddAltTags example.
In this example, we take a PDF with images of a fox and a dog where the Alt keys are missing: no_alt_attribute.pdf
Code can't recognize a fox or a dog, so we create a new document with Alt attributes saying "Figure without an Alt description": added_alt_attributes.pdf)
We add this description by walking through the structure tree, looking for structural elements marked as /Figure
elements:
public void manipulatePdf(String src, String dest)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfDictionary catalog = reader.getCatalog();
PdfDictionary structTreeRoot =
catalog.getAsDict(PdfName.STRUCTTREEROOT);
manipulate(structTreeRoot);
PdfStamper stamper = new PdfStamper(
reader, new FileOutputStream(dest));
stamper.close();
}
public void manipulate(PdfDictionary element) {
if (element == null)
return;
if (PdfName.FIGURE.equals(element.get(PdfName.S))) {
element.put(PdfName.ALT,
new PdfString("Figure without an Alt description"));
}
PdfArray kids = element.getAsArray(PdfName.K);
if (kids == null) return;
for (int i = 0; i < kids.size(); i++)
manipulate(kids.getAsDict(i));
}
You can easily port this Java example to C#:
- Get the root dictionary from the
PdfReader
object, - Get the root of the structure tree (a dictionary),
- Loop over all the kids of every branch of that tree,
- When a lead is a figure, add an
/Alt
entry.
Once this is done, use PdfStamper
to save the altered file.
这篇关于使用 iText 为标记的 pdf (PDF/UA) 中的图像添加替代文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!