在 EPS 的 XMP 元数据中添加属性 | Java
为了在 EPS 文件的 XMP 元数据中添加属性,需要执行以下步骤:
- 初始化用于输入 EPS 文件的输入流。
- 从先前创建的输入流创建 PsDocument 的实例。
- 从 PsDocument 获取 XmpMetadata 的实例。如果给定的 EPS 文件不包含 XMP 元数据,则将创建新的实例,并使用 PS 元数据注释中的值填充该实例并返回给您。
- 现在您可以添加元数据文件的属性了。
- 初始化用于输出 EPS 文件的输出流。
- 保存包含更改后的 XMP 元数据的 EPS 文件。
以下代码片段展示了如何在 Java 中在 EPS 文件的 XMP 元数据中添加属性:
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 }您可以从 GitHub下载示例和数据文件。