Add XMP metadata to EPS file using C++

In order to add XMP metadata to EPS file it is necessary to do several steps:

  1. Initialize an input stream for input EPS file.
  2. Create an instance of PsDocument from created earlier input stream.
  3. 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.
  4. Now you can view values of metadata fields.
  5. Initialize an output stream for output EPS file.
  6. Save EPS file with new XMP metadata.


Following code snippet shows how to add XMP metadata to EPS file in C++:

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
 2// The path to the documents directory.
 3System::String dataDir = RunExamples::GetDataDir_WorkingWithXMPMetadataInEPS();
 4// Initialize EPS file input stream
 5System::SharedPtr<System::IO::FileStream> psStream = System::MakeObject<System::IO::FileStream>(dataDir + u"add_input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
 6// Create PsDocument instance from stream
 7System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(psStream);
 8
 9
10{
11    auto __finally_guard_0 = ::System::MakeScopeGuard([&psStream]()
12    {
13        psStream->Close();
14    });
15    
16    try
17    {
18        // 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)
19        System::SharedPtr<XmpMetadata> xmp = document->GetXmpMetadata();
20        
21        // Check metadata values extracted from PS metadata comments and set up in new XMP metadata
22        
23        // Get "CreatorTool" value
24        if (xmp->Contains(u"xmp:CreatorTool"))
25        {
26            System::Console::WriteLine(System::String(u"CreatorTool: ") + xmp->idx_get(u"xmp:CreatorTool")->ToStringValue());
27        }
28        
29        // Get "CreateDate" value
30        if (xmp->Contains(u"xmp:CreateDate"))
31        {
32            System::Console::WriteLine(System::String(u"CreateDate: ") + xmp->idx_get(u"xmp:CreateDate")->ToStringValue());
33        }
34        
35        // Get "format" value
36        if (xmp->Contains(u"dc:format"))
37        {
38            System::Console::WriteLine(System::String(u"Format: ") + xmp->idx_get(u"dc:format")->ToStringValue());
39        }
40        
41        // Get "title" value
42        if (xmp->Contains(u"dc:title"))
43        {
44            System::Console::WriteLine(System::String(u"Title: ") + xmp->idx_get(u"dc:title")->ToArray()->idx_get(0)->ToStringValue());
45        }
46        
47        // Get "creator" value
48        if (xmp->Contains(u"dc:creator"))
49        {
50            System::Console::WriteLine(System::String(u"Creator: ") + xmp->idx_get(u"dc:creator")->ToArray()->idx_get(0)->ToStringValue());
51        }
52        
53        // Get "MetadataDate" value
54        if (xmp->Contains(u"xmp:MetadataDate"))
55        {
56            System::Console::WriteLine(System::String(u"MetadataDate: ") + xmp->idx_get(u"xmp:MetadataDate")->ToStringValue());
57        }
58        
59        // Save EPS file with new XMP metadata
60        
61        // Create ouput stream
62        System::SharedPtr<System::IO::FileStream> outPsStream = System::MakeObject<System::IO::FileStream>(RunExamples::GetOutDir() + u"add_output.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
63        
64        // Save EPS file
65        
66        {
67            auto __finally_guard_1 = ::System::MakeScopeGuard([&outPsStream]()
68            {
69                outPsStream->Close();
70            });
71            
72            try
73            {
74                document->Save(outPsStream);
75                outPsStream->Flush();
76            }
77            catch (...)
78            {
79                throw;
80            }
81        }
82        
83    }
84    catch (...)
85    {
86        throw;
87    }
88}

See adding XMP metadata in Java and .NET.



You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.