自定义架构到XMP元数据 [英] custom schema to XMP metadata

查看:300
本文介绍了自定义架构到XMP元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将XMP标准模式不支持的自定义元数据写入pdf文件,因此我编写了自己的包含自己属性的模式.我可以使用PDFBox或iTextPDF库成功将这些其他自定义元数据写入PDF文件.但是,如果不解析XMP xml,就无法在客户端读取自定义元数据.

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.

我想应该有一些我不知道的API,用于将您的自定义模式返回到Java类.

I guess there should be some API that I am not aware of for getting your custom schema back to your java class.

如果我朝着正确的方向思考,或者实际上需要解析xml以便将我的自定义数据返回客户端时,请帮我吗?

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?

这是我使用PDFBox库编写的代码

Here is the code I wrote using PDFBox library

自定义元数据文件.

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);
}
}

自定义架构文件.

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客户端文件.

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();
        }
    }
}
 }

我期望在XMPClient文件中,通过从类元数据查询它来从可恢复的元数据中获取EMXSchema对象.

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());

但是我收到了Null Pointer Exception,表明未找到此异常. 如果我做得对,请问有人可以帮我吗?还是我需要解析XMP才能获得收件人的值.

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.

谢谢

推荐答案

最后,我自己动手了. 解决方案是使用XMPMetadata类的另一个构造函数,该构造函数接受预定义的文档类.

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());

现在,我的自定义emxMetadata包含非null的emxSchema2对象,我可以从中获取接收者对象.但是,要使其工作,我必须修改EMXMetadata以支持您的架构类的XMLNamespaceMapping

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);
}

}

这篇关于自定义架构到XMP元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆