问题
I want to write custom metadata to a pdf file which are not supported by XMP standard schemas hence I wrote my own schema containing my own properties. I can successfully write these additional custom metadata to my PDF file using either PDFBox or iTextPDF library. I am however unable to read the custom metadata at client side without parsing the XMP xml.
I guess there should be some API that I am not aware of for getting your custom schema back to your java class.
Please help me if I am thinking in right direction or do I actually need to parse the xml for getting my custom data back at client side?
Here is the code I wrote using PDFBox library
Custom Metadata File.
package com.ecomail.emx.core.xmp;
import java.io.IOException;
import org.apache.jempbox.xmp.XMPMetadata;
public class EMXMetadata extends XMPMetadata {
public EMXMetadata() throws IOException {
super();
}
public EMXSchema addEMXSchema() {
EMXSchema schema = new EMXSchema(this);
return (EMXSchema) basicAddSchema(schema);
}
public EMXSchema getEMXSchema() throws IOException {
return (EMXSchema) getSchemaByClass(EMXSchema.class);
}
}
Custom Schema File.
package com.ecomail.emx.core.xmp;
import java.util.List;
import org.apache.jempbox.xmp.XMPMetadata;
import org.apache.jempbox.xmp.XMPSchema;
import org.w3c.dom.Element;
public class EMXSchema extends XMPSchema {
public static final String NAMESPACE = "http://www.test.com/emx/elements/1.1/";
public EMXSchema(XMPMetadata parent) {
super(parent, "test", NAMESPACE);
}
public EMXSchema(Element element, String prefix) {
super(element, prefix);
}
public String getMetaDataType() {
return getTextProperty(prefix + ":metaDataType");
}
public void setMetaDataType(String metaDataType) {
setTextProperty(prefix + ":metaDataType", metaDataType);
}
public void removeRecipient(String recipient) {
removeBagValue(prefix + ":recipient", recipient);
}
public void addRecipient(String recipient) {
addBagValue(prefix + ":recipient", recipient);
}
public List<String> getRecipients() {
return getBagList(prefix + ":recipient");
}
}
XML Client File.
package com.ecomail.emx.core.xmp;
import java.util.GregorianCalendar;
import org.apache.jempbox.xmp.XMPMetadata;
import org.apache.jempbox.xmp.XMPSchemaDublinCore;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
public class XMPClient {
private XMPClient() {
}
public static void main(String[] args) throws Exception {
PDDocument document = null;
try {
document = PDDocument.load("/home/silver/SVNRoot/ecomail/trunk/sample.pdf");
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDDocumentInformation info = document.getDocumentInformation();
EMXMetadata metadata = new EMXMetadata();
XMPSchemaDublinCore dcSchema = metadata.addDublinCoreSchema();
dcSchema.setTitle(info.getTitle());
dcSchema.addContributor("Contributor");
dcSchema.setCoverage("coverage");
dcSchema.addCreator("PDFBox");
dcSchema.addDate(new GregorianCalendar());
dcSchema.setDescription("description");
dcSchema.addLanguage("language");
dcSchema.setCoverage("coverage");
dcSchema.setFormat("format");
EMXSchema emxSchema = metadata.addEMXSchema();
emxSchema.addRecipient("Recipient 1");
emxSchema.addRecipient("Recipient 2");
PDMetadata metadataStream = new PDMetadata(document);
metadataStream.importXMPMetadata(metadata);
catalog.setMetadata(metadataStream);
document.save("/home/silver/SVNRoot/ecomail/trunk/sample1.pdf");
document.close();
document = PDDocument.load("/home/silver/SVNRoot/ecomail/trunk/sample1.pdf");
PDDocumentCatalog catalog2 = document.getDocumentCatalog();
PDMetadata metadataStream2 = catalog2.getMetadata();
XMPMetadata metadata2 = metadataStream2.exportXMPMetadata();
EMXSchema emxSchema2 = (EMXSchema) metadata2.getSchemaByClass(EMXSchema.class);
System.out.println("recipients : " + emxSchema2.getRecipients());
} finally {
if (document != null) {
document.close();
}
}
}
}
In the XMPClient file I am expecting that I will get EMXSchema object back from the resulatant meta data by querying it from its class name.
XMPMetadata metadata2 = metadataStream2.exportXMPMetadata();
EMXSchema emxSchema2 = (EMXSchema) metadata2.getSchemaByClass(EMXSchema.class);
System.out.println("recipients : " + emxSchema2.getRecipients());
But I am getting Null Pointer Exception indicating this was not found. Can anybody please help me if I am doing it right way or do I need to parse the XMP to get my recipients values.
Thanks
回答1:
Finally I got it working myself. The solution is to use the another constructor of the XMPMetadata class that accepts a predefined document class.
document = PDDocument.load("/home/silver/SVNRoot/ecomail/trunk/sample1.pdf");
PDDocumentCatalog catalog2 = document.getDocumentCatalog();
PDMetadata metadataStream2 = catalog2.getMetadata();
System.out.println(metadataStream2.getInputStreamAsString());
InputStream xmpIn = metadataStream2.createInputStream();
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setExpandEntityReferences(true);
f.setIgnoringComments(true);
f.setIgnoringElementContentWhitespace(true);
f.setValidating(false);
f.setCoalescing(true);
f.setNamespaceAware(true);
DocumentBuilder builder = f.newDocumentBuilder();
Document xmpDoc = builder.parse(xmpIn);
EMXMetadata emxMetadata = new EMXMetadata(xmpDoc);
EMXSchema emxSchema2 = emxMetadata.getEMXSchema();
System.out.println("recipients : " + emxSchema2.getRecipients());
Now my custom emxMetadata contains non null emxSchema2 object and I can get back my recipient objects from it. However to make it work I had to modify EMXMetadata to support XMLNamespaceMapping for your schema class
public class EMXMetadata extends XMPMetadata {
public EMXMetadata() throws IOException {
super();
addXMLNSMapping(EMXSchema.NAMESPACE, EMXSchema.class);
}
public EMXMetadata(Document xmpDoc) {
super(xmpDoc);
addXMLNSMapping(EMXSchema.NAMESPACE, EMXSchema.class);
}
public EMXSchema addEMXSchema() {
EMXSchema schema = new EMXSchema(this);
return (EMXSchema) basicAddSchema(schema);
}
public EMXSchema getEMXSchema() throws IOException {
return (EMXSchema) getSchemaByClass(EMXSchema.class);
}
}
来源:https://stackoverflow.com/questions/7965599/custom-schema-to-xmp-metadata