Page 1 of 1

issue adding rels to header created from xml file

PostPosted: Sat Feb 18, 2012 10:13 am
by nhanus09
Hi Jason...

I have used the createheaderfooter and another post to create the header from xml file....

- the template.docx is blank except for page orientation and magin settings.

- i have sucessfully added the header1.xml part to the document... I think the issue is in creating the rel to the new header.

- i have tried doing so using:

Relationship relationship = mainDocumentPart.getRelationshipsPart().getRelationshipByID("rId6");
createHeaderReference(wordMLPackage,relationship);
wordMLPackage.save(new File(baseDir + "/header-added.docx"));

but no ref is being made..???

I have attatched the full src..

thanks :)

Re: issue adding rels to header created from xml file

PostPosted: Sat Feb 18, 2012 10:54 am
by jason
There are several problems with your code (just looking at it; I haven't tried to run it):

Code: Select all
Relationship relationship = mainDocumentPart.getRelationshipsPart().getRelationshipByID("rId6");


I think you are assuming the header will be allocated rId6 when it is added. You should get the Relationship which is returned by addTargetPart.

Your createHeaderReference method doesn't do anything with the relationship arg.

Re: issue adding rels to header created from xml file

PostPosted: Sat Feb 18, 2012 11:51 am
by nhanus09
Hi Jason, thanks for the reply..

I am unsure on how to get the relationship from thefollowing...
Im attempting to add the header part from an external xml file..
the rId6 is the id that gets assigned.. this is the only way i cld find to get the
relationship to pass to createHeaderReference() -- wich i have not even gotten to...

wordMLPackage = WordprocessingMLPackage.load(new File(baseDir + file));
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();

FileInputStream 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);

Relationship relationship = mainDocumentPart.getRelationshipsPart().getRelationshipByID("rId6");

Re: issue adding rels to header created from xml file

PostPosted: Sat Feb 18, 2012 12:03 pm
by nhanus09
I added:

wordprocessingMLPackage.getMainDocumentPart().relationships.addRelationship(relationship); to createHeaderReference()
and i got....

yes so that ID was already in there, so i just need to figure out how to get the rel... :)

Exception in thread "main" org.docx4j.openpackaging.exceptions.InvalidOperationException: Refusing to add another rel with id rId6. Target is header1.xml
at org.docx4j.openpackaging.parts.relationships.RelationshipsPart.addRelationship(RelationshipsPart.java:621)
at com.dms.docx4j.CreateHeaderFromXML.createHeaderReference(CreateHeaderFromXML.java:104)
at com.dms.docx4j.CreateHeaderFromXML.replaceHeader(CreateHeaderFromXML.java:73)
at com.dms.docx4j.CreateHeaderFromXML.main(CreateHeaderFromXML.java:39)
9574 [main] ERROR org.docx4j.openpackaging.parts.relationships.RelationshipsPart - Refusing to add another rel with id rId6. Target is header1.xml

Re: issue adding rels to header created from xml file

PostPosted: Sat Feb 18, 2012 12:52 pm
by nhanus09
ok so i fixed the attachForeignPart() to return the rel..
but i still get the error:

Exception in thread "main" org.docx4j.openpackaging.exceptions.InvalidOperationException: Refusing to add another rel with id rId8. Target is header1.xml
at org.docx4j.openpackaging.parts.relationships.RelationshipsPart.addRelationship(RelationshipsPart.java:621)
at com.dms.docx4j.CreateHeaderFromXML.createHeaderReference(CreateHeaderFromXML.java:136)
at com.dms.docx4j.CreateHeaderFromXML.replaceHeader(CreateHeaderFromXML.java:83)
at com.dms.docx4j.CreateHeaderFromXML.main(CreateHeaderFromXML.java:41)

i even tried to remove all existing rels... ??

public static void replaceHeader(String file, String baseDir, String expandedDir) throws Exception {
WordprocessingMLPackage wordMLPackage = null;
if (file.endsWith(".docx")) {

wordMLPackage = WordprocessingMLPackage.load(new File(baseDir + file));
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();

try
{
removeRels(mainDocumentPart);
}
catch(Exception e)
{
;
}

FileInputStream 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");

Relationship relationship = attachForeignPart(wordMLPackage,
wordMLPackage.getMainDocumentPart(),
externalCtm,
"word/header1.xml",
in);

createHeaderReference(wordMLPackage,relationship);

wordMLPackage.save(new File(baseDir + "/header-added.docx"));
}
}

/*
* adds the target part
* return: relationship
*/
public static Relationship attachForeignPart(WordprocessingMLPackage wordMLPackage,Base attachmentPoint,ContentTypeManager foreignCtm,
String resolvedPartUri, FileInputStream is) throws Exception {
Part foreignPart = Load.getRawPart(is, foreignCtm, resolvedPartUri, null);
// the null means this won't work for an AlternativeFormatInputPart
Relationship rel = attachmentPoint.addTargetPart(foreignPart);
// Add content type
ContentTypeManager packageCtm = wordMLPackage.getContentTypeManager();
packageCtm.addOverrideContentType(foreignPart.getPartName().getURI(), foreignPart.getContentType());
return rel;
}

Re: issue adding rels to header created from xml file

PostPosted: Sat Feb 18, 2012 1:04 pm
by nhanus09
Jason...

upon looknig at the docx that was created despite it saying that it wld not create the ref..

i can see in word/_rels/document.xml.rels a ref to header1.xml with :

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Target="header1.xml" />
</Relationships>

this is the entire file....
and in word/header1.xml is the correct header?? im at a complete loss lol :)
any advise wld be great thanks...

nate.