Reading and adding XMP metadata in EPS/PS documents in Java

Extract, inspect, add, and update XMP (Extensible Metadata Platform) metadata in EPS and PostScript (PS) documents using Aspose.Page for Java. Embed standard Dublin Core properties, document creation statistics, copyright details, and custom metadata structures directly into PostScript file streams without altering the underlying visual vector graphic content.

 

Aspose.Page for Java provides a high-performance XmpHeaderConverter and XmpMetadata object model to extract existing XMP metadata or inject new properties into EPS/PS files.

  • To read information like author details, creation dates, copyright notices, and custom attributes from EPS files use XmpMetadata.get() method.
  • To edit existing metadata fields like document titles or modification history use XmpMetadata.set() method.
  • To add proprietary, enterprise-specific metadata attributes to graphic files use XmpMetadata.registerNamespace() method.

Reading existing XMP metadata from an EPS File

Extract and parse standard and user-defined XMP properties from an EPS document to inspect metadata fields like dc:title, xmp:CreatorTool, and xmp:CreateDate.

// Get XMP metadata from EPS document.
// Learn more: https://docs.aspose.com/page/java/working-with-xm-metadata-in-eps/

// Create PsDocument instance from file
PsDocument document = new PsDocument(getDataDir() + "get_input.eps");

// Get XMP metadata. If EPS file doesn't contain XMP metadata we get new one filled with values from PS metadata comments (%%Creator, %%CreateDate, %%Title etc)
XmpMetadata xmp = document.getXmpMetadata();

// Get "CreatorTool" value
if (xmp.containsKey("xmp:CreatorTool"))
    System.out.println("CreatorTool: " + xmp.get("xmp:CreatorTool").toStringValue());

// Get "CreateDate" value
if (xmp.containsKey("xmp:CreateDate"))
    System.out.println("CreateDate: " + xmp.get("xmp:CreateDate").toStringValue());

// Get a width of a thumbnail image if exists
if (xmp.containsKey("xmp:Thumbnails") && xmp.get("xmp:Thumbnails").isArray()) {
    XmpValue val = xmp.get("xmp:Thumbnails").toArray()[0];
    if (val.isNamedValues() && val.toDictionary().containsKey("xmpGImg:width"))
        System.out.println("Thumbnail Width: " + val.toDictionary().get("xmpGImg:width").toInteger());
}

// Get "format" value
if (xmp.containsKey("dc:format"))
    System.out.println("Format: " + xmp.get("dc:format").toStringValue());

// Get "DocumentID" value
if (xmp.containsKey("xmpMM:DocumentID"))
    System.out.println("DocumentID: " + xmp.get("xmpMM:DocumentID").toStringValue());

Adding and injecting new XMP metadata into an EPS File

Inject standard metadata properties (such as Dublin Core or XMP Basic schemas) into an EPS document and save the updated file output.

// Add XMP metadata to EPS document.
// Learn more: https://docs.aspose.com/page/java/working-with-xm-metadata-in-eps/

// Initialize EPS file input stream
try (FileInputStream psStream = new FileInputStream(getDataDir() + "add_input.eps")) {
    // Create PsDocument instance from stream. Given EPS file doesn't contain XMP metadata, but usual metadata does contain.
    PsDocument document = new PsDocument(psStream);

    String outputFileName = "add_xmp_metadata_out.eps";

    XmpMetadata xmp = null;

    try {
        // Get XMP metadata. If EPS file doesn't contain XMP metadata we get new one filled with values from PS metadata comments (%%Creator, %%CreateDate, %%Title etc)
        xmp = document.getXmpMetadata();

        // Check metadata values extracted from PS metadata comments and set up in new XMP metadata

        // Get "CreatorTool" value
        if (xmp.containsKey("xmp:CreatorTool"))
            System.out.println("CreatorTool: " + xmp.get("xmp:CreatorTool").toStringValue());

        // Get "CreateDate" value
        if (xmp.containsKey("xmp:CreateDate"))
            System.out.println("CreateDate: " + xmp.get("xmp:CreateDate").toStringValue());

        // Get "format" value
        if (xmp.containsKey("dc:format"))
            System.out.println("Format: " + xmp.get("dc:format").toStringValue());

        // Get "title" value
        if (xmp.containsKey("dc:title"))
            System.out.println("Title: " + xmp.get("dc:title").toArray()[0].toStringValue());

        // Get "creator" value
        if (xmp.containsKey("dc:creator"))
            System.out.println("Creator: " + xmp.get("dc:creator").toArray()[0].toStringValue());

        // Get "MetadataDate" value
        if (xmp.containsKey("xmp:MetadataDate"))
            System.out.println("MetadataDate: " + xmp.get("xmp:MetadataDate").toStringValue());

        // Update MetadataDate value                
        Date metadataDate = xmp.get("xmp:MetadataDate").toDateTime();

        // Save EPS file with new XMP metadata

        // Create ouput stream
        try (FileOutputStream outPsStream = new FileOutputStream(getOutputDir() + outputFileName)) {
            // Save EPS file
            document.save(outPsStream);
        }
    } catch (IOException ex) {
    }

Adding custom named namespaces and array metadata

Register enterprise-specific XML namespaces and store complex data types, such as array lists, inside the document’s XMP structure.

// Add namespace in XMP metadata in EPS document.
// Learn more: https://docs.aspose.com/page/java/working-with-xm-metadata-in-eps/
	
// Initialize EPS file input stream
try (FileInputStream psStream = new FileInputStream(getDataDir() + "add_simple_props_input.eps")) {
    // Create PsDocument instance from stream
    PsDocument document = new PsDocument(psStream);
	
    String outputFileName = "add_xmp_namespace_out.eps";
	
    try {
        // Get XMP metadata. If EPS file doesn't contain XMP metadata we get new one filled with values from PS metadata comments (%%Creator, %%CreateDate, %%Title etc)
        XmpMetadata xmp = document.getXmpMetadata();
	
        //Change XMP metadata values
	
        // Add new XML namespace "tmp".
        xmp.registerNamespaceURI("tmp", "http://www.some.org/schema/tmp#");
	
        // Add new string property in new namespace.
        xmp.put("tmp:newKey", new XmpValue("NewValue"));
	
        // Save EPS file with changed XMP metadata
	
        // Create ouput stream
        try (FileOutputStream outPsStream = new FileOutputStream(getOutputDir() + outputFileName)) {
            // Save EPS file
            document.save(outPsStream);
        }
    } catch (IOException ex) {
    }

Installation and Setup

Add Aspose.Page for Java to your Maven:

Package Manager Console Command

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-page</artifactId>
    <version>Latest</version>
</dependency>

FAQ

1. Can I extract XMP metadata from an EPS file that does not contain a standard XMP header?

If an EPS file lacks an existing XMP header packet, PsDocument.getXmpMetadata() returns an empty XmpMetadata structure. You can populate it with properties and call document.save() to write a new valid XMP packet to the file.

2. Which standard XMP schemas are supported by Aspose.Page for Java?

Aspose.Page natively supports all standard XMP schemas, including Dublin Core (dc:), XMP Basic (xmp:), XMP Rights Management (xmpRights:), XMP Media Management (xmpMM:), and Adobe PDF schema (pdf:).

3. Does updating XMP metadata re-encode or degrade the visual EPS graphics?

No. Aspose.Page modifies or injects the XMP header metadata block located in the document preamble without altering or re-compressing vector shapes, raster graphics, or PostScript code.