ドキュメントプロパティの操作
ドキュメントプロパティを使用すると、ドキュメントに関する有用な情報を格納できます。 これらのプロパティは、2つのグループに分けることができます:
- 文書のタイトル、作成者名、文書の統計情報などの値を含むシステムまたは組み込み。
- ユーザーが名前と値の両方を定義できる名前と値のペアとして提供されます。
APIとバージョン番号に関する情報が出力文書に直接書き込まれることを知っておくと便利です。 たとえば、ドキュメントをPDFに変換すると、Aspose.Wordsは"Application"フィールドに"Aspose.Words"を入力し、“PDFProducer"フィールドに"Aspose.WordsforJavaYYを入力します。ここで、YY.M.Nは変換に使用されるAspose.Wordsのバージョンです。 詳細については、以下を参照してください 出力文書に含まれるジェネレータ名またはプロデューサー名.
ドキュメントのプロパティへのアクセス
Aspose.Wordsのドキュメントプロパティにアクセスするには、次を使用します:
-
BuiltInDocumentProperties組み込みプロパティを取得します。
-
CustomDocumentPropertiesカスタムプロパティを取得します。
BuiltInDocumentProperties
BuiltInDocumentProperties
DocumentPropertyクラスを使用すると、ドキュメントプロパティの名前、値、および型を取得できます。 [値]https://reference.aspose.com/words/java/com.aspose.words/documentproperty#Value)はオブジェクトを返しますが、プロパティ値を特定の型に変換できるメソッドのセットがあります。 プロパティの型がわかったら、DocumentProperty.[ToString]のようなDocumentProperty.ToXXXメソッドのいずれかを使用できます](https://reference.aspose.com/words/java/com.aspose.words/documentproperty/#toString)とDocumentProperty.ToIntは、適切な型の値を取得します。
次のコード例は、ドキュメント内のすべての組み込みプロパティとカスタムプロパティを列挙する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String fileName = dataDir + "Properties.doc"; | |
Document doc = new Document(fileName); | |
System.out.println("1. Document name: " + fileName); | |
System.out.println("2. Built-in Properties"); | |
for (DocumentProperty prop : doc.getBuiltInDocumentProperties()) | |
System.out.println(prop.getName() + " : " + prop.getValue()); | |
System.out.println("3. Custom Properties"); | |
for (DocumentProperty prop : doc.getCustomDocumentProperties()) | |
System.out.println(prop.getName() + " : " + prop.getValue()); |
Microsoft Wordでは、“ファイル→プロパティ"メニューを使用して文書のプロパティにアクセスできます。

ドキュメントプロパティの追加または削除
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-Java | |
Document doc = new Document(dataDir + "Properties.doc"); | |
CustomDocumentProperties props = doc.getCustomDocumentProperties(); | |
if (props.get("Authorized") == null) { | |
props.add("Authorized", true); | |
props.add("Authorized By", "John Smith"); | |
props.add("Authorized Date", new Date()); | |
props.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); | |
props.add("Authorized Amount", 123.45); | |
} |
カスタムドキュメントプロパティを削除する方法を次のコード例に示します:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Properties.doc"); | |
doc.getCustomDocumentProperties().remove("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-Java | |
Document doc = new Document(dataDir + "test.docx"); | |
// Retrieve a list of all custom document properties from the file. | |
CustomDocumentProperties customProperties = doc.getCustomDocumentProperties(); | |
// Add linked to content property. | |
DocumentProperty customProperty = customProperties.addLinkToContent("PropertyName", "BookmarkName"); | |
// Also, accessing the custom document property can be performed by using the | |
// property name. | |
customProperty = customProperties.get("PropertyName"); | |
// Check whether the property is linked to content. | |
boolean isLinkedToContent = customProperty.isLinkToContent(); | |
// Get the source of the property. | |
String source = customProperty.getLinkSource(); | |
// Get the value of the property. | |
String value = customProperty.getValue().toString(); |
ドキュメント変数の取得
Variablesプロパティを使用してドキュメント変数のコレクションを取得できます。 変数名と値は文字列です。
次のコード例は、ドキュメント変数を列挙する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
for (java.util.Map.Entry entry : doc.getVariables()) { | |
String name = entry.getKey().toString(); | |
String value = entry.getValue().toString(); | |
// Do something useful. | |
System.out.println("Name: " + name + ", Value: " + value); | |
} |
文書から個人情報を削除する
Word文書を他の人と共有する場合は、作成者名や会社などの個人情報を削除することをお勧めします。 これを行うには、RemovePersonalInformationプロパティを使用して、文書の保存時にMicrosoft Wordがコメント、リビジョン、および文書のプロパティからすべてのユーザー情報を削除することを示すフラグを設定します。