首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PDFBox:创建的PDF/A-3A文档的验证由于流长不匹配而失败

PDFBox:创建的PDF/A-3A文档的验证由于流长不匹配而失败
EN

Stack Overflow用户
提问于 2022-04-08 10:55:06
回答 1查看 95关注 0票数 0

我正在尝试使用示例CreatePDFA创建一个PDF/A-3A文件,并附带一个附带的复杂文本文件,但是使用VeraPDF验证器验证结果文件失败了。我使用的是PDFBox v.3.0.0 RC1。

错误:在流字典中指定的长度键的值应该与文件中的字节数相匹配,该字节数应位于LINE FEED (0Ah)字符后面的流关键字之后,以及EOL标记之前的endstream关键字之前。

错误位置是: root/indirectObjects0/directObject

如何将正确的长度添加到PDF文件中?附件代码如下:

代码语言:javascript
复制
    private void attachFile(PDDocument doc, File file, String mediaType, String description) throws IOException {

        long fileSize = file.length();
        BasicFileAttributes fileAttr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);

        GregorianCalendar creationTimeCal = new GregorianCalendar();
        creationTimeCal.setTimeInMillis(fileAttr.creationTime().toMillis());

        GregorianCalendar modifiedTimeCal = new GregorianCalendar();
        modifiedTimeCal.setTimeInMillis(fileAttr.lastModifiedTime().toMillis());

        // embedded files are stored in a named tree
        PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();

        // first create the file specification, which holds the embedded file
        PDComplexFileSpecification fs = new PDComplexFileSpecification();

        // use both methods for backwards, cross-platform and cross-language compatibility.
        fs.setFile(file.getName());
        fs.setFileUnicode(file.getName());

        COSDictionary dict = fs.getCOSObject();
        dict.setName("AFRelationship", "Alternative");

        try (InputStream is = Files.newInputStream(file.toPath(), StandardOpenOption.READ)) {

            PDEmbeddedFile ef = new PDEmbeddedFile(doc, is);

            // now lets some of the optional parameters
            ef.setSubtype(mediaType);
            ef.setSize((int) fileSize);
            ef.setCreationDate(creationTimeCal);
            ef.setModDate(modifiedTimeCal);
            ef.setDecodedStreamLength((int) fileSize);

            // use both methods for backwards, cross-platform and cross-language compatibility.
            fs.setEmbeddedFile(ef);
            fs.setEmbeddedFileUnicode(ef);
            fs.setFileDescription(StringUtils.defaultString(description, file.getName()));

            // create a new tree node and add the embedded file
            String attachmentName = StringUtils.defaultString(file.getName(),description);
            PDEmbeddedFilesNameTreeNode treeNode = new PDEmbeddedFilesNameTreeNode();
            treeNode.setNames(Collections.singletonMap(attachmentName, fs));

            // add the new node as kid to the root node
            List<PDEmbeddedFilesNameTreeNode> kids = new ArrayList<>();
            kids.add(treeNode);
            efTree.setKids(kids);

            // add the tree to the document catalog
            PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
            names.setEmbeddedFiles(efTree);
            doc.getDocumentCatalog().setNames(names);

            // AF entry (Array) in catalog with the FileSpec
            COSArray cosArray = new COSArray();
            cosArray.add(fs);
            doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray);

            // show attachments panel in some viewers
            doc.getDocumentCatalog().setPageMode(PageMode.USE_ATTACHMENTS);
        }
    }

可以在这里找到PDF文件:https://gofile.io/d/FuWIdV

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-11 07:05:42

PdfBox的最新版本3.0.0-alpha2 2似乎解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71795785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档