Đọc và thêm siêu dữ liệu XMP trong tài liệu EPS/PS bằng Java
Trích xuất, kiểm tra, thêm và cập nhật siêu dữ liệu XMP (Extensible Metadata Platform) trong các tài liệu EPS và PostScript (PS) bằng Aspose.Page for Java. Nhúng các thuộc tính Dublin Core tiêu chuẩn, thống kê tạo tài liệu, thông tin bản quyền và cấu trúc siêu dữ liệu tùy chỉnh trực tiếp vào các luồng tệp PostScript mà không làm thay đổi nội dung trực quan của đồ họa vector bên dưới.
Aspose.Page for Java cung cấp mô hình đối tượng XmpHeaderConverter và XmpMetadata hiệu suất cao để trích xuất siêu dữ liệu XMP hiện có hoặc chèn các thuộc tính mới vào tệp EPS/PS.
- Để đọc các thông tin như chi tiết tác giả, ngày tạo, thông báo bản quyền và các thuộc tính tùy chỉnh từ tệp EPS, hãy sử dụng phương thức
XmpMetadata.get(). - Để chỉnh sửa các trường siêu dữ liệu hiện có như tiêu đề tài liệu hoặc lịch sử sửa đổi, hãy sử dụng phương thức
XmpMetadata.set(). - Để thêm các thuộc tính siêu dữ liệu độc quyền và dành riêng cho doanh nghiệp vào các tệp đồ họa, hãy sử dụng phương thức
XmpMetadata.registerNamespace().
Đọc siêu dữ liệu XMP hiện có từ tệp EPS
Trích xuất và phân tích các thuộc tính XMP tiêu chuẩn và do người dùng xác định từ tài liệu EPS để kiểm tra các trường siêu dữ liệu như dc:title, xmp:CreatorTool và 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());Thêm và chèn siêu dữ liệu XMP mới vào tệp EPS
Chèn các thuộc tính siêu dữ liệu tiêu chuẩn (chẳng hạn như lược đồ Dublin Core hoặc XMP Basic) vào tài liệu EPS và lưu tệp đầu ra đã cập nhật.
// 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) {
}Thêm namespace có tên tùy chỉnh và siêu dữ liệu mảng
Đăng ký các namespace XML dành riêng cho doanh nghiệp và lưu trữ các kiểu dữ liệu phức tạp, chẳng hạn như danh sách mảng, bên trong cấu trúc XMP của tài liệu.
// 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) {
}Cài đặt và thiết lập
Thêm Aspose.Page for Java vào Maven của bạn:
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. Tôi có thể trích xuất siêu dữ liệu XMP từ tệp EPS không chứa tiêu đề XMP tiêu chuẩn không?
Nếu tệp EPS không có gói tiêu đề XMP hiện có, PsDocument.getXmpMetadata() sẽ trả về một cấu trúc XmpMetadata trống. Bạn có thể điền các thuộc tính vào đó và gọi document.save() để ghi một gói XMP hợp lệ mới vào tệp.
2. Aspose.Page for Java hỗ trợ những lược đồ XMP tiêu chuẩn nào?
Aspose.Page hỗ trợ gốc tất cả các lược đồ XMP tiêu chuẩn, bao gồm Dublin Core (dc:), XMP Basic (xmp:), XMP Rights Management (xmpRights:), XMP Media Management (xmpMM:) và lược đồ Adobe PDF (pdf:).
3. Việc cập nhật siêu dữ liệu XMP có mã hóa lại hoặc làm giảm chất lượng đồ họa EPS trực quan không?
Không. Aspose.Page sửa đổi hoặc chèn khối siêu dữ liệu tiêu đề XMP nằm trong phần mở đầu của tài liệu mà không thay đổi hoặc nén lại các hình vector, đồ họa raster hoặc mã PostScript.