Aspose::Words::Properties::DocumentProperty Class Reference

Detailed Description

Represents a custom or built-in document property.

See also
Aspose::Words::Properties::DocumentPropertyCollection
Examples

Shows how to work with built-in document properties.

auto doc = MakeObject<Document>(MyDir + u"Properties.docx");
// The "Document" object contains some of its metadata in its members.
std::cout << "Document filename:\n\t \"" << doc->get_OriginalFileName() << "\"" << std::endl;
// The document also stores metadata in its built-in properties.
// Each built-in property is a member of the document's "BuiltInDocumentProperties" object.
std::cout << "Built-in Properties:" << std::endl;
for (const auto& docProperty : System::IterateOver(doc->get_BuiltInDocumentProperties()))
{
std::cout << docProperty->get_Name() << std::endl;
std::cout << String::Format(u"\tType:\t{0}", docProperty->get_Type()) << std::endl;
// Some properties may store multiple values.
if (System::ObjectExt::Is<System::Collections::Generic::ICollection<SharedPtr<System::Object>>>(docProperty->get_Value()))
{
for (const auto& value : System::IterateOver(
System::DynamicCast_noexcept<System::Collections::Generic::ICollection<SharedPtr<System::Object>>>(docProperty->get_Value())))
{
std::cout << "\tValue:\t\"" << value << "\"" << std::endl;
}
}
else
{
std::cout << "\tValue:\t\"" << docProperty->get_Value() << "\"" << std::endl;
}
}

#include <Aspose.Words.Cpp/Properties/DocumentProperty.h>

+ Inheritance diagram for Aspose::Words::Properties::DocumentProperty:

Public Member Functions

bool get_IsLinkToContent ()
 Shows whether this property is linked to content or not. More...
 
String get_LinkSource () const
 Gets the source of a linked custom document property. More...
 
String get_Name () const
 Returns the name of the property. More...
 
PropertyType get_Type () const
 Gets the data type of the property. More...
 
SharedPtr< Objectget_Value ()
 Gets or sets the value of the property. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_Value (const SharedPtr< Object > &value)
 Setter for get_Value. More...
 
bool ToBool ()
 Returns the property value as bool. More...
 
ArrayPtr< uint8_t > ToByteArray ()
 Returns the property value as byte array. More...
 
DateTime ToDateTime ()
 Returns the property value as DateTime in UTC. More...
 
double ToDouble ()
 Returns the property value as double. More...
 
int32_t ToInt ()
 Returns the property value as integer. More...
 
String ToString () const override
 Returns the property value as a string formatted according to the current locale. More...
 

Static Public Member Functions

static const TypeInfoType ()
 

Member Function Documentation

◆ get_IsLinkToContent()

bool Aspose::Words::Properties::DocumentProperty::get_IsLinkToContent ( )

Shows whether this property is linked to content or not.

Examples

Shows how to link a custom document property to a bookmark.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->StartBookmark(u"MyBookmark");
builder->Write(u"Hello world!");
builder->EndBookmark(u"MyBookmark");
// Link a new custom property to a bookmark. The value of this property
// will be the contents of the bookmark that it references in the "LinkSource" member.
SharedPtr<CustomDocumentProperties> customProperties = doc->get_CustomDocumentProperties();
SharedPtr<DocumentProperty> customProperty = customProperties->AddLinkToContent(u"Bookmark", u"MyBookmark");
ASPOSE_ASSERT_EQ(true, customProperty->get_IsLinkToContent());
ASSERT_EQ(u"MyBookmark", customProperty->get_LinkSource());
ASPOSE_ASSERT_EQ(u"Hello world!", customProperty->get_Value());
doc->Save(ArtifactsDir + u"DocumentProperties.LinkCustomDocumentPropertiesToBookmark.docx");

◆ get_LinkSource()

System::String Aspose::Words::Properties::DocumentProperty::get_LinkSource ( ) const

Gets the source of a linked custom document property.

Examples

Shows how to link a custom document property to a bookmark.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->StartBookmark(u"MyBookmark");
builder->Write(u"Hello world!");
builder->EndBookmark(u"MyBookmark");
// Link a new custom property to a bookmark. The value of this property
// will be the contents of the bookmark that it references in the "LinkSource" member.
SharedPtr<CustomDocumentProperties> customProperties = doc->get_CustomDocumentProperties();
SharedPtr<DocumentProperty> customProperty = customProperties->AddLinkToContent(u"Bookmark", u"MyBookmark");
ASPOSE_ASSERT_EQ(true, customProperty->get_IsLinkToContent());
ASSERT_EQ(u"MyBookmark", customProperty->get_LinkSource());
ASPOSE_ASSERT_EQ(u"Hello world!", customProperty->get_Value());
doc->Save(ArtifactsDir + u"DocumentProperties.LinkCustomDocumentPropertiesToBookmark.docx");

◆ get_Name()

System::String Aspose::Words::Properties::DocumentProperty::get_Name ( ) const

Returns the name of the property.

Cannot be null and cannot be an empty string.

Examples

Shows how to work with built-in document properties.

auto doc = MakeObject<Document>(MyDir + u"Properties.docx");
// The "Document" object contains some of its metadata in its members.
std::cout << "Document filename:\n\t \"" << doc->get_OriginalFileName() << "\"" << std::endl;
// The document also stores metadata in its built-in properties.
// Each built-in property is a member of the document's "BuiltInDocumentProperties" object.
std::cout << "Built-in Properties:" << std::endl;
for (const auto& docProperty : System::IterateOver(doc->get_BuiltInDocumentProperties()))
{
std::cout << docProperty->get_Name() << std::endl;
std::cout << String::Format(u"\tType:\t{0}", docProperty->get_Type()) << std::endl;
// Some properties may store multiple values.
if (System::ObjectExt::Is<System::Collections::Generic::ICollection<SharedPtr<System::Object>>>(docProperty->get_Value()))
{
for (const auto& value : System::IterateOver(
System::DynamicCast_noexcept<System::Collections::Generic::ICollection<SharedPtr<System::Object>>>(docProperty->get_Value())))
{
std::cout << "\tValue:\t\"" << value << "\"" << std::endl;
}
}
else
{
std::cout << "\tValue:\t\"" << docProperty->get_Value() << "\"" << std::endl;
}
}

◆ get_Type()

Aspose::Words::Properties::PropertyType Aspose::Words::Properties::DocumentProperty::get_Type ( ) const

Gets the data type of the property.

Examples

Shows how to work with built-in document properties.

auto doc = MakeObject<Document>(MyDir + u"Properties.docx");
// The "Document" object contains some of its metadata in its members.
std::cout << "Document filename:\n\t \"" << doc->get_OriginalFileName() << "\"" << std::endl;
// The document also stores metadata in its built-in properties.
// Each built-in property is a member of the document's "BuiltInDocumentProperties" object.
std::cout << "Built-in Properties:" << std::endl;
for (const auto& docProperty : System::IterateOver(doc->get_BuiltInDocumentProperties()))
{
std::cout << docProperty->get_Name() << std::endl;
std::cout << String::Format(u"\tType:\t{0}", docProperty->get_Type()) << std::endl;
// Some properties may store multiple values.
if (System::ObjectExt::Is<System::Collections::Generic::ICollection<SharedPtr<System::Object>>>(docProperty->get_Value()))
{
for (const auto& value : System::IterateOver(
System::DynamicCast_noexcept<System::Collections::Generic::ICollection<SharedPtr<System::Object>>>(docProperty->get_Value())))
{
std::cout << "\tValue:\t\"" << value << "\"" << std::endl;
}
}
else
{
std::cout << "\tValue:\t\"" << docProperty->get_Value() << "\"" << std::endl;
}
}

Shows how to work with a document's custom properties.

auto doc = MakeObject<Document>();
SharedPtr<CustomDocumentProperties> properties = doc->get_CustomDocumentProperties();
ASSERT_EQ(0, properties->get_Count());
// Custom document properties are key-value pairs that we can add to the document.
properties->Add(u"Authorized", true);
properties->Add(u"Authorized By", String(u"John Doe"));
properties->Add(u"Authorized Date", System::DateTime::get_Today());
properties->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber());
properties->Add(u"Authorized Amount", 123.45);
// The collection sorts the custom properties in alphabetic order.
ASSERT_EQ(1, properties->IndexOf(u"Authorized Amount"));
ASSERT_EQ(5, properties->get_Count());
// Print every custom property in the document.
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<DocumentProperty>>> enumerator = properties->GetEnumerator();
while (enumerator->MoveNext())
{
std::cout << String::Format(u"Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", enumerator->get_Current()->get_Name(),
enumerator->get_Current()->get_Type(), enumerator->get_Current()->get_Value())
<< std::endl;
}
}
// Display the value of a custom property using a DOCPROPERTY field.
auto builder = MakeObject<DocumentBuilder>(doc);
auto field = System::DynamicCast<FieldDocProperty>(builder->InsertField(u" DOCPROPERTY \"Authorized By\""));
field->Update();
ASSERT_EQ(u"John Doe", field->get_Result());
// We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom".
doc->Save(ArtifactsDir + u"DocumentProperties.DocumentPropertyCollection.docx");
// Below are three ways or removing custom properties from a document.
// 1 - Remove by index:
properties->RemoveAt(1);
ASSERT_FALSE(properties->Contains(u"Authorized Amount"));
ASSERT_EQ(4, properties->get_Count());
// 2 - Remove by name:
properties->Remove(u"Authorized Revision");
ASSERT_FALSE(properties->Contains(u"Authorized Revision"));
ASSERT_EQ(3, properties->get_Count());
// 3 - Empty the entire collection at once:
properties->Clear();
ASSERT_EQ(0, properties->get_Count());

◆ get_Value()

System::SharedPtr<System::Object> Aspose::Words::Properties::DocumentProperty::get_Value ( )

Gets or sets the value of the property.

Cannot be null.

Examples

Shows how to work with built-in document properties.

auto doc = MakeObject<Document>(MyDir + u"Properties.docx");
// The "Document" object contains some of its metadata in its members.
std::cout << "Document filename:\n\t \"" << doc->get_OriginalFileName() << "\"" << std::endl;
// The document also stores metadata in its built-in properties.
// Each built-in property is a member of the document's "BuiltInDocumentProperties" object.
std::cout << "Built-in Properties:" << std::endl;
for (const auto& docProperty : System::IterateOver(doc->get_BuiltInDocumentProperties()))
{
std::cout << docProperty->get_Name() << std::endl;
std::cout << String::Format(u"\tType:\t{0}", docProperty->get_Type()) << std::endl;
// Some properties may store multiple values.
if (System::ObjectExt::Is<System::Collections::Generic::ICollection<SharedPtr<System::Object>>>(docProperty->get_Value()))
{
for (const auto& value : System::IterateOver(
System::DynamicCast_noexcept<System::Collections::Generic::ICollection<SharedPtr<System::Object>>>(docProperty->get_Value())))
{
std::cout << "\tValue:\t\"" << value << "\"" << std::endl;
}
}
else
{
std::cout << "\tValue:\t\"" << docProperty->get_Value() << "\"" << std::endl;
}
}

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Properties::DocumentProperty::GetType ( ) const
overridevirtual

Reimplemented from System::Object.

◆ Is()

virtual bool Aspose::Words::Properties::DocumentProperty::Is ( const System::TypeInfo target) const
overridevirtual

Reimplemented from System::Object.

◆ set_Value()

void Aspose::Words::Properties::DocumentProperty::set_Value ( const System::SharedPtr< System::Object > &  value)

◆ ToBool()

bool Aspose::Words::Properties::DocumentProperty::ToBool ( )

Returns the property value as bool.

Throws an exception if the property type is not Boolean.

Examples

Shows various type conversion methods of custom document properties.

auto doc = MakeObject<Document>();
SharedPtr<CustomDocumentProperties> properties = doc->get_CustomDocumentProperties();
properties->Add(u"Authorized", true);
properties->Add(u"Authorized By", String(u"John Doe"));
properties->Add(u"Authorized Date", authDate);
properties->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber());
properties->Add(u"Authorized Amount", 123.45);
ASPOSE_ASSERT_EQ(true, properties->idx_get(u"Authorized")->ToBool());
ASSERT_EQ(u"John Doe", System::ObjectExt::ToString(properties->idx_get(u"Authorized By")));
ASSERT_EQ(authDate, properties->idx_get(u"Authorized Date")->ToDateTime());
ASSERT_EQ(1, properties->idx_get(u"Authorized Revision")->ToInt());
ASPOSE_ASSERT_EQ(123.45, properties->idx_get(u"Authorized Amount")->ToDouble());

◆ ToByteArray()

System::ArrayPtr<uint8_t> Aspose::Words::Properties::DocumentProperty::ToByteArray ( )

Returns the property value as byte array.

Throws an exception if the property type is not ByteArray.

Examples

Shows how to add a thumbnail to a document that we save as an Epub.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world!");
// If we save a document, whose "Thumbnail" property contains image data that we added, as an Epub,
// a reader that opens that document may display the image before the first page.
SharedPtr<BuiltInDocumentProperties> properties = doc->get_BuiltInDocumentProperties();
ArrayPtr<uint8_t> thumbnailBytes = System::IO::File::ReadAllBytes(ImageDir + u"Logo.jpg");
properties->set_Thumbnail(thumbnailBytes);
doc->Save(ArtifactsDir + u"DocumentProperties.Thumbnail.epub");
// We can extract a document's thumbnail image and save it to the local file system.
SharedPtr<DocumentProperty> thumbnail = doc->get_BuiltInDocumentProperties()->idx_get(u"Thumbnail");
System::IO::File::WriteAllBytes(ArtifactsDir + u"DocumentProperties.Thumbnail.gif", thumbnail->ToByteArray());

◆ ToDateTime()

System::DateTime Aspose::Words::Properties::DocumentProperty::ToDateTime ( )

Returns the property value as DateTime in UTC.

Throws an exception if the property type is not DateTime.

Microsoft Word stores only the date part (no time) for custom date properties.

Examples

Shows how to create a custom document property which contains a date and time.

auto doc = MakeObject<Document>();
doc->get_CustomDocumentProperties()->Add(u"AuthorizationDate", System::DateTime::get_Now());
std::cout << "Document authorized on " << doc->get_CustomDocumentProperties()->idx_get(u"AuthorizationDate")->ToDateTime() << std::endl;

Shows various type conversion methods of custom document properties.

auto doc = MakeObject<Document>();
SharedPtr<CustomDocumentProperties> properties = doc->get_CustomDocumentProperties();
properties->Add(u"Authorized", true);
properties->Add(u"Authorized By", String(u"John Doe"));
properties->Add(u"Authorized Date", authDate);
properties->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber());
properties->Add(u"Authorized Amount", 123.45);
ASPOSE_ASSERT_EQ(true, properties->idx_get(u"Authorized")->ToBool());
ASSERT_EQ(u"John Doe", System::ObjectExt::ToString(properties->idx_get(u"Authorized By")));
ASSERT_EQ(authDate, properties->idx_get(u"Authorized Date")->ToDateTime());
ASSERT_EQ(1, properties->idx_get(u"Authorized Revision")->ToInt());
ASPOSE_ASSERT_EQ(123.45, properties->idx_get(u"Authorized Amount")->ToDouble());

◆ ToDouble()

double Aspose::Words::Properties::DocumentProperty::ToDouble ( )

Returns the property value as double.

Examples

Shows various type conversion methods of custom document properties.

auto doc = MakeObject<Document>();
SharedPtr<CustomDocumentProperties> properties = doc->get_CustomDocumentProperties();
properties->Add(u"Authorized", true);
properties->Add(u"Authorized By", String(u"John Doe"));
properties->Add(u"Authorized Date", authDate);
properties->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber());
properties->Add(u"Authorized Amount", 123.45);
ASPOSE_ASSERT_EQ(true, properties->idx_get(u"Authorized")->ToBool());
ASSERT_EQ(u"John Doe", System::ObjectExt::ToString(properties->idx_get(u"Authorized By")));
ASSERT_EQ(authDate, properties->idx_get(u"Authorized Date")->ToDateTime());
ASSERT_EQ(1, properties->idx_get(u"Authorized Revision")->ToInt());
ASPOSE_ASSERT_EQ(123.45, properties->idx_get(u"Authorized Amount")->ToDouble());

◆ ToInt()

int32_t Aspose::Words::Properties::DocumentProperty::ToInt ( )

Returns the property value as integer.

Examples

Shows various type conversion methods of custom document properties.

auto doc = MakeObject<Document>();
SharedPtr<CustomDocumentProperties> properties = doc->get_CustomDocumentProperties();
properties->Add(u"Authorized", true);
properties->Add(u"Authorized By", String(u"John Doe"));
properties->Add(u"Authorized Date", authDate);
properties->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber());
properties->Add(u"Authorized Amount", 123.45);
ASPOSE_ASSERT_EQ(true, properties->idx_get(u"Authorized")->ToBool());
ASSERT_EQ(u"John Doe", System::ObjectExt::ToString(properties->idx_get(u"Authorized By")));
ASSERT_EQ(authDate, properties->idx_get(u"Authorized Date")->ToDateTime());
ASSERT_EQ(1, properties->idx_get(u"Authorized Revision")->ToInt());
ASPOSE_ASSERT_EQ(123.45, properties->idx_get(u"Authorized Amount")->ToDouble());

◆ ToString()

System::String Aspose::Words::Properties::DocumentProperty::ToString ( ) const
overridevirtual

Returns the property value as a string formatted according to the current locale.

Converts a boolean property into "Y" or "N". Converts a date property into a short date string. For all other types converts a property using Object.ToString().

Examples

Shows how to work with custom document properties.

auto doc = MakeObject<Document>(MyDir + u"Properties.docx");
// Every document contains a collection of custom properties, which, like the built-in properties, are key-value pairs.
// The document has a fixed list of built-in properties. The user creates all of the custom properties.
ASSERT_EQ(u"Value of custom document property", System::ObjectExt::ToString(doc->get_CustomDocumentProperties()->idx_get(u"CustomProperty")));
doc->get_CustomDocumentProperties()->Add(u"CustomProperty2", String(u"Value of custom document property #2"));
std::cout << "Custom Properties:" << std::endl;
for (const auto& customDocumentProperty : System::IterateOver(doc->get_CustomDocumentProperties()))
{
std::cout << customDocumentProperty->get_Name() << std::endl;
std::cout << String::Format(u"\tType:\t{0}", customDocumentProperty->get_Type()) << std::endl;
std::cout << "\tValue:\t\"" << customDocumentProperty->get_Value() << "\"" << std::endl;
}

Shows various type conversion methods of custom document properties.

auto doc = MakeObject<Document>();
SharedPtr<CustomDocumentProperties> properties = doc->get_CustomDocumentProperties();
properties->Add(u"Authorized", true);
properties->Add(u"Authorized By", String(u"John Doe"));
properties->Add(u"Authorized Date", authDate);
properties->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber());
properties->Add(u"Authorized Amount", 123.45);
ASPOSE_ASSERT_EQ(true, properties->idx_get(u"Authorized")->ToBool());
ASSERT_EQ(u"John Doe", System::ObjectExt::ToString(properties->idx_get(u"Authorized By")));
ASSERT_EQ(authDate, properties->idx_get(u"Authorized Date")->ToDateTime());
ASSERT_EQ(1, properties->idx_get(u"Authorized Revision")->ToInt());
ASPOSE_ASSERT_EQ(123.45, properties->idx_get(u"Authorized Amount")->ToDouble());

Reimplemented from System::Object.

◆ Type()

static const System::TypeInfo& Aspose::Words::Properties::DocumentProperty::Type ( )
static