You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在iText 7中创建Image时直接设置替代文本?

关于iText中创建Image时直接设置替代文本的问题

我对遍历已完成的PDFDocument来判断元素是否为Figure并动态添加替代文本的方案不感兴趣,我认为这是一种取巧的做法。该方案的实现代码如下:

public void manipulate(PdfDictionary element) {
    if (element == null) {
        return;
    }

    // 如果元素是Figure,则添加/Alt条目
    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.getAsDictionary(i));
    }
}

相反,我希望在创建指定Image时显式指定替代文本,我的代码示例如下:

ClassPathResource resourceLogo = new ClassPathResource(PDF_LOGO_FILENAME);
File fileLogo = resourceLogo.getFile();
ImageData logoData = ImageDataFactory.create(fileLogo.getAbsolutePath());
Image logo = new Image(logoData);
// ... 这里Image或ImageData类里有没有类似.setAlternativeText的方法?
// 比如 logo.setAltText(..), logoData.setAltText(..)
p.add(logo);

请问Image或ImageData类中是否有类似.setAlternativeText的方法可实现该需求?


内容的提问来源于stack exchange,提问作者gene b.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 13:40:03