ドキュメントプロパティの操作

ドキュメントプロパティを使用すると、ドキュメントに関する有用な情報を格納できます。 これらのプロパティは、2つのグループに分けることができます:

  • 文書のタイトル、作成者名、文書の統計情報などの値を含むシステムまたは組み込み。
  • ユーザーが名前と値の両方を定義できる名前と値のペアとして提供されます。

APIとバージョン番号に関する情報が出力ドキュメントに直接書き込まれることを知っておくと便利です。 たとえば、ドキュメントをPDFに変換すると、Aspose.Wordsは"Application"フィールドに"Aspose.Words"を入力し、“PDFProducer"フィールドに"PDF"を入力しますC++の場合Aspose.Words YY.ここで、YY.M.Nは変換に使用されるAspose.Wordsのバージョンです。 詳細については、以下を参照してください 出力文書に含まれるジェネレータ名またはプロデューサー名.

ドキュメントのプロパティへのアクセス

Aspose.Wordsのドキュメントプロパティにアクセスするには、次を使用します:

BuiltInDocumentProperties

BuiltInDocumentProperties

DocumentPropertyクラスを使用すると、ドキュメントプロパティの名前、値、および型を取得できます。 Valueはオブジェクトを返しますが、特定の型に変換されたプロパティ値を取得できるメソッドのセットがあります。 プロパティの型がわかったら、DocumentProperty.ToStringDocumentProperty.ToIntなどのDocumentProperty.ToXXXメソッドのいずれかを使用して、適切な型の値を取得できます。

次のコード例は、ドキュメント内のすべての組み込みプロパティとカスタムプロパティを列挙する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::String fileName = inputDataDir + u"Properties.doc";
System::SharedPtr<Document> doc = System::MakeObject<Document>(fileName);
std::cout << "1. Document name: " << fileName.ToUtf8String() << std::endl;
std::cout << "2. Built-in Properties" << std::endl;
for (System::SharedPtr<DocumentProperty> prop : System::IterateOver(doc->get_BuiltInDocumentProperties()))
{
std::cout << prop->get_Name().ToUtf8String() << " : " << prop->get_Value()->ToString().ToUtf8String() << std::endl;
}
std::cout << "3. Custom Properties" << std::endl;
for (System::SharedPtr<DocumentProperty> prop : System::IterateOver(doc->get_CustomDocumentProperties()))
{
std::cout << prop->get_Name().ToUtf8String() << " : " << prop->get_Value()->ToString().ToUtf8String() << std::endl;
}

Microsoft Wordでは、“ファイル→プロパティ"メニューを使用して文書のプロパティにアクセスできます。

work-with-document-properties-aspose-words-cpp_1.png

ドキュメントプロパティの追加または削除

Aspose.Wordsを使用して組み込みのドキュメントプロパティを追加または削除することはできません。 値を変更または更新できるのは、その値のみです。

Aspose.Wordsを使用してカスタムドキュメントプロパティを追加するには、Addメソッドを使用して、新しいプロパティ名と適切な型の値を渡します。 このメソッドは、新しく作成されたDocumentPropertyオブジェクトを返します。

カスタムプロパティを削除するには、Removeメソッドを使用して削除するプロパティ名を渡すか、RemoveAtメソッドを使用してインデックスでプロパティを Clearメソッドを使用してすべてのプロパティを削除することもできます。

次のコード例では、指定された名前のカスタムプロパティがドキュメントに存在するかどうかを確認し、さらにいくつかのカスタムドキュメントプ:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc");
System::SharedPtr<CustomDocumentProperties> props = doc->get_CustomDocumentProperties();
if (props->idx_get(u"Authorized") == nullptr)
{
props->Add(u"Authorized", true);
props->Add(u"Authorized By", System::String(u"John Smith"));
props->Add(u"Authorized Date", System::DateTime::get_Today());
props->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber());
props->Add(u"Authorized Amount", 123.45);
}

カスタムドキュメントプロパティを削除する方法を次のコード例に示します:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc");
doc->get_CustomDocumentProperties()->Remove(u"Authorized Date");

組み込みのドキュメントのプロパティを更新する

Aspose.Wordsは、Microsoft Wordのようにドキュメントプロパティを自動的に更新しませんが、一部の統計的な組み込みドキュメントプロパティを更新する方法を提供します。 次のプロパティを再計算して更新するには、UpdateWordCountメソッドを呼び出します:

コンテンツにリンクされた新しいカスタムプロパティを作成する

Aspose.Wordsは、コンテンツにリンクされた新しいカスタムドキュメントプロパティを作成するためのAddLinkToContentメソッドを提供します。 このプロパティは、新しく作成されたpropertyオブジェクトを返します。LinkSourceが無効な場合はnullを返します。

次のコード例は、カスタムプロパティへのリンクを構成する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"test.docx");
// Retrieve a list of all custom document properties from the file.
System::SharedPtr<CustomDocumentProperties> customProperties = doc->get_CustomDocumentProperties();
// Add linked to content property.
System::SharedPtr<DocumentProperty> customProperty = customProperties->AddLinkToContent(u"PropertyName", u"BookmarkName");
// Also, accessing the custom document property can be performed by using the property name.
customProperty = customProperties->idx_get(u"PropertyName");
// Check whether the property is linked to content.
bool isLinkedToContent = customProperty->get_IsLinkToContent();
// Get the source of the property.
System::String source = customProperty->get_LinkSource();
// Get the value of the property.
System::String value = customProperty->get_Value()->ToString();

ドキュメント変数の取得

Variablesプロパティを使用してドキュメント変数のコレクションを取得できます。 変数名と値は文字列です。

次のコード例は、ドキュメント変数を列挙する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithDocument();
// Load the template document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::String variables = u"";
for (System::Collections::Generic::KeyValuePair<System::String, System::String> entry : System::IterateOver(doc->get_Variables()))
{
System::String name = entry.get_Key();
System::String value = entry.get_Value();
if (variables == u"")
{
// Do something useful.
variables = System::String(u"Name: ") + name + u"," + u"Value: " + value;
}
else
{
variables = variables + u"Name: " + name + u"," + u"Value: " + value;
}
}

文書から個人情報を削除する

Word文書を他の人と共有する場合は、作成者名や会社などの個人情報を削除することをお勧めします。 これを行うには、RemovePersonalInformationプロパティを使用して、文書の保存時にMicrosoft Wordがコメント、リビジョン、および文書のプロパティからすべてのユーザー情報を削除することを示すフラグを設定します。

次のコード例は、個人情報を削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc");
doc->set_RemovePersonalInformation(true);
System::String outputPath = outputDataDir + u"DocProperties.RemovePersonalInformation.docx";
doc->Save(outputPath);