2

I am not getting how to update meta data (title,subject,author etc..) for docx file using apache poi. I have tried it for a doc file using apache poi:


     File poiFilesystem = new File(file_path1);

     /* Open the POI filesystem. */
     InputStream is = new FileInputStream(poiFilesystem);
    POIFSFileSystem poifs = new POIFSFileSystem(is);
     is.close();

     /* Read the summary information. */
     DirectoryEntry dir = poifs.getRoot();
     SummaryInformation si;
     try
     {
        DocumentEntry siEntry = (DocumentEntry)
             dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
         DocumentInputStream dis = new DocumentInputStream(siEntry);
         PropertySet ps = new PropertySet(dis);
         dis.close();
         si = new SummaryInformation(ps);
     }
     catch (FileNotFoundException ex)
     {
         /* There is no summary information yet. We have to create a new
         * one. */
         si = PropertySetFactory.newSummaryInformation();
     }


     si.setAuthor("xzy");
     System.out.println("Author changed to " + si.getAuthor() + ".");
    si.setSubject("mysubject");
    si.setTitle("mytitle");
4

1 回答 1

4

下面与POI-3.10. 您可以设置一些元数据PackageProperties

import java.util.Date;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.openxml4j.util.Nullable;

class SetDOCXMetadata{
  public static void main(String[] args){
      try{
          OPCPackage opc = OPCPackage.open("metadata.docx");
          PackageProperties pp = opc.getPackageProperties();

          Nullable<String> foo = pp.getLastModifiedByProperty();
          System.out.println(foo.hasValue()?foo.getValue():"empty");
          //Set some properties
          pp.setCreatorProperty("M Kazarian");
          pp.setLastModifiedByProperty("M Kazarian " + System.currentTimeMillis());
          pp.setModifiedProperty(new Nullable<Date>(new Date()));
          pp.setTitleProperty("M Kazarian document");

          opc.close();
      } catch (Exception e) {}
  }
}
于 2013-11-07T09:54:28.527 回答