Get XMP metadata from EPS file using Java
In order to extract XMP metadata from 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.
The following code snippet shows how to extract XMP metadata from EPS file in Java:
1// Get XMP metadata from EPS document.
2
3// Create PsDocument instance from file
4PsDocument document = new PsDocument(getDataDir() + "get_input.eps");
5
6// 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)
7XmpMetadata xmp = document.getXmpMetadata();
8
9// Get "CreatorTool" value
10if (xmp.containsKey("xmp:CreatorTool"))
11 System.out.println("CreatorTool: " + xmp.get("xmp:CreatorTool").toStringValue());
12
13// Get "CreateDate" value
14if (xmp.containsKey("xmp:CreateDate"))
15 System.out.println("CreateDate: " + xmp.get("xmp:CreateDate").toStringValue());
16
17// Get a width of a thumbnail image if exists
18if (xmp.containsKey("xmp:Thumbnails") && xmp.get("xmp:Thumbnails").isArray()) {
19 XmpValue val = xmp.get("xmp:Thumbnails").toArray()[0];
20 if (val.isNamedValues() && val.toDictionary().containsKey("xmpGImg:width"))
21 System.out.println("Thumbnail Width: " + val.toDictionary().get("xmpGImg:width").toInteger());
22}
23
24// Get "format" value
25if (xmp.containsKey("dc:format"))
26 System.out.println("Format: " + xmp.get("dc:format").toStringValue());
27
28// Get "DocumentID" value
29if (xmp.containsKey("xmpMM:DocumentID"))
30 System.out.println("DocumentID: " + xmp.get("xmpMM:DocumentID").toStringValue());You can download examples and data files from GitHub.