gpt4 book ai didi

java - 提取名称中未列出的嵌入文件

转载 作者:行者123 更新时间:2023-12-01 13:50:31 26 4
gpt4 key购买 nike

我正在尝试从 a PDF 中提取嵌入文件位于此处:

Catalog / AF[0] / EF / F
  • AF 是一个数组
  • 第一个条目是文件规范字典
  • EF 是字典
  • F 应该是嵌入式文件流

使用 PDFBox 我可以做到这一点:

PDFParser parser = new PDFParser(is);
parser.parse();
PDDocument document = parser.getPDDocument();
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary(catalog);
PDEmbeddedFilesNameTreeNode embeddedFiles = namesDictionary.getEmbeddedFiles();
List<PDNameTreeNode> kids = embeddedFiles.getKids();
PDEmbeddedFilesNameTreeNode node = (PDEmbeddedFilesNameTreeNode) kids.get(0);
COSDictionary cosDictionary = node.getCOSDictionary();
COSArray a = (COSArray) cosDictionary.getDictionaryObject(COSName.NAMES);
COSDictionary d = (COSDictionary) a.getObject(1);
COSDictionary ef = (COSDictionary) d.getDictionaryObject(COSName.EF);
COSDictionary f = (COSDictionary) ef.getDictionaryObject(COSName.F);
System.out.println(f);

输出(格式化以提高可读性):

COSDictionary{(COSName{Length}:COSInt{1433})
(COSName{Filter}:COSName{FlateDecode})
(COSName{Type}:COSName{EmbeddedFile})
(COSName{Subtype}:COSName{text/xml})
(COSName{Params}:COSDictionary{
(COSName{Size}:COSInt{12030})
(COSName{ModDate}:COSString{D:20130628111510+02'00'})
}
)
}

这就是我到目前为止所期望的。但是这个嵌入的 XML 文件的字节在哪里呢?我如何访问它们?

最佳答案

我找到了一个更简单的方法。由于嵌入文件位于 KIDS 下,而不是 NAMES(这是正确的),因此可以正常工作:

List<PDNameTreeNode> kids = embeddedFiles.getKids();
if (kids != null) {
for (PDNameTreeNode kid : embeddedFiles.getKids()) {
PDComplexFileSpecification spec =
(PDComplexFileSpecification) kid.getValue(ZUGFERD_XML_FILENAME);
PDEmbeddedFile file = spec.getEmbeddedFile();
return file.getByteArray();
}
}

关于java - 提取名称中未列出的嵌入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19997270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com