在 EPS 的 XMP 元数据中添加数组项 | Java
为了在 EPS 文件的 XMP 元数据中添加数组项,需要执行以下步骤:
- 初始化用于输入 EPS 文件的输入流。
- 从先前创建的输入流创建 PsDocument 的实例。
- 从 PsDocument 获取 XmpMetadata 的实例。如果给定的 EPS 文件不包含 XMP 元数据,则将创建新的实例,并使用 PS 元数据注释中的值填充该实例并返回给您。
- 现在,您可以向数组元数据文件添加项了。
- 初始化用于输出 EPS 文件的输出流。
- 保存包含更改后的 XMP 元数据的 EPS 文件。
以下代码片段展示了如何在 Java 中向 EPS 文件的 XMP 元数据中添加数组项:
1// Add array items 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_array_items_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 one more title. I will be added at the end of array by default.
17 xmp.addArrayItem("dc:title", new XmpValue("NewTitle"));
18
19 // Add one more creator. It will be added in the array by an index (0).
20 xmp.addArrayItem("dc:creator", 0, new XmpValue("NewCreator"));
21
22 // Save EPS file with changed XMP metadata
23
24 // Create ouput stream
25 try (FileOutputStream outPsStream = new FileOutputStream(getOutputDir() + outputFileName)) {
26 // Save EPS file
27 document.save(outPsStream);
28 }
29 } catch (IOException ex) {
30 }您可以从 GitHub下载示例和数据文件。