在 EPS 的 XMP 元数据中添加属性 | Java
为了在 EPS 文件的 XMP 元数据中添加属性,需要执行以下步骤:
- 初始化用于输入 EPS 文件的输入流。
- 从先前创建的输入流创建 PsDocument 的实例。
- 从 PsDocument 获取 XmpMetadata 的实例。如果给定的 EPS 文件不包含 XMP 元数据,则将创建新的实例,并使用 PS 元数据注释中的值填充该实例并返回给您。
- 现在您可以添加元数据文件的属性了。
- 初始化用于输出 EPS 文件的输出流。
- 保存包含更改后的 XMP 元数据的 EPS 文件。
以下代码片段展示了如何在 Java 中在 EPS 文件的 XMP 元数据中添加属性:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
2// Set license
3new License().setLicense(BaseExamplesTest.LICENSE_PATH);
4
5// The path to the documents directory.
6String dataDir = Utils.getDataDir();
7
8// Initialize input EPS file stream
9FileInputStream psStream = new FileInputStream(dataDir + "xmp3.eps");
10
11PsDocument document = new PsDocument(psStream);
12
13try {
14 // 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)
15 XmpMetadata xmp = document.getXmpMetadata();
16
17 // Add date property "xmp:Date1" value
18 TimeZone.setDefault( TimeZone.getTimeZone("UTC"));
19 Date now = new Date();
20 xmp.put("xmp:Date1", new XmpValue(now));
21
22 // Add integer property "xmp:Intg1" value
23 xmp.put("xmp:Intg1", new XmpValue(111));
24
25 // Add double property "xmp:Double1" value
26 xmp.put("xmp:Double1", new XmpValue(111.11D));
27
28 // Add string property "xmp:String1" value
29 xmp.put("xmp:String1", new XmpValue("ABC"));
30
31
32 // Initialize output EPS file stream
33 FileOutputStream outPsStream = new FileOutputStream(dataDir + "xmp3_changed.eps");
34
35 // Save document with changed XMP metadata
36 try {
37 document.save(outPsStream);
38 } finally {
39 outPsStream.close();
40 }
41
42} finally {
43 // close input EPS stream
44 psStream.close();
45}
您可以从 GitHub下载示例和数据文件。