pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vitaliy Kubay <kubay.vita...@gmail.com>
Subject Parsing Metadata - User defined schema
Date Wed, 31 Jan 2018 17:23:45 GMT
I have created a custom metadata schema as follows.

// MyMetadata.java
public class MyMetadata extends XMPMetadata {

    public MyMetadata() {
        super();
    }

    public MySchema createAndAddMySchema() {
        MySchema schema = new MySchema(this);
        addSchema(schema);
        return schema;
    }

    public MySchema getMySchema() {
        return (MySchema) getSchema(MySchema.class);
    }
}

// MySchema.java
public class MySchema extends XMPSchema {
    public static final String NAMESPACE = "http://www.mydomain.com/";

    public MySchema(XMPMetadata parent) {
        super(parent, NAMESPACE, "userdefined");
    }

    public void setId(String id) {
        setTextPropertyValueAsSimple("id", id);
    }

    public String getId() {
        return getUnqualifiedTextPropertyValue("id");
    }

    // ......

}



Parsing Metadata - User defined schema

// main.kt
fun attachMetadata(...) {

    ...

    val lu = LayerUtility(wmdoc)
    val xobject = lu.importPageAsForm(wmdoc, wmpage)

    // attach metadata to the xobject
    val metadata = HYGMetadata()
    val hyg = metadata.createAndAddHYGSchema()
    hyg.watermarkId = "OverlayOverlay"

    val metadataStream = PDMetadata(outdoc)
    val serializer = XmpSerializer()
    val baos = ByteArrayOutputStream()

    serializer.serialize(metadata, baos, true)
    metadataStream.importXMPMetadata( baos.toByteArray() )
    xobject.cosObject.setItem(COSName.METADATA, metadataStream)

    println("Num pages: " + outdoc.numberOfPages)
    // draw the watermark on all pages
    for (i in 0 .. (outdoc.numberOfPages-1)) {
        println(i)
        val cspg = PDPageContentStream(outdoc, outdoc.getPage(i),
PDPageContentStream.AppendMode.APPEND, true)
        cspg.drawForm(xobject)
        cspg.close()
    }

    // Save and close
    inpdoc.close()
    outdoc.save(outputdoc)
    outdoc.close()
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message