Thanks to your help Jason I almost have adding these objects working. Right now I'm running into a couple issues and they're tied to the names of the embedded files (which I guess are determined by the PartName class).
If I'm copying parts from only one WordprocessingMLPackage to a new WordprocessingMLPackage, I can copy over the embedded files without any issue. If I rename the docx file to zip, the Excel files get added with out any problems. However, say I'm copying parts from MULTIPLE files to only one new WordprocessingMLPackage, the document blows up when I open it. I believe the issue is do to the fact that the other MULTIPLE files have embedded objects that have the same name. For example, in one package, I had a "Microsoft_Office_Excel_97-2003_Worksheet1.xls" but I had the exact same file in another package.
That being said, I figured I'd change the PartName's to point to new names that I create (i.e. 1.xls, 2.xls, etc) whenever I add that part to the new WordMLPackage. That way even if 2 packages have the same names for embedded files, they would have different names in the new package. That being said, I used the following code:
- Code: Select all
WordprocessingMLPackage newPkg = cvp.getPackage(sanitized);
MainDocumentPart newPkgMainDocPart = newPkg.getMainDocumentPart();
//get relationship id from Object o
String oldRelId = extractRelationshipId(o);
//get part based on relationship Id
Part oldPart = oldPkg.getMainDocumentPart().getRelationshipsPart().getPart(oldRelId);
//if old part is not null, create new part in cost volume pkg
if( oldPart != null ){
//System.out.println("OLD PART NAME BEFORE UPDATE: " + oldPart.getPartName());
//add part and get relationship to new part
oldPart.setPackage(newPkg);
oldPart.setOwningRelationshipPart(newPkgMainDocPart.getRelationshipsPart());
Relationship newRel = cvp.getPackage(sanitized).getMainDocumentPart().addTargetPart(oldPart);
//update part name to generated specific name
//NOTE, cvp.getNextExternalObjectPrefixId() returns an new unique ID that I use to generate a unique file name every time I add a part
Part newPart = newPkgMainDocPart.getRelationshipsPart().getPart(newRel.getId());
String oldName = newPart.partName.getName();
String fileExt = ECVTFileUtils.getFilenameExtension(oldName);
String newName = oldName.replace(fileExt, "_EXT_OBJ_" + cvp.getNextExternalObjectPrefixId() + fileExt);
newPart.partName = new PartName(newName); //not sure why PartName doesn't have a setter for partName
System.out.println("OLD PART NAME AFTER UPDATE: " + oldPart.getPartName());
System.out.println("NEW PART NAME AFTER UPDATE: " + newPart.getPartName());
//update relationship ID reference to use new ID
return updateRelationshipId(newRel.getId(), o, oldPkg, cvp, sanitized);
} else {
null
}
So now I'm running into 2 issues:
1) The file names in document.xml.rels still reflect the old part names. The files in the actual zip have the new names, but the actual file names in the Relationships tags in document.xml.rels don't change. For example, say I rename a PartName from "AAA.xls" to "BBB.xls". The actual file in the zip will be "BBB.xls", but document.xml.rels will still show "AAA.xls"
2) Is it possible to create a Part based of an existing Part (essentially a deep copy)? In the code above, the name of the old part in the old document and the new part in the new document end up having the same name and that's causing problems for me.
Thanks!
Justin