デジタル技術の現代では、ドキュメント内の隠された情報を明らかにすることが、効果的なドキュメント管理と分析に不可欠です。作成者、作成日、改訂履歴などの詳細を含むドキュメントのメタデータは、ドキュメントの起源と進化に関する貴重な洞察を提供します。この記事では、メタデータ ライブラリを活用して、Java を使用して DOCX からメタデータを読み取る 方法について詳しく説明します。これにより、開発者はドキュメント リポジトリをより深く調査し、ドキュメント処理ワークフローを改善できます。以下は、Java を使用して DOCX のメタデータを読み取る 方法を示す重要な手順とコード例です。
Javaを使用してDOCXからメタデータを読み取る手順
- DOCX メタデータの抽出に GroupDocs.Metadata for Java を使用するように IDE を設定します。
- Metadata クラスをインスタンス化し、DOCX ファイル パスをコンストラクタの引数として渡します。
- すべてのメタデータプロパティを検査するための条件またはルールを作成する
- Metadata.findProperties メソッドの述語を指定します
- 取得したプロパティを反復処理する
DOCX ファイルからメタデータを抽出すると、ドキュメントの分類を自動化したり、ドキュメント リポジトリに検索機能を実装したり、組織のコンプライアンスを確保したりするなど、開発者にとってさまざまな機会が生まれます。提供されている手順に従うと、Java がインストールされていることを前提として、Windows、macOS、Linux などの一般的なオペレーティング システムと互換性があります。このプロセスでは、Java で DOCX のメタデータを抽出するために追加のソフトウェアをインストールする必要はありません。推奨ライブラリを設定し、ファイル パスを調整したら、次のコードをプロジェクトに統合するのは簡単で問題なく実行できます。
Java を使用して DOCX からメタデータを読み取るコード
import com.groupdocs.metadata.Metadata; | |
import com.groupdocs.metadata.core.FileFormat; | |
import com.groupdocs.metadata.core.IReadOnlyList; | |
import com.groupdocs.metadata.core.MetadataProperty; | |
import com.groupdocs.metadata.core.MetadataPropertyType; | |
import com.groupdocs.metadata.licensing.License; | |
import com.groupdocs.metadata.search.FallsIntoCategorySpecification; | |
import com.groupdocs.metadata.search.OfTypeSpecification; | |
import com.groupdocs.metadata.search.Specification; | |
import com.groupdocs.metadata.tagging.Tags; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class ReadMetadataFromDOCXUsingJava { | |
public static void main(String[] args) { | |
// Set License to avoid the limitations of Metadata library | |
License license = new License(); | |
license.setLicense("GroupDocs.Metadata.lic"); | |
Metadata metadata = new Metadata("input.docx"); | |
if (metadata.getFileFormat() != FileFormat.Unknown && !metadata.getDocumentInfo().isEncrypted()) { | |
System.out.println(); | |
// Fetch all metadata properties that fall into a particular category | |
IReadOnlyList<MetadataProperty> properties = metadata.findProperties(new FallsIntoCategorySpecification(Tags.getContent())); | |
System.out.println("The metadata properties describing some characteristics of the file content: title, keywords, language, etc."); | |
for (MetadataProperty property : properties) { | |
System.out.println(String.format("Property name: %s, Property value: %s", property.getName(), property.getValue())); | |
} | |
// Fetch all properties having a specific type and value | |
int year = Calendar.getInstance().get(Calendar.YEAR); | |
properties = metadata.findProperties(new OfTypeSpecification(MetadataPropertyType.DateTime).and(new ReadMetadataFromDOCXUsingJava().new YearMatchSpecification(year))); | |
System.out.println("All datetime properties with the year value equal to the current year"); | |
for (MetadataProperty property : properties) { | |
System.out.println(String.format("Property name: %s, Property value: %s", property.getName(), property.getValue())); | |
} | |
// Fetch all properties whose names match the specified regex | |
Pattern pattern = Pattern.compile("^author|company|(.+date.*)$", Pattern.CASE_INSENSITIVE); | |
properties = metadata.findProperties(new ReadMetadataFromDOCXUsingJava().new RegexSpecification(pattern)); | |
System.out.println(String.format("All properties whose names match the following regex: %s", pattern.pattern())); | |
for (MetadataProperty property : properties) { | |
System.out.println(String.format("Property name: %s, Property value: %s", property.getName(), property.getValue())); | |
} | |
} | |
} | |
// Define your own specifications to filter metadata properties | |
public class YearMatchSpecification extends Specification { | |
public YearMatchSpecification(int year) { | |
setValue(year); | |
} | |
public final int getValue() { | |
return auto_Value; | |
} | |
private void setValue(int value) { | |
auto_Value = value; | |
} | |
private int auto_Value; | |
public boolean isSatisfiedBy(MetadataProperty candidate) { | |
Date date = candidate.getValue().toClass(Date.class); | |
if (date != null) { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(date); | |
return getValue() == calendar.get(Calendar.YEAR); | |
} | |
return false; | |
} | |
} | |
public class RegexSpecification extends Specification { | |
private Pattern pattern; | |
public RegexSpecification(Pattern pattern) { | |
this.pattern = pattern; | |
} | |
@Override | |
public boolean isSatisfiedBy(MetadataProperty metadataProperty) { | |
Matcher matcher = pattern.matcher(metadataProperty.getName()); | |
return matcher.find(); | |
} | |
} | |
} |
まとめると、DOCX ファイルからメタデータを読み取る機能により、開発者はドキュメント ライブラリに関する貴重な情報を得ることができます。この記事で詳しく説明されている手順に従い、提供されているコード例を使用すると、Java で DOCX のメタデータを簡単に取得 でき、ドキュメントの整理、分析、管理を改善できます。さまざまなメタデータ プロパティを調べ、ライブラリを詳しく調べて、Java でのドキュメント処理能力を強化してください。おめでとうございます。これで、Java を使用して DOCX メタデータを抽出するスキルを習得できました。
前回のディスカッションでは、Java を使用してドキュメントの詳細にアクセスする方法についての包括的なチュートリアルを提供しました。このトピックについてさらに深く理解するには、Javaを使用してドキュメント情報を取得する の方法に関する詳細なガイドを参照することをお勧めします。