I am trying to modify header footer sample to have different images in both header and footer. In result, there are two different images in header and footer separately, but the picture of both images are the same! The image from footer is showing inside the image of header.
My code is based on Docx4j samples:
http://dev.plutext.org/trac/docx4j/brow ... ooter.java
I added a line for creating Footer relationship in main method:
- Code: Select all
Relationship relationship2 = createFooterPart(wordMLPackage);
Also added this:
- Code: Select all
public static Relationship createFooterPart(
WordprocessingMLPackage wordprocessingMLPackage)
throws Exception {
FooterPart footerPart = new FooterPart();
// Have to do this so that the next line can
// add an image
footerPart.setPackage(wordprocessingMLPackage);
footerPart.setJaxbElement(getFtr(wordprocessingMLPackage, footerPart));
return wordprocessingMLPackage.getMainDocumentPart()
.addTargetPart(footerPart);
}
And:
- Code: Select all
public static Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage,
Part sourcePart) throws Exception {
Ftr ftr = objectFactory.createFtr();
ftr.getEGBlockLevelElts().add(
newImage(wordprocessingMLPackage,
sourcePart, getBytes(true), "filename", "alttext", 1, 2
)
);
return ftr;
}
And changed getBytes method to provide different image files:
- Code: Select all
public static byte[] getBytes(boolean isFooter) throws Exception {
File file = null;
if (isFooter)
file = new File(System.getProperty("user.dir"), "imagesrepository/logo.png");
else
file = new File(System.getProperty("user.dir"), "imagesrepository/logo2.png");
....
I also changed createHeaderReference to createHeaderAndFooterReference:
- Code: Select all
public static void createHeaderAndFooterReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship headerRelationship, Relationship footerRelationship)
throws InvalidFormatException {
SectPr sectPr = objectFactory.createSectPr();
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(headerRelationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);// add header or
// footer references
FooterReference footerReference = objectFactory.createFooterReference();
footerReference.setId(footerRelationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);// add header or
// footer references
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
}
The whole code is here:
- Code: Select all
package test.word;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import javax.xml.bind.JAXBException;
import org.docx4j.XmlUtils;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.relationships.Relationships;
import org.docx4j.wml.Body;
import org.docx4j.wml.FooterReference;
import org.docx4j.wml.Ftr;
import org.docx4j.wml.Hdr;
import org.docx4j.wml.HdrFtrRef;
import org.docx4j.wml.HeaderReference;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.Text;
public class HeaderFooter {
private static ObjectFactory objectFactory = new ObjectFactory();
public static void main(String[] args) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.createPackage();
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
Relationship relationship = createHeaderPart(wordMLPackage);
Relationship relationship2 = createFooterPart(wordMLPackage);
createHeaderAndFooterReference(wordMLPackage, relationship,
relationship2);
wordMLPackage.save(new File(System.getProperty("user.dir"),
"headerFooter.docx"));
// mainDocumentPart.marshal(new FileOutputStream(new File(System
// .getProperty("user.dir"), "headerfooter.xml")));
}
public static Relationship createHeaderPart(
WordprocessingMLPackage wordprocessingMLPackage) throws Exception {
HeaderPart headerPart = new HeaderPart();
// Have to do this so that the next line can
// add an image
headerPart.setPackage(wordprocessingMLPackage);
headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart));
return wordprocessingMLPackage.getMainDocumentPart().addTargetPart(
headerPart);
}
public static Relationship createFooterPart(
WordprocessingMLPackage wordprocessingMLPackage) throws Exception {
FooterPart footerPart = new FooterPart();
// Have to do this so that the next line can
// add an image
footerPart.setPackage(wordprocessingMLPackage);
footerPart.setJaxbElement(getFtr(wordprocessingMLPackage, footerPart));
return wordprocessingMLPackage.getMainDocumentPart().addTargetPart(
footerPart);
}
public static Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
Part sourcePart) throws Exception {
Hdr hdr = objectFactory.createHdr();
hdr.getEGBlockLevelElts().add(
newImage(wordprocessingMLPackage, sourcePart, getBytes(false),
"filename", "alttext", 1, 2));
return hdr;
}
public static Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage,
Part sourcePart) throws Exception {
Ftr ftr = objectFactory.createFtr();
ftr.getEGBlockLevelElts().add(
newImage(wordprocessingMLPackage, sourcePart, getBytes(true),
"filename", "alttext", 1, 2));
return ftr;
}
public static byte[] getBytes(boolean isFooter) throws Exception {
File file = null;
if (isFooter)
file = new File(System.getProperty("user.dir"),
"imagesrepository/logo.png");
else
file = new File(System.getProperty("user.dir"),
"imagesrepository/logo2.png");
java.io.InputStream is = new java.io.FileInputStream(file);
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
System.out.println("Could not completely read file "
+ file.getName());
}
is.close();
return bytes;
}
// public static P getP() {
// P headerP = objectFactory.createP();
// R run1 = objectFactory.createR();
// Text text = objectFactory.createText();
// text.setValue("123head123");
// run1.getRunContent().add(text);
// headerP.getParagraphContent().add(run1);
// return headerP;
// }
public static org.docx4j.wml.P newImage(
WordprocessingMLPackage wordMLPackage, Part sourcePart,
byte[] bytes, String filenameHint, String altText, int id1, int id2)
throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
.createImagePart(wordMLPackage, sourcePart, bytes);
Inline inline = imagePart.createImageInline(filenameHint, altText, id1,
id2);
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.R run = factory.createR();
p.getParagraphContent().add(run);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
run.getRunContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return p;
}
public static void createHeaderAndFooterReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship headerRelationship, Relationship footerRelationship)
throws InvalidFormatException {
SectPr sectPr = objectFactory.createSectPr();
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(headerRelationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);// add header or
// footer references
FooterReference footerReference = objectFactory.createFooterReference();
footerReference.setId(footerRelationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);// add header or
// footer references
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
}
}
As you can see the images are conflicting with each other. I thought it might be because of ids, but it is not.
Any idea what am I missing?
I am using docx4j-nightly-20100907.jar under windows seven.
Thanks in advance for your time and consideration.