Java で Word ドキュメントのメタデータを編集する方法

この短いガイドでは、Java で Word ドキュメントのメタデータを編集する方法 について簡単に説明します。 IDE で必要な構成、詳細な手順によるプログラミング ロジック、Java で DOCX メタデータ エディターを開発するための実行可能なサンプル コードについて説明します。この機能を明確に理解するために、さまざまなタイプのカスタム プロパティと組み込みプロパティを示します。

Java で Word ドキュメントのプロパティを変更する手順

  1. Aspose.Words for Java を使用してドキュメントのプロパティを編集するように環境を設定します
  2. プロパティを変更する対象の Word ファイルをロードします
  3. custom properties のコレクションにアクセスし、目的のプロパティが利用可能かどうかを確認します
  4. カスタム プロパティの新しい値を設定する
  5. built-in properties にアクセスして更新します
  6. 更新されたプロパティを含む結果の Word ファイルを保存します。

これらの手順に従うことで、Java で Word メタデータを編集できます。このプロセスは、ソース Word ファイルをロードし、カスタム プロパティにアクセスし、要件に従ってプロパティを変更することで開始されます。次の手順では、結果の Word ファイルを保存する前に、組み込みプロパティにアクセスして適宜更新します。

Java の Word でドキュメントのプロパティを編集するコード

import com.aspose.words.*;
public class Main {
public static void main(String[] args) throws Exception // Update Word Metadata in Java
{
// Set the licenses
new com.aspose.words.License().setLicense("Aspose.Total.lic");
// Load the document
Document doc = new Document("SampleProps.docx");
// Access the properties
CustomDocumentProperties custProps = doc.getCustomDocumentProperties();
// Check the desired property
if (custProps.get("Reviewed") != null)
{
// Set new properties
custProps.get("Reviewed By").setValue("Mart");
custProps.get("Reviewed Date").setValue(new java.util.Date());
}
// Access the properties
BuiltInDocumentProperties documentProperties = doc.getBuiltInDocumentProperties();
// Update the properties
documentProperties.get("Pages").setValue(doc.getPageCount());
documentProperties.get("Comments").setValue("Document Comments");
documentProperties.get("Title").setValue("Document Title");
// Save the output file
doc.save("Output.docx");
System.out.println("Done");
}
}

このコード スニペットを通じて、Java で Word ドキュメントのメタデータを編集するプロセスを学習しました。 Document クラスの getCustomDocumentProperties() メソッドは、編集用のカスタム プロパティのコレクションを提供し、get() メソッドを使用して個々のプロパティにアクセスできます。同様に、 getBuiltInDocumentProperties() メソッドは、変更のための組み込みプロパティへのアクセスを提供します。

この記事では、Word メタデータにアクセスして変更する方法を説明しました。 Word ファイルをマークダウン ファイルに変換する場合は、Javaを使用してWordをマークダウンに変換する方法 の記事を参照してください。

 日本語