Add XMP metadata to EPS file using Java

In order to add XMP metadata to EPS file it is necessary to do several steps:

  1. Initialize an input stream for input EPS file.
  2. Create an instance of PsDocument from created earlier input stream.
  3. Get an instance of XmpMetadata from the PsDocument. If given EPS file doesn’t contain XMP metadata the new one will be created, filled in with values from PS metadata comments and returned to you.
  4. Now you can view values of metadata fileds.
  5. Initialize an output stream for output EPS file.
  6. Save EPS file with new XMP metadata.


Following code snippet shows how to add XMP metadata to EPS file in Java:

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2// Set license
 3new License().setLicense(BaseExamplesTest.LICENSE_PATH);
 4
 5// The path to the documents directory.
 6String dataDir = Utils.getDataDir();
 7
 8// Initialize input EPS file stream
 9FileInputStream psStream = new FileInputStream(dataDir + "xmp2.eps");
10
11PsDocument document = new PsDocument(psStream);
12
13try {
14    // 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)
15    XmpMetadata xmp = document.getXmpMetadata();
16
17    // Get "CreatorTool" value
18    if (xmp.containsKey("xmp:CreatorTool"))
19        System.out.println("CreatorTool: " + xmp.get("xmp:CreatorTool").toStringValue());
20    
21    // Get "CreateDate" value
22    if (xmp.containsKey("xmp:CreateDate"))
23        System.out.println("CreateDate: " + xmp.get("xmp:CreateDate").toStringValue());
24
25    // Get "Title" value
26    if (xmp.containsKey("dc:title"))
27        System.out.println("Title: " + xmp.get("dc:title").toArray()[0].toStringValue());
28
29    // Get "format" value
30    if (xmp.containsKey("dc:format"))
31        System.out.println("Format: " + xmp.get("dc:format").toStringValue());
32
33    // Get "creator" value
34    if (xmp.containsKey("dc:creator"))
35        System.out.println("Creator: " + xmp.get("dc:creator").toArray()[0].toStringValue());
36    
37    // Get "MetadataDate" value
38    if (xmp.containsKey("xmp:MetadataDate"))
39        System.out.println("MetadataDate: " + xmp.get("xmp:MetadataDate").toStringValue());
40    
41    
42    // Initialize output EPS file stream
43    FileOutputStream outPsStream = new FileOutputStream(dataDir + "xmp2_changed.eps");
44    
45    // Save document with new XMP metadata
46		try {			
47			document.save(outPsStream);
48		} finally {
49			outPsStream.close();
50		}
51
52} finally {
53    // close input EPS stream
54		psStream.close();
55}

See adding XMP metadata in .NET and C++.



You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.