我有一个可用的PDF文档输入流。我想将subject元数据添加到文档中,然后将其保存。我不确定该怎么做。

我在这里遇到了一个样本食谱:https://pdfbox.apache.org/1.8/cookbook/workingwithmetadata.html

但是,它仍然是模糊的。以下是我正在尝试的地方以及遇到问题的地方

PDDocument doc = PDDocument.load(myInputStream);
PDDocumentCatalog catalog = doc.getDocumentCatalog();
InputStream newXMPData = ...; //what goes here? How can I add subject tag?
PDMetadata newMetadata = new PDMetadata(doc, newXMLData, false );
catalog.setMetadata( newMetadata );
//does anything else need to happen to save the document??
//I would like an outputstream of the document (with metadata) so that I can save it to an S3 bucket

最佳答案

此答案使用xmpbox,并且来自源代码下载中的AddMetadataFromDocInfo example:

XMPMetadata xmp = XMPMetadata.createXMPMetadata();
DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
dc.setDescription("descr");
XmpSerializer serializer = new XmpSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serialize(xmp, baos, true);
PDMetadata metadata = new PDMetadata(doc);
metadata.importXMPMetadata(baos.toByteArray());
doc.getDocumentCatalog().setMetadata(metadata);

10-07 23:35