Set Word Metadata Example
This program demonstrates setting metadata in a Word file.
Problem Statement
Write a Java program that sets author and title metadata in a Word .docx file.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.poi.xwpf.usermodel.*; |
| 3 | public class WordMetadataExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq Word Metadata Example"); |
| 6 | try(XWPFDocument doc=new XWPFDocument()) { |
| 7 | doc.getProperties().getCoreProperties().setCreator("Eduinq Author"); |
| 8 | doc.getProperties().getCoreProperties().setTitle("Eduinq Word Metadata Example"); |
| 9 | try(FileOutputStream fos=new FileOutputStream("metadata.docx")) { |
| 10 | doc.write(fos); |
| 11 | System.out.println("Eduinq Metadata Set Successfully"); |
| 12 | } |
| 13 | } catch(IOException e) { |
| 14 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 15 | } |
| 16 | } |
| 17 | } |
Program Output
Eduinq Word Metadata Example Eduinq Metadata Set Successfully
Explanation
Metadata set using getCoreProperties(). - Author and title fields updated. - Exception handling ensures safe operations. - In enterprise systems, metadata is used for indexing and compliance. Conclusion: Shows setting metadata in Word files.