使用 Java 将 XMP 元数据添加到 EPS 文件

要将 XMP 元数据添加到 EPS 文件,需要执行以下几个步骤:

  1. 为输入 EPS 文件初始化输入流。
  2. 从先前创建的输入流创建 PsDocument 实例。
  3. 从 PsDocument 获取 XmpMetadata 实例。如果给定的 EPS 文件不包含 XMP 元数据,则将创建新的实例,并使用 PS 元数据注释中的值填充该实例并返回给您。
  4. 现在您可以查看元数据字段的值。
  5. 为输出 EPS 文件初始化输出流。
  6. 使用新的 XMP 元数据保存 EPS 文件。


以下代码片段展示了如何在 Java 中将 XMP 元数据添加到 EPS 文件:

 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 + "xmp2.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    // Get "CreatorTool" value
18    if (xmp.containsKey("xmp:CreatorTool"))
19        System.out.println("CreatorTool: " + xmp.get("xmp:CreatorTool").toStringValue());
20    
21    // Get "CreateDate" value
22    if (xmp.containsKey("xmp:CreateDate"))
23        System.out.println("CreateDate: " + xmp.get("xmp:CreateDate").toStringValue());
24
25    // Get "Title" value
26    if (xmp.containsKey("dc:title"))
27        System.out.println("Title: " + xmp.get("dc:title").toArray()[0].toStringValue());
28
29    // Get "format" value
30    if (xmp.containsKey("dc:format"))
31        System.out.println("Format: " + xmp.get("dc:format").toStringValue());
32
33    // Get "creator" value
34    if (xmp.containsKey("dc:creator"))
35        System.out.println("Creator: " + xmp.get("dc:creator").toArray()[0].toStringValue());
36    
37    // Get "MetadataDate" value
38    if (xmp.containsKey("xmp:MetadataDate"))
39        System.out.println("MetadataDate: " + xmp.get("xmp:MetadataDate").toStringValue());
40    
41    
42    // Initialize output EPS file stream
43    FileOutputStream outPsStream = new FileOutputStream(dataDir + "xmp2_changed.eps");
44    
45    // Save document with new XMP metadata
46		try {			
47			document.save(outPsStream);
48		} finally {
49			outPsStream.close();
50		}
51
52} finally {
53    // close input EPS stream
54		psStream.close();
55}

请参阅在 .NETC++ 中添加 XMP 元数据。

您可以从 GitHub下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.