2009年2月5日星期四

在JAVA里使用POI为word文档添加元数据

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import java.util.Iterator;
import java.util.Map;

import org.apache.poi.hpsf.CustomProperties;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class WordMetadataSetter {
public static boolean writeDocMetadata(String path,String docName, Map Properties) throws UnexpectedPropertySetTypeException, NoPropertySetStreamException, MarkUnsupportedException, WritingNotSupportedException {
boolean isSuccess = false;
File dataDir = new File(path);
File doc = new File(dataDir, docName);

/* Read a test document doc into a POI filesystem. */
POIFSFileSystem poifs;
try {
poifs = new POIFSFileSystem(new FileInputStream(doc));
DirectoryEntry dir = poifs.getRoot();
DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
DocumentEntry dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

/*
* Read the summary information stream and the document summary
* information stream from the POI filesystem.
*
* Please note that the result consists of SummaryInformation and
* DocumentSummaryInformation instances which are in memory only. To
* make them permanent they have to be written to a POI filesystem
* explicitly (overwriting the former contents). Then the POI filesystem
* should be saved to a file.
*/
DocumentInputStream dis = new DocumentInputStream(siEntry);
PropertySet ps = new PropertySet(dis);
SummaryInformation si = new SummaryInformation(ps);
dis = new DocumentInputStream(dsiEntry);
ps = new PropertySet(dis);
DocumentSummaryInformation dsi = new DocumentSummaryInformation(ps);

/*
* Write all properties supported by HPSF to the summary information
* (e.g. author, edit date, application name) and to the document
* summary information (e.g. company, manager).
*/
CustomProperties customProperties = dsi.getCustomProperties();
if (customProperties == null)
customProperties = new CustomProperties();
setProperties(customProperties,Properties);

dsi.setCustomProperties(customProperties);
si.write(dir, siEntry.getName());
dsi.write(dir, dsiEntry.getName());

OutputStream out = new FileOutputStream(doc);
poifs.writeFilesystem(out);
out.close();
isSuccess=true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return isSuccess;
}

private static void setProperties(CustomProperties customProperties,Map Properties){
if(customProperties==null||Properties==null)
throw new NullPointerException();
Iterator it=Properties.entrySet().iterator() ;
while(it.hasNext()){
Map.Entry entry=(Map.Entry) it.next() ;
String key=entry.getKey().toString();
String value=entry.getValue().toString();
customProperties.put(key,value);
}
}
}



import java.util.Date;
import java.util.Hashtable;
import java.util.Map;

import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
import org.apache.poi.hpsf.WritingNotSupportedException;

import junit.framework.TestCase;

public class TestWordMetadataSetter extends TestCase {

private static final String POI_FS = "TestWriteWellKnown.doc";
private static final String POI_PATH = "C:\\temp";
private static final Map PROPERTIES = new Hashtable();

public TestWordMetadataSetter(){
PROPERTIES.put("author", "fangyuan");
PROPERTIES.put("create date", new Date());
PROPERTIES.put("num", "1");
}

public void testWriteDocMetadata() throws WritingNotSupportedException, UnexpectedPropertySetTypeException, NoPropertySetStreamException, MarkUnsupportedException{
boolean result=WordMetadataSetter.writeDocMetadata(POI_PATH, POI_FS, PROPERTIES);
assertEquals(result,true);
}

}

没有评论: