Hi,
I need to update or insert new properties to pptx documents.
I did it with docx and it worked but it doesn't work for pptx.
Here the function that I use for docx and pptx:
public String updateMSProperties(NodeRef noderefToUpdate, String[] properties, String[] values, String typeFile) throws Exception
{
log.fatal("noderefToUpdate "+ noderefToUpdate);
log.fatal("typeFile "+ typeFile);
log.fatal("updateMSProperties JAVA");
log.fatal("typeFile: "+typeFile);
String docx = new String("docx");
String pptx = new String("pptx");
WordprocessingMLPackage wordMLPackage = null;
PresentationMLPackage pptMLPackage = null;
org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = null;
if (properties.length != values.length) {
return "Error: Amount of properties and values not matching props:"+properties.length+" values: "+values.length;
}
log.fatal("Reading node...");
// Get Reader
ContentReader reader = services.getContentService().getReader(noderefToUpdate, ContentModel.PROP_CONTENT);
InputStream is;
OutputStream os;
//The reader may be null, e.g. for folders and the like
if (reader == null || !reader.exists()) {
throw new ContentIOException("Could not get ContentReader from node " + noderefToUpdate);
}
try
{
is = reader.getContentInputStream();
}
catch (Exception e){return "Error: Could not get document " + e;}
log.fatal("Creating package...");
try
{
if(typeFile.equals(docx)){
log.fatal(" in1 typeFile == docx ");
wordMLPackage = WordprocessingMLPackage.load(is);
docPropsCustomPart = wordMLPackage.getDocPropsCustomPart();
}
if(typeFile.equals(pptx)){
log.fatal(" in1 typeFile == pptx ");
pptMLPackage = (PresentationMLPackage)OpcPackage.load(is);
docPropsCustomPart = pptMLPackage.getDocPropsCustomPart();
}
}
catch (Exception e){
is.close();
return "Error: Docx seems to be corrupt: " + e;
}
is.close();
log.fatal("Package created...");
// Custom properties
log.fatal("Trying to get custom property part...");
//org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = wordMLPackage.getDocPropsCustomPart();
if(docPropsCustomPart==null){
log.fatal("docPropsCustomPart Not existing... so creating...");
docPropsCustomPart = new org.docx4j.openpackaging.parts.DocPropsCustomPart();
docPropsCustomPart.setJaxbElement(new Properties());
if(docPropsCustomPart==null){
return "Error: Cannot get Custom Properties and cannot create one";
}
log.fatal("Adding newly created docPropsCustomPart to doc...");
//wordMLPackage.addTargetPart(docPropsCustomPart);
if(typeFile.equals(docx)){
log.fatal(" in2 typeFile == docx ");
wordMLPackage.addTargetPart(docPropsCustomPart);
}
if(typeFile.equals(pptx)){
log.fatal(" in2 typeFile == pptx ");
pptMLPackage.addTargetPart(docPropsCustomPart);
}
}
log.fatal("Start updating docPropsCustomPart...");
// Update the props....
for(int i=0;i<properties.length;i++) {
String thisProp = properties[i];
String thisValue = values[i];
log.fatal("Updating "+thisProp+"=>"+thisValue);
docPropsCustomPart.setProperty(thisProp,thisValue);
}
log.fatal("Running field updater...");
try
{
log.fatal(" before FieldUpdater");
if(typeFile.equals(docx)){
log.fatal("init FieldUpdater... ");
org.docx4j.model.fields.FieldUpdater fu = new org.docx4j.model.fields.FieldUpdater(wordMLPackage);
fu.update(true);
}
//org.docx4j.model.fields.FieldUpdater fu = new org.docx4j.model.fields.FieldUpdater(wordMLPackage);
//fu.update(true);
}
catch (Exception e){
return "Error: Updating fields failed: " + e;
}
log.fatal("Writing back...");
// Get writer
ContentWriter writer = services.getContentService().getWriter(noderefToUpdate, ContentModel.PROP_CONTENT, true);
if(writer == null) {
throw new ContentIOException("Could not get ContentWriter from node " + noderefToUpdate);
}
try
{
os = writer.getContentOutputStream();;
}
catch (Exception e){return "Error: Could not save document " + e;}
log.fatal("SaveToZipFile");
SaveToZipFile saver = null;
if(typeFile.equals(docx)){
log.fatal(" in3 typeFile == docx ");
saver = new SaveToZipFile(wordMLPackage);
}
if(typeFile.equals(pptx)){
log.fatal(" in3 typeFile == pptx ");
saver = new SaveToZipFile(pptMLPackage);
}
log.fatal("SaveToZipFile done");
saver.save(os);
os.close();
log.fatal("Finished...");
return "Success";
}
What am I doing wrong?
Many thanks