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

Java生成PDF/A-1b失败:VeraPDF提示缺失PDF/A识别Schema

使用iText 5.5.13.1生成PDF/A-1b无法通过VeraPDF验证

我在Java中使用iText 5.5.13.1生成PDF/A-1b,尝试多种方案均无效,生成的文件无法通过VeraPDF验证。即使测试时使用原本符合PDF/A标准的文件,仍出现如下错误:

MainXMPPackage  
Identification_size == 1    
root/document[0]/metadata[0](2 0 obj PDMetadata)/XMPPackage[0]
The document metadata stream doesn't contains PDF/A Identification Schema

以下是我的实现代码及Maven依赖:

实现代码

public byte[] convertirAPdfA(byte[] pdf) throws IOException, DocumentException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // 读取原始PDF文档
        PdfReader reader = new PdfReader(pdf);
        Document document = new Document(reader.getPageSizeWithRotation(1));

        // 创建PdfAWriter写入新的PDF/A
        PdfAWriter writer = PdfAWriter.getInstance(document, outputStream, PdfAConformanceLevel.PDF_A_1B);

        // 打开文档准备写入
        document.open();

        // 创建并添加XmpMetadata
        writer.createXmpMetadata();

        // 创建并嵌入ICC配置文件
        ICC_Profile icc = ICC_Profile.getInstance(getClass().getResourceAsStream("/icc/sRGB-IEC61966-2.1.icc"));
        writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

        // 创建字体
        BaseFont font = BaseFont.createFont("/fonts/BookAntiqua/BookAntiqua.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        writer.getDirectContent().setFontAndSize(font, 12);

        // 将原始PDF的页面添加到新的PDF/A中
        PdfContentByte cb = writer.getDirectContent();
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            document.newPage();
            PdfImportedPage page = writer.getImportedPage(reader, i);
            cb.addTemplate(page, 0, 0);
        }

        // 创建XMP元数据
        XMPMeta xmpMeta = XMPMetaFactory.create();

        try {
            // 添加DC(Dublin Core)元数据
            xmpMeta.setProperty(XMPConst.NS_DC, "dc:format", "application/pdf");
            xmpMeta.setProperty(XMPConst.NS_DC, "dc:title", "Title of the Document");
            xmpMeta.setProperty(XMPConst.NS_DC, "dc:creator", "Your Name");
            xmpMeta.setProperty(XMPConst.NS_DC, "dc:description", "Description of the document");

            // 添加PDF/A识别模式
            XMPMetaFactory.getSchemaRegistry().registerNamespace("http://www.aiim.org/pdfa/ns/id/", "pdfaid");
            xmpMeta.setProperty("http://www.aiim.org/pdfa/ns/id/", "pdfaid:part", "1");
            xmpMeta.setProperty("http://www.aiim.org/pdfa/ns/id/", "pdfaid:conformance", "B");

            // 将XMP元数据序列化为字节数组
            ByteArrayOutputStream xmpOutputStream = new ByteArrayOutputStream();
            XMPMetaFactory.serialize(xmpMeta, xmpOutputStream, new SerializeOptions().setUseCompactFormat(true));

            // 在文档中设置XMP元数据
            writer.setXmpMetadata(xmpOutputStream.toByteArray());
        } catch (XMPException e) {
            e.printStackTrace();
        }

        // 关闭文档和阅读器
        document.close();
        reader.close();

        // 返回PDF/A的字节数组
        return outputStream.toByteArray();
    }

Maven依赖

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-pdfa</artifactId>
    <version>5.5.13.1</version>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.1</version>
</dependency>

内容的提问来源于stack exchange,提问作者S3RG1OGALAZ

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 03:42:36