Manage Tags and Custom Data in Presentations Using C++
Overview
This article explains how Aspose.Slides works with tags and custom data in PowerPoint presentations. Presentation-specific data can be stored as tags or custom XML parts. Tags are simple key-value string pairs, while custom XML parts can store structured metadata and application-specific XML payloads.
Aspose.Slides provides APIs for adding, reading, updating, auditing, and removing custom XML parts at the presentation, slide, and shape levels. Custom XML parts are useful for integrations that store information such as document-management identifiers, workflow state, compliance metadata, template-binding data, or other structured application data inside a presentation.
Data Storage in Presentation Files
PPTX files—files with the .pptx extension—are stored in the PresentationML format, which is part of the Office Open XML specification. Office Open XML defines the package structure and relationships used to store presentation content and related data.
A presentation contains multiple parts connected by relationships. For example, a slide part contains the content of a single slide and can have explicit relationships to other parts defined by ISO/IEC 29500.
Custom data can be stored as tags (ITagCollection) or custom XML parts (ICustomXmlPartCollection). Both are available through the ICustomData interface.
Work with Custom XML Parts
The ICustomData::get_CustomXmlParts method returns the collection of custom XML parts associated with a particular presentation object. For example:
presentation->get_CustomData()->get_CustomXmlParts()contains custom XML parts associated with the presentation itself.slide->get_CustomData()->get_CustomXmlParts()contains custom XML parts associated with a specific slide.shape->get_CustomData()->get_CustomXmlParts()contains custom XML parts associated with a specific shape.
Use Presentation::get_AllCustomXmlParts when you need to inspect all custom XML parts in the presentation regardless of where they are associated.
Add a Custom XML Part to a Presentation
Use ICustomXmlPartCollection::Add to add XML data to a custom XML part collection. The XML must be valid and non-empty.
The following example adds structured metadata to the presentation-level custom data collection:
#include <DOM/ICustomData.h>
#include <DOM/ICustomXmlPart.h>
#include <DOM/ICustomXmlPartCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/guid.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
System::String customXmlContent =
u"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
u"<metadata xmlns=\"urn:example:metadata\">"
u"<documentId>DOC-1001</documentId>"
u"<workflowState>Draft</workflowState>"
u"</metadata>";
auto presentation = System::MakeObject<Presentation>();
auto customXmlPart = presentation->get_CustomData()->get_CustomXmlParts()->Add(customXmlContent);
// Add assigns an identifier automatically. Set a specific GUID only when required.
customXmlPart->set_ItemId(System::Guid::NewGuid());
presentation->Save(u"presentation_with_custom_xml.pptx", SaveFormat::Pptx);
The Add method can also accept XML as a byte array or stream, which is useful when XML content is already available in binary form.
Add a Custom XML Part to a Slide or Shape
Custom XML data can be associated with a specific slide or shape instead of the whole presentation. This is useful when metadata describes only one object, such as a template key, external record identifier, or binding information.
The following example adds one custom XML part to a slide and another to a shape:
#include <DOM/IAutoShape.h>
#include <DOM/ICustomData.h>
#include <DOM/ICustomXmlPartCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/ITextFrame.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slides()->idx_get(0);
slide->get_CustomData()->get_CustomXmlParts()->Add(
u"<slideMetadata xmlns=\"urn:example:slides\">"
u"<templateKey>TitleSlide</templateKey>"
u"</slideMetadata>");
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50.0f, 50.0f, 250.0f, 80.0f);
shape->get_TextFrame()->set_Text(u"Customer data");
shape->get_CustomData()->get_CustomXmlParts()->Add(
u"<shapeMetadata xmlns=\"urn:example:shapes\">"
u"<recordId>CRM-4281</recordId>"
u"</shapeMetadata>");
presentation->Save(u"object_custom_xml.pptx", SaveFormat::Pptx);
The level at which a part is added determines which object’s get_CustomData()->get_CustomXmlParts() collection contains the relationship to that part. Presentation-level data is appropriate for document-wide metadata, slide-level data for information that belongs to a particular slide, and shape-level data for metadata tied to an individual shape.
List and Audit All Custom XML Parts
Use Presentation::get_AllCustomXmlParts to retrieve all custom XML parts from a presentation. Each ICustomXmlPart exposes its identifier, XML content, and associated namespace schemas.
The following example lists all custom XML parts and their namespace schemas:
#include <DOM/ICustomXmlPart.h>
#include <DOM/Presentation.h>
#include <system/console.h>
using namespace Aspose::Slides;
auto presentation = System::MakeObject<Presentation>(u"presentation.pptx");
for (auto customXmlPart : presentation->get_AllCustomXmlParts())
{
System::Console::WriteLine(System::String(u"ItemId: ") + customXmlPart->get_ItemId().ToString());
System::Console::WriteLine(u"XML:");
System::Console::WriteLine(customXmlPart->get_XmlAsString());
for (auto namespaceSchema : customXmlPart->get_NamespaceSchemas())
{
System::Console::WriteLine(System::String(u"Namespace schema: ") + namespaceSchema);
}
System::Console::WriteLine();
}
ICustomXmlPart::get_NamespaceSchemas returns the XML schemas associated with the custom XML part. This information can be useful when auditing presentations that contain XML produced by external systems.
Read and Update XML Content and ItemId
Use ICustomXmlPart::get_XmlAsString and set_XmlAsString to work with XML as a UTF-8 string, or ICustomXmlPart::get_XmlData and set_XmlData to work with the raw XML bytes. Both representations can be read and updated.
The ICustomXmlPart::get_ItemId method returns the GUID that identifies the custom XML part in the Office Open XML document. The identifier can also be changed with set_ItemId when an integration requires a new identifier.
The following example updates the XML content and the identifier:
#include <DOM/ICustomXmlPart.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/console.h>
#include <system/guid.h>
#include <system/text/encoding.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>(u"presentation.pptx");
auto customXmlPart = presentation->get_AllCustomXmlParts()->idx_get(0);
// Read the current XML as text.
auto currentXmlContent = customXmlPart->get_XmlAsString();
System::Console::WriteLine(currentXmlContent);
// Update the XML as a UTF-8 string.
customXmlPart->set_XmlAsString(
u"<metadata xmlns=\"urn:example:metadata\">"
u"<documentId>DOC-1001</documentId>"
u"<workflowState>Approved</workflowState>"
u"</metadata>");
// XmlData provides the same XML content as raw bytes.
auto customXmlData = customXmlPart->get_XmlData();
System::Console::WriteLine(System::Text::Encoding::get_UTF8()->GetString(customXmlData));
// Replace the identifier when required by the integration.
customXmlPart->set_ItemId(System::Guid::NewGuid());
presentation->Save(u"updated_custom_xml.pptx", SaveFormat::Pptx);
When assigning XML with set_XmlAsString or set_XmlData, provide valid, non-empty XML. Use one representation or the other depending on whether the application works primarily with strings or byte data.
Remove a Custom XML Part
Aspose.Slides provides several ways to remove custom XML data:
ICustomXmlPart::Removeremoves the custom XML part from the presentation.ICustomXmlPartCollection::Removeremoves a specific part from a custom XML part collection.ICustomXmlPartCollection::RemoveAtremoves the part at a specified collection index.ICustomXmlPartCollection::Clearremoves all parts from a specific collection.
The following example removes one presentation-level custom XML part by reference:
#include <DOM/ICustomData.h>
#include <DOM/ICustomXmlPartCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>(u"presentation.pptx");
auto customXmlParts = presentation->get_CustomData()->get_CustomXmlParts();
if (customXmlParts->get_Count() > 0)
{
auto customXmlPart = customXmlParts->idx_get(0);
customXmlParts->Remove(customXmlPart);
}
presentation->Save(u"custom_xml_removed.pptx", SaveFormat::Pptx);
If you already have an ICustomXmlPart and want to remove that part from the presentation rather than addressing a particular collection, call customXmlPart->Remove().
You can also remove an item by index:
presentation->get_CustomData()->get_CustomXmlParts()->RemoveAt(0);
Clear All Custom XML Parts from a Collection
Use Clear when all custom XML parts associated with a particular presentation object should be removed.
#include <DOM/ICustomData.h>
#include <DOM/ICustomXmlPartCollection.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>(u"presentation.pptx");
presentation->get_Slides()->idx_get(0)->get_CustomData()->get_CustomXmlParts()->Clear();
presentation->Save(u"slide_custom_xml_cleared.pptx", SaveFormat::Pptx);
Clear affects only the selected collection. For example, clearing a slide’s collection does not clear the presentation-level or shape-level collections.
To remove every custom XML part in the presentation, iterate through get_AllCustomXmlParts() and remove each part:
#include <DOM/ICustomXmlPart.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>(u"presentation.pptx");
for (auto customXmlPart : presentation->get_AllCustomXmlParts())
{
customXmlPart->Remove();
}
presentation->Save(u"all_custom_xml_removed.pptx", SaveFormat::Pptx);
Handle Linked or Shared Custom XML Parts
In an Office Open XML presentation, the same custom XML part can be referenced from more than one presentation object. For example, an existing file can contain relationships from multiple slides or shapes to the same underlying custom XML part.
A shared part should be treated as one data object with multiple references:
- Updating it with
set_XmlAsString,set_XmlData, orset_ItemIdchanges the underlying custom XML part, so the change applies wherever that part is referenced. get_ItemId()can be used to identify the same custom XML part while auditing object-level collections.- Removing a part from a specific
get_CustomXmlParts()collection removes it from that collection. UseICustomXmlPart::Remove()when the part itself should be removed from the presentation. - Before deleting or replacing a shared part, inspect the object-level collections to determine whether other slides or shapes still reference it.
The Add overloads create a new custom XML part from XML content; they do not accept an existing ICustomXmlPart. Therefore, shared relationships are most commonly encountered when loading presentations that already contain them.
The following example audits presentation-, slide-, and shape-level collections by ItemId and reports parts referenced from more than one place:
#include <algorithm>
#include <vector>
#include <DOM/ICustomData.h>
#include <DOM/ICustomXmlPart.h>
#include <DOM/ICustomXmlPartCollection.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/guid.h>
#include <system/string.h>
using namespace Aspose::Slides;
struct CustomXmlReferenceEntry
{
System::Guid itemId;
std::vector<System::String> owners;
};
auto presentation = System::MakeObject<Presentation>(u"presentation.pptx");
std::vector<CustomXmlReferenceEntry> referencesByItemId;
auto registerCustomXmlParts = [&referencesByItemId](
const System::String& ownerName,
const System::SharedPtr<ICustomXmlPartCollection>& customXmlParts)
{
for (int32_t partIndex = 0; partIndex < customXmlParts->get_Count(); ++partIndex)
{
auto customXmlPart = customXmlParts->idx_get(partIndex);
auto itemId = customXmlPart->get_ItemId();
auto entry = std::find_if(
referencesByItemId.begin(),
referencesByItemId.end(),
[&itemId](const CustomXmlReferenceEntry& referenceEntry)
{
return referenceEntry.itemId == itemId;
});
if (entry == referencesByItemId.end())
{
referencesByItemId.push_back({ itemId, { ownerName } });
}
else
{
entry->owners.push_back(ownerName);
}
}
};
registerCustomXmlParts(u"Presentation", presentation->get_CustomData()->get_CustomXmlParts());
for (int32_t slideIndex = 0; slideIndex < presentation->get_Slides()->get_Count(); ++slideIndex)
{
auto slide = presentation->get_Slides()->idx_get(slideIndex);
registerCustomXmlParts(
System::String::Format(u"Slide {0}", slideIndex + 1),
slide->get_CustomData()->get_CustomXmlParts());
for (int32_t shapeIndex = 0; shapeIndex < slide->get_Shapes()->get_Count(); ++shapeIndex)
{
auto shape = slide->get_Shapes()->idx_get(shapeIndex);
registerCustomXmlParts(
System::String::Format(u"Slide {0}, shape {1}", slideIndex + 1, shapeIndex),
shape->get_CustomData()->get_CustomXmlParts());
}
}
for (const auto& referenceEntry : referencesByItemId)
{
if (referenceEntry.owners.size() > 1)
{
System::Console::WriteLine(
System::String(u"Shared custom XML part: ") + referenceEntry.itemId.ToString());
for (const auto& ownerName : referenceEntry.owners)
{
System::Console::WriteLine(System::String(u" Referenced by: ") + ownerName);
}
}
}
This type of audit is useful before modifying or deleting custom XML data in presentations created by external systems, because the same metadata part may participate in more than one relationship.
Get Values of Tags
In slides, a tag corresponds to the IDocumentProperties::get_Keywords property. This sample code shows how to get a tag value with Aspose.Slides for C++ for Presentation:
#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
using namespace Aspose::Slides;
auto presentation = System::MakeObject<Presentation>(u"presentation.pptx");
auto keywords = presentation->get_DocumentProperties()->get_Keywords();
Add Tags to Presentations
Aspose.Slides allows you to add tags to presentations. A tag typically consists of two items:
- the name of a custom property, for example,
MyTag; - the value of the custom property, for example,
My Tag Value.
If you need to classify presentations based on a specific rule or property, you can add tags for that purpose. For example, if you want to categorize presentations from North American countries, you can create a North American tag and assign the relevant country as its value.
This sample code shows how to add a tag to a Presentation using Aspose.Slides for C++:
#include <DOM/ICustomData.h>
#include <DOM/ITagCollection.h>
#include <DOM/Presentation.h>
using namespace Aspose::Slides;
auto presentation = System::MakeObject<Presentation>(u"presentation.pptx");
auto tags = presentation->get_CustomData()->get_Tags();
tags->idx_set(u"MyTag", u"My Tag Value");
Tags can also be set for a Slide:
#include <DOM/ICustomData.h>
#include <DOM/ISlideCollection.h>
#include <DOM/ITagCollection.h>
#include <DOM/Presentation.h>
using namespace Aspose::Slides;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slides()->idx_get(0);
slide->get_CustomData()->get_Tags()->idx_set(u"tag", u"value");
Or for an individual Shape:
#include <DOM/IAutoShape.h>
#include <DOM/ICustomData.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/ITagCollection.h>
#include <DOM/ITextFrame.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
using namespace Aspose::Slides;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slides()->idx_get(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 50.0f);
shape->get_TextFrame()->set_Text(u"My text");
shape->get_CustomData()->get_Tags()->idx_set(u"tag", u"value");
Limitations
Tags added through the get_CustomData()->get_Tags() collection are stored only in the PowerPoint file. They are not transferred to the PDF tag structure when the presentation is exported to PDF. Consequently, a custom identifier assigned as a tag cannot be retrieved from the tagged PDF.
Workaround: You can store a custom identifier in the object’s Alt Text (for example, shape->set_AlternativeText(u"MyId")). After exporting to PDF, the Alt Text may appear in the PDF tag structure.
FAQ
Can I remove all tags from a presentation, slide, or shape in one operation?
Yes. The tag collection supports a Clear operation that deletes all key-value pairs at once.
How do I delete a single tag by its name without iterating over the whole collection?
Use Remove(name) on TagCollection to delete the tag by its key.
How can I retrieve the complete list of tag names for analytics or filtering?
Use GetNamesOfTags on the tag collection; it returns an array of all tag names.
How can I find all custom XML parts regardless of where they are stored?
Use Presentation::get_AllCustomXmlParts to retrieve all custom XML parts in the presentation.
Should I use get_XmlAsString/set_XmlAsString or get_XmlData/set_XmlData to update a custom XML part?
Use get_XmlAsString and set_XmlAsString when the application works with UTF-8 XML text. Use get_XmlData and set_XmlData when the XML is already available as a byte array or when binary-oriented processing is more convenient. Both representations refer to the XML content of the same custom XML part.