Add properties in XMP metadata of EPS | Java
In order to add properties in XMP metadata of 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 add properties of metadata fileds.
- Initialize an output stream for output EPS file.
- Save EPS file with changed XMP metadata.
The following code snippet shows how to add properties in XMP metadata of EPS file in Java:
1// Add simple properties in XMP metadata in EPS document.
2
3// Initialize EPS file input stream
4try (FileInputStream psStream = new FileInputStream(getDataDir() + "add_simple_props_input.eps")) {
5 // Create PsDocument instance from stream
6 PsDocument document = new PsDocument(psStream);
7
8 String outputFileName = "add_xmp_simple_props_out.eps";
9 ZoneId systemZone = ZoneId.systemDefault();
10 OffsetDateTime now = LocalDateTime.now().atZone(systemZone).toOffsetDateTime();
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 XmpMetadata xmp = document.getXmpMetadata();
15
16 //Change XMP metadata values
17
18 // Add Integer poperty
19 xmp.put("xmp:Intg1", new XmpValue(111));
20
21 // Add DateTime poperty
22 xmp.put("xmp:Date1", new XmpValue(Date.from(now.toInstant())));
23
24 // Add Double poperty
25 xmp.put("xmp:Double1", new XmpValue(111.11D));
26
27 // Add String poperty
28 xmp.put("xmp:String1", new XmpValue("ABC"));
29
30 // Save EPS file with changed XMP metadata
31
32 // Create ouput stream
33 try (FileOutputStream outPsStream = new FileOutputStream(getOutputDir() + outputFileName)) {
34 // Save EPS file
35 document.save(outPsStream);
36 }
37 } catch (IOException ex) {
38 }You can download examples and data files from GitHub.