使用 Java 读取和添加 EPS/PS 文档中的 XMP 元数据
使用 Aspose.Page for Java 在 EPS 和 PostScript (PS) 文档中提取、检查、添加和更新 XMP(可扩展元数据平台)元数据。将标准 Dublin Core 属性、文档创建统计信息、版权详细信息和自定义元数据结构直接嵌入 PostScript 文件流,而无需更改底层的矢量图形视觉内容。
Aspose.Page for Java 提供高性能的 XmpHeaderConverter 和 XmpMetadata 对象模型,用于提取现有 XMP 元数据或向 EPS/PS 文件注入新属性。
- 要从 EPS 文件中读取作者详细信息、创建日期、版权声明和自定义属性等信息,请使用
XmpMetadata.get()方法。 - 要编辑现有元数据字段,例如文档标题或修改历史记录,请使用
XmpMetadata.set()方法。 - 要向图形文件添加专有的、企业特定的元数据属性,请使用
XmpMetadata.registerNamespace()方法。
从 EPS 文件读取现有 XMP 元数据
从 EPS 文档中提取并解析标准和用户定义的 XMP 属性,以检查 dc:title、xmp:CreatorTool 和 xmp:CreateDate 等元数据字段。
// Get XMP metadata from EPS document.
// Learn more: https://docs.aspose.com/page/java/working-with-xm-metadata-in-eps/
// Create PsDocument instance from file
PsDocument document = new PsDocument(getDataDir() + "get_input.eps");
// 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)
XmpMetadata xmp = document.getXmpMetadata();
// Get "CreatorTool" value
if (xmp.containsKey("xmp:CreatorTool"))
System.out.println("CreatorTool: " + xmp.get("xmp:CreatorTool").toStringValue());
// Get "CreateDate" value
if (xmp.containsKey("xmp:CreateDate"))
System.out.println("CreateDate: " + xmp.get("xmp:CreateDate").toStringValue());
// Get a width of a thumbnail image if exists
if (xmp.containsKey("xmp:Thumbnails") && xmp.get("xmp:Thumbnails").isArray()) {
XmpValue val = xmp.get("xmp:Thumbnails").toArray()[0];
if (val.isNamedValues() && val.toDictionary().containsKey("xmpGImg:width"))
System.out.println("Thumbnail Width: " + val.toDictionary().get("xmpGImg:width").toInteger());
}
// Get "format" value
if (xmp.containsKey("dc:format"))
System.out.println("Format: " + xmp.get("dc:format").toStringValue());
// Get "DocumentID" value
if (xmp.containsKey("xmpMM:DocumentID"))
System.out.println("DocumentID: " + xmp.get("xmpMM:DocumentID").toStringValue());向 EPS 文件添加和注入新的 XMP 元数据
将标准元数据属性(例如 Dublin Core 或 XMP Basic 架构)注入 EPS 文档,并保存更新后的文件输出。
// Add XMP metadata to EPS document.
// Learn more: https://docs.aspose.com/page/java/working-with-xm-metadata-in-eps/
// Initialize EPS file input stream
try (FileInputStream psStream = new FileInputStream(getDataDir() + "add_input.eps")) {
// Create PsDocument instance from stream. Given EPS file doesn't contain XMP metadata, but usual metadata does contain.
PsDocument document = new PsDocument(psStream);
String outputFileName = "add_xmp_metadata_out.eps";
XmpMetadata xmp = null;
try {
// 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)
xmp = document.getXmpMetadata();
// Check metadata values extracted from PS metadata comments and set up in new XMP metadata
// Get "CreatorTool" value
if (xmp.containsKey("xmp:CreatorTool"))
System.out.println("CreatorTool: " + xmp.get("xmp:CreatorTool").toStringValue());
// Get "CreateDate" value
if (xmp.containsKey("xmp:CreateDate"))
System.out.println("CreateDate: " + xmp.get("xmp:CreateDate").toStringValue());
// Get "format" value
if (xmp.containsKey("dc:format"))
System.out.println("Format: " + xmp.get("dc:format").toStringValue());
// Get "title" value
if (xmp.containsKey("dc:title"))
System.out.println("Title: " + xmp.get("dc:title").toArray()[0].toStringValue());
// Get "creator" value
if (xmp.containsKey("dc:creator"))
System.out.println("Creator: " + xmp.get("dc:creator").toArray()[0].toStringValue());
// Get "MetadataDate" value
if (xmp.containsKey("xmp:MetadataDate"))
System.out.println("MetadataDate: " + xmp.get("xmp:MetadataDate").toStringValue());
// Update MetadataDate value
Date metadataDate = xmp.get("xmp:MetadataDate").toDateTime();
// Save EPS file with new XMP metadata
// Create ouput stream
try (FileOutputStream outPsStream = new FileOutputStream(getOutputDir() + outputFileName)) {
// Save EPS file
document.save(outPsStream);
}
} catch (IOException ex) {
}添加自定义命名空间和数组元数据
注册企业特定的 XML 命名空间,并在文档的 XMP 结构中存储复杂数据类型,例如数组列表。
// Add namespace in XMP metadata in EPS document.
// Learn more: https://docs.aspose.com/page/java/working-with-xm-metadata-in-eps/
// Initialize EPS file input stream
try (FileInputStream psStream = new FileInputStream(getDataDir() + "add_simple_props_input.eps")) {
// Create PsDocument instance from stream
PsDocument document = new PsDocument(psStream);
String outputFileName = "add_xmp_namespace_out.eps";
try {
// 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)
XmpMetadata xmp = document.getXmpMetadata();
//Change XMP metadata values
// Add new XML namespace "tmp".
xmp.registerNamespaceURI("tmp", "http://www.some.org/schema/tmp#");
// Add new string property in new namespace.
xmp.put("tmp:newKey", new XmpValue("NewValue"));
// Save EPS file with changed XMP metadata
// Create ouput stream
try (FileOutputStream outPsStream = new FileOutputStream(getOutputDir() + outputFileName)) {
// Save EPS file
document.save(outPsStream);
}
} catch (IOException ex) {
}安装和设置
将 Aspose.Page for Java 添加到您的 Maven:
Package Manager Console Command
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-page</artifactId>
<version>Latest</version>
</dependency>
FAQ
1. 我可以从不包含标准 XMP 标头的 EPS 文件中提取 XMP 元数据吗?
如果 EPS 文件缺少现有的 XMP 标头数据包,PsDocument.getXmpMetadata() 将返回一个空的 XmpMetadata 结构。您可以向其中填充属性并调用 document.save(),以便将新的有效 XMP 数据包写入文件。
2. Aspose.Page for Java 支持哪些标准 XMP 架构?
Aspose.Page 原生支持所有标准 XMP 架构,包括 Dublin Core (dc:)、XMP Basic (xmp:)、XMP Rights Management (xmpRights:)、XMP Media Management (xmpMM:) 和 Adobe PDF 架构 (pdf:)。
3. 更新 XMP 元数据是否会重新编码或降低 EPS 可视图形的质量?
不会。Aspose.Page 会修改或注入位于文档前导部分中的 XMP 标头元数据块,而不会更改或重新压缩矢量形状、光栅图形或 PostScript 代码。