Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/fa98856acf917d1c24271bcd88cd61aa to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/fa98856acf917d1c24271bcd88cd61aa to your computer and use it in GitHub Desktop.
Add, change and update XMP metadata in EPS file with Java

Working with XMP Metadata in EPS Examples

These code snippets show how to add, read, and modify XMP metadata in EPS files using Aspose.Page for Java. XMP metadata enables embedding document information for archiving and workflow automation.

Key Use Cases

  • How to read XMP metadata from EPS file
  • How to create XMP packets (CreatorTool, CreateDate, Thumbnails etc)
  • How to change XMP properties values (title, creator, ModifyDate)
  • How to add custom properties and namespaces and synchronize with PS metadata

How to Run Examples

  1. Reference Aspose.Page for Java in your project.
  2. Copy the required code snippet into your project.
  3. Download a temporary license and set it up as described here or use a paid license.
  4. Run the example.

Restrictions

In evaluation mode, input EPS file is limited to 500Kb. Apply a valid license to unlock full functionality.

Related Documentation

See more about working with XMP metadata in EPS files in Working with XMP Metadata in EPS files | Java chapter of the our tutorial.

Related Resources

Requirements

  • Java 8 or higher
  • Aspose.Page for Java library
Aspose.Page for Java – Add XMP Metadata in EPS Examples
// 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) {
}
// Add array items 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_array_items_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 one more title. I will be added at the end of array by default.
xmp.addArrayItem("dc:title", new XmpValue("NewTitle"));
// Add one more creator. It will be added in the array by an index (0).
xmp.addArrayItem("dc:creator", 0, new XmpValue("NewCreator"));
// 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) {
}
// Add named value 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_named_value_input.eps")) {
// Create PsDocument instance from stream
PsDocument document = new PsDocument(psStream);
String outputFileName = "add_xmp_named_value_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 named value to "xmpTPg:MaxPageSize" structure.
xmp.addNamedValue("xmpTPg:MaxPageSize", "stDim: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) {
}
// 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) {
}
// Add simple properties 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_simple_props_out.eps";
ZoneId systemZone = ZoneId.systemDefault();
OffsetDateTime now = LocalDateTime.now().atZone(systemZone).toOffsetDateTime();
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 Integer poperty
xmp.put("xmp:Intg1", new XmpValue(111));
// Add DateTime poperty
xmp.put("xmp:Date1", new XmpValue(Date.from(now.toInstant())));
// Add Double poperty
xmp.put("xmp:Double1", new XmpValue(111.11D));
// Add String poperty
xmp.put("xmp:String1", new XmpValue("ABC"));
// 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) {
}
// Change array items 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 = "change_xmp_array_items_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
// Change title item at index 0
xmp.setArrayItem("dc:title", 0, new XmpValue("NewTitle"));
// Change creator item at index 0
xmp.setArrayItem("dc:creator", 0, new XmpValue("NewCreator"));
// 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) {
}
// Change named value 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_named_value_input.eps")) {
// Create PsDocument instance from stream
PsDocument document = new PsDocument(psStream);
String outputFileName = "change_xmp_named_value_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
// Change named value "stDim:unit" in "xmpTPg:MaxPageSize" structure.
xmp.setNamedValue("xmpTPg:MaxPageSize", "stDim:unit", new XmpValue("Inches"));
// Add named value "stDim:newKey" in "xmpTPg:MaxPageSize" structure.
xmp.setNamedValue("xmpTPg:MaxPageSize", "stDim: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) {
}
// Change values 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() + "get_input.eps")) {
// Create PsDocument instance from stream
PsDocument document = new PsDocument(psStream);
String outputFileName = "change_values_out.eps";
ZoneId systemZone = ZoneId.systemDefault();
OffsetDateTime now = LocalDateTime.now().atZone(systemZone).toOffsetDateTime();
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
// Change ModifyDate value
xmp.put("xmp:ModifyDate", new XmpValue(Date.from(now.toInstant())));
// Change Creator value
XmpValue value = new XmpValue("Aspose.Page");
xmp.put("dc:creator", value);
// Change Title value
value = new XmpValue("(change_values.eps)");
xmp.put("dc:title", value);
// 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) {
}
// 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());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment