Java – how to add metadata to PDF documents using pdfbox?

I have an input stream of PDF documents for me to use I want to add the topic metadata to the document and save it I don't know what to do

Here I see an example recipe: https://pdfbox.apache.org/1.8/cookbook/workingwithmetadata.html

However, it is still vague Here are what I'm trying to do and what I have questions about

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

Solution

This answer uses xmp@R_817_2419 @And from the addmetadatafromdocinfo example in the source code download:

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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>