Add XMP metadata to EPS file using Java
In order to add XMP metadata to EPS file it is necessary to do several steps:
- Initialize an input stream for input EPS file.
- Create an instance of PsDocument from created earlier input stream.
- 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.
- Now you can view values of metadata fileds.
- Initialize an output stream for output EPS file.
- Save EPS file with new XMP metadata.
The following code snippet shows how to add XMP metadata to EPS file in Java:
1// Add XMP metadata to EPS document.
2
3// Initialize EPS file input stream
4try (FileInputStream psStream = new FileInputStream(getDataDir() + "add_input.eps")) {
5 // Create PsDocument instance from stream. Given EPS file doesn't contain XMP metadata, but usual metadata does contain.
6 PsDocument document = new PsDocument(psStream);
7
8 String outputFileName = "add_xmp_metadata_out.eps";
9
10 XmpMetadata xmp = null;
11
12 try {
13 // 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)
14 xmp = document.getXmpMetadata();
15
16 // Check metadata values extracted from PS metadata comments and set up in new XMP metadata
17
18 // Get "CreatorTool" value
19 if (xmp.containsKey("xmp:CreatorTool"))
20 System.out.println("CreatorTool: " + xmp.get("xmp:CreatorTool").toStringValue());
21
22 // Get "CreateDate" value
23 if (xmp.containsKey("xmp:CreateDate"))
24 System.out.println("CreateDate: " + xmp.get("xmp:CreateDate").toStringValue());
25
26 // Get "format" value
27 if (xmp.containsKey("dc:format"))
28 System.out.println("Format: " + xmp.get("dc:format").toStringValue());
29
30 // Get "title" value
31 if (xmp.containsKey("dc:title"))
32 System.out.println("Title: " + xmp.get("dc:title").toArray()[0].toStringValue());
33
34 // Get "creator" value
35 if (xmp.containsKey("dc:creator"))
36 System.out.println("Creator: " + xmp.get("dc:creator").toArray()[0].toStringValue());
37
38 // Get "MetadataDate" value
39 if (xmp.containsKey("xmp:MetadataDate"))
40 System.out.println("MetadataDate: " + xmp.get("xmp:MetadataDate").toStringValue());
41
42 // Update MetadataDate value
43 Date metadataDate = xmp.get("xmp:MetadataDate").toDateTime();
44
45 // Save EPS file with new XMP metadata
46
47 // Create ouput stream
48 try (FileOutputStream outPsStream = new FileOutputStream(getOutputDir() + outputFileName)) {
49 // Save EPS file
50 document.save(outPsStream);
51 }
52 } catch (IOException ex) {
53 }You can download examples and data files from GitHub.