First, copying header and footer from a document with header/footer to a document with none of these
- Code: Select all
public static void copyHeaderAndFooter(String sourcefile, String targetfile) throws Exception {
WordprocessingMLPackage source;
WordprocessingMLPackage target;
if (sourcefile.endsWith(".docx")) {
try {
source = WordprocessingMLPackage.load(new File(sourcefile));
target = WordprocessingMLPackage.load(new File(targetfile));
// Find the part we want to copy
RelationshipsPart rp = source.getMainDocumentPart().getRelationshipsPart();
Relationship rel = rp.getRelationshipByType(Namespaces.HEADER);
Part p = rp.getPart(rel);
log.debug(p.getPartName().getName());
p.setPartName(new PartName("/word/header1.xml"));
// Now try adding it
target.getMainDocumentPart().addTargetPart(p);
rp = source.getMainDocumentPart().getRelationshipsPart();
rel = rp.getRelationshipByType(Namespaces.FOOTER);
p = rp.getPart(rel);
log.debug(p.getPartName().getName());
p.setPartName(new PartName("/word/footer1.xml"));
// Now try adding it
target.getMainDocumentPart().addTargetPart(p);
target.save(new File(targetfile));
} catch (Docx4JException e) {
log.error(e);
}
}
}
results in nothing at all.. no headers & footers in target document (based on the CopyPart sample)
Then, I tried getting the raw xml files, based on the ImportForeignPart sample
- Code: Select all
public static void replaceHeader(String file, String baseDir, String expandedDir) throws Exception {
WordprocessingMLPackage wordMLPackage = null;
if (file.endsWith(".docx")) {
try {
wordMLPackage = WordprocessingMLPackage.load(new File(baseDir + file));
InputStream in = new FileInputStream(baseDir + expandedDir + "/[Content_Types].xml");
ContentTypeManager externalCtm = new ContentTypeManager();
externalCtm.parseContentTypesFile(in);
// Example of a part which become a rel of the word document
in = new FileInputStream(baseDir + expandedDir + "/word/header1.xml");
attachForeignPart(wordMLPackage,
wordMLPackage.getMainDocumentPart(),
externalCtm,
"word/header1.xml",
in);
// Example of a part which become a rel of the package
in = new FileInputStream(baseDir + expandedDir + "/docProps/app.xml");
attachForeignPart(wordMLPackage, wordMLPackage,
externalCtm, "docProps/app.xml", in);
wordMLPackage.save(new File(baseDir + "header-added.docx"));
} catch (Docx4JException e) {
log.error(e);
}
}
}
public static void attachForeignPart(WordprocessingMLPackage wordMLPackage,
Base attachmentPoint,
ContentTypeManager foreignCtm,
String resolvedPartUri, InputStream is) throws Exception {
Part foreignPart = Load.getRawPart(is, foreignCtm, resolvedPartUri, null);
// the null means this won't work for an AlternativeFormatInputPart
attachmentPoint.addTargetPart(foreignPart);
// Add content type
ContentTypeManager packageCtm = wordMLPackage.getContentTypeManager();
packageCtm.addOverrideContentType(foreignPart.getPartName().getURI(), foreignPart.getContentType());
log.debug("Attached foreign part: " + resolvedPartUri);
}
this one results in a document without a header, however, if I unzip the produced document I find the correct header in /word/header1.xml...
going further... I managed to import a header and add it to my document based on the HeaderFooterCreate sample, but this only adds to the existing header in the document, so I thought I'd use code from StripParts sample to remove headers/footers first...
so I added this If statement to the sample
- Code: Select all
} else if (stripHeaderFooter &&
part instanceof org.docx4j.openpackaging.parts.WordprocessingML.FooterPart
|| part instanceof org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart) {
deletions.add(r);
sb.append(".. DELETED");
if (part.getRelationshipsPart() == null) {
sb.append(".. no rels");
} else {
traverseRelationships(wordMLPackage, part.getRelationshipsPart(), sb, indent + " ");
}
}
but this only results in a document with broken relationships/references to header and footer when opening in Word...
long post and a little sidetracked but individually I would think that all of these would work, though I'm basically looking for the right code to open a document and replace it's header and footer with my own content...
cheers,
Kjetil