ドキュメントのプロパティとは、タイトル、サブタイトル、作成者、会社名、キーワードなど、Word ドキュメントに含まれるいくつかの重要な情報です。この情報を読み取ることで、ドキュメントの特定、ドキュメントの種類や出所の迅速な確認、ドキュメントの保管や整理の効率化などに役立てることができます。ただし、作成者や会社名などの文書のプロパティが他人に知られることが不適切な場合は、プライバシー保護のために削除することができます。この記事では、Spire.Doc for Java を使用して、プログラム的に Word ドキュメントのプロパティを読んだり削除したりする方法を紹介します。 Word 文書から組み込みとユーザー定義のプロパティを読み取る Word 文書から組み込みとユーザー定義のプロパティを削除する Spire.Doc for Java をインストールします まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>11.5.5</version> </dependency> </dependencies> Word 文書から組み込みとユーザー定義のプロパティを読み取る Word ドキュメントには、組み込みのプロパティとユーザー定義のプロパティの2種類があります。組み込みプロパティとは、あらかじめ定義されたプロパティのことで、ユーザー定義プロパティとは、名前、型、値などをカスタマイズしたプロパティのことです。この2種類の文書プロパティを読み取るための詳細な手順は、以下の通りです。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getBuiltinDocumentProperties() メソッドと Document.getCustomDocumentProperties() メソッドを使用して、すべての組み込みプロパティとユーザー定義プロパティを取得します。 BuiltinDocumentProperties クラスのメソッドを使用して、各ビルトインプロパティを取得します。 CustomDocumentProperties クラスのメソッドを使って、ユーザー定義プロパティをループして、その名前と値を取得します。 Java import com.spire.doc.BuiltinDocumentProperties; import com.spire.doc.CustomDocumentProperties; import com.spire.doc.Document; public class GetDocumentProperties { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/サンプル.docx"); //StringBuilderのオブジェクトを作成する StringBuilder properties = new StringBuilder(); //すべての組み込みプロパティとユーザー定義プロパティを取得する BuiltinDocumentProperties builtinDocumentProperties = document.getBuiltinDocumentProperties(); CustomDocumentProperties customDocumentProperties = document.getCustomDocumentProperties(); //各組み込みプロパティを取得する String title = builtinDocumentProperties.getTitle(); String subject = builtinDocumentProperties.getSubject(); String author = builtinDocumentProperties.getAuthor(); String manager = builtinDocumentProperties.getManager(); String category = builtinDocumentProperties.getCategory(); String company = builtinDocumentProperties.getCompany(); String keywords = builtinDocumentProperties.getKeywords(); String comments = builtinDocumentProperties.getComments(); //表示する文字列の形式を設定する String builtinProperties = String.format("組み込みのプロパティ:rnタイトル:" + title + "rnサブタイトル:" + subject + "rn作成者:" + author + "rn管理者:" + manager + "rn分類:" + category + "rn会社名:" + company + "rnキーワード:"+ keywords + "rnコメント:" + comments ); //StringBuilderのオブジェクトに組み込みのプロパティを追加する properties.append(builtinProperties); //各ユーザー定義プロパティを取得する properties.append("rnrnユーザー定義のプロパティ:"); for (int i = 0; i < customDocumentProperties.getCount(); i++) { String customProperties = String.format("rn" + customDocumentProperties.get(i).getName() + ":" + document.getCustomDocumentProperties().get(i).getValue()); //StringBuilderのオブジェクトにユーザー定義のプロパティを追加する properties.append(customProperties); } //ドキュメントのプロパティを出力する System.out.println(properties); } } Word 文書から組み込みとユーザー定義のプロパティを削除する 組み込みのドキュメントのプロパティは、その値を空に設定することで削除することができます。ユーザー定義のドキュメントのプロパティについては、CustomDocumentProperties.get().getName() メソッドで名前を取得し、CustomDocumentProperties.remove() メソッドを使用して削除することができます。詳しい手順は以下の通りです。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getBuiltinDocumentProperties() メソッドと Document.getCustomDocumentProperties() メソッドを使用して、すべての組み込みプロパティとユーザー定義プロパティを取得します。 BuiltinDocumentProperties クラスのメソッドを使用して、値を空にして組み込みプロパティを削除します。 CustomDocumentProperties.getCount() メソッドを使用して、ユーザー定義プロパティの件数を取得します。 CustomDocumentProperties.get().getName() メソッドを使用してユーザー定義プロパティをループして名前を取得し、CustomDocumentProperties.remove() メソッドを使用して名前によって各ユーザー定義プロパティを削除します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.BuiltinDocumentProperties; import com.spire.doc.CustomDocumentProperties; import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RemoveDocumentProperties { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/サンプル.docx"); //すべての組み込みプロパティとユーザー定義プロパティを取得する BuiltinDocumentProperties builtinDocumentProperties = document.getBuiltinDocumentProperties(); CustomDocumentProperties customDocumentProperties = document.getCustomDocumentProperties(); //組み込みプロパティの値を空に設定することで削除する builtinDocumentProperties.setTitle(""); builtinDocumentProperties.setSubject(""); builtinDocumentProperties.setAuthor(""); builtinDocumentProperties.setManager(""); builtinDocumentProperties.setCompany(""); builtinDocumentProperties.setCategory(""); builtinDocumentProperties.setKeywords(""); builtinDocumentProperties.setComments(""); //ユーザー定義プロパティの件数を取得する int count = customDocumentProperties.getCount(); //ユーザー定義プロパティをループして削除する for (int i = count; i > 0; i-- ){ //ユーザー定義プロパティの名前を取得する String name = customDocumentProperties.get(i-1).getName(); //ユーザー定義プロパティをその名前で削除する customDocumentProperties.remove(name); } //ドキュメントを保存する document.saveToFile("ドキュメントのプロパティの削除.docx", FileFormat.Auto); document.dispose(); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。