Add named value in XMP metadata of EPS | Java
In order to add named value 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 named value to structure 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 named value in XMP metadata in EPS file in Java:
1// Add named value in XMP metadata in EPS document.
2
3// Initialize EPS file input stream
4try (FileInputStream psStream = new FileInputStream(getDataDir() + "add_named_value_input.eps")) {
5 // Create PsDocument instance from stream
6 PsDocument document = new PsDocument(psStream);
7
8 String outputFileName = "add_xmp_named_value_out.eps";
9
10 try {
11 // 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)
12 XmpMetadata xmp = document.getXmpMetadata();
13
14 //Change XMP metadata values
15
16 // Add named value to "xmpTPg:MaxPageSize" structure.
17 xmp.addNamedValue("xmpTPg:MaxPageSize", "stDim:newKey", new XmpValue("NewValue"));
18
19 // Save EPS file with changed XMP metadata
20
21 // Create ouput stream
22 try (FileOutputStream outPsStream = new FileOutputStream(getOutputDir() + outputFileName)) {
23 // Save EPS file
24 document.save(outPsStream);
25 }
26 } catch (IOException ex) {
27 }You can download examples and data files from GitHub.