Add XMP metadata to EPS file using .NET
You can check the quality of Aspose.Page XMP Metadata working with Metadata web app
In order to add XMP metadata to EPS file it is necessary to do several steps:
- Initialize an input stream for input EPS file.
- Create an instance of PsDocument from created earlier input stream.
- Get an instance of XmpMetadata from the PsDocument. If given EPS file doesn’t contain XMP metadata the new one will be created, filled in with values from PS metadata comments and returned to you.
- Now you can view values of metadata fields.
- Initialize an output stream for output EPS file.
- Save EPS file with new XMP metadata.
The following code snippet shows how to add XMP metadata to EPS file in C#:
1// Add XMP metadata to EPS document.
2
3// Initialize EPS file input stream
4System.IO.FileStream psStream = new System.IO.FileStream(DataDir + "add_input.eps", System.IO.FileMode.Open, System.IO.FileAccess.Read);
5
6// Create PsDocument instance from stream. Given EPS file doesn't contain XMP metadata, but usual metadata does contain.
7PsDocument document = new PsDocument(psStream);
8
9string outputFileName = "add_xmp_metadata_out.eps";
10
11XmpMetadata xmp = null;
12
13try
14{
15 // 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)
16 xmp = document.GetXmpMetadata();
17
18 // Check metadata values extracted from PS metadata comments and set up in new XMP metadata
19
20 // Get "CreatorTool" value
21 if (xmp.Contains("xmp:CreatorTool"))
22 Console.WriteLine("CreatorTool: " + xmp["xmp:CreatorTool"].ToStringValue());
23
24 // Get "CreateDate" value
25 if (xmp.Contains("xmp:CreateDate"))
26 Console.WriteLine("CreateDate: " + xmp["xmp:CreateDate"].ToStringValue());
27
28 // Get "format" value
29 if (xmp.Contains("dc:format"))
30 Console.WriteLine("Format: " + xmp["dc:format"].ToStringValue());
31
32 // Get "title" value
33 if (xmp.Contains("dc:title"))
34 Console.WriteLine("Title: " + xmp["dc:title"].ToArray()[0].ToStringValue());
35
36 // Get "creator" value
37 if (xmp.Contains("dc:creator"))
38 Console.WriteLine("Creator: " + xmp["dc:creator"].ToArray()[0].ToStringValue());
39
40 // Get "MetadataDate" value
41 if (xmp.Contains("xmp:MetadataDate"))
42 Console.WriteLine("MetadataDate: " + xmp["xmp:MetadataDate"].ToStringValue());
43
44 // Update MetadataDate value
45 DateTime metadataDate = xmp["xmp:MetadataDate"].ToDateTime();
46
47 // Save EPS file with new XMP metadata
48
49 // Create ouput stream
50 using (System.IO.FileStream outPsStream = new System.IO.FileStream(OutputDir + outputFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
51 {
52 // Save EPS file
53 document.Save(outPsStream);
54 }
55}
56finally
57{
58 psStream.Close();
59}
Evaluate XMP metadata working online on our Metadata application.
You can download examples and data files from GitHub.