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

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

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


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

 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}

请参阅在 Java.NET 中添加 XMP 元数据。

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.