<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/>
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Target="header1.xml"/>
</Relationships>
<w:document>
<w:body>
:
<w:sectPr>
<w:headerReference w:type="default" r:id="rId6"/>
<w:footerReference w:type="default" r:id="rId7"/>
</w:sectPr>
</w:body>
</w:document>
<w:hdr>
<w:p>
<w:pPr>
<w:pStyle w:val="Header"/>
</w:pPr>
<w:r>
<w:t>My header</w:t>
</w:r>
</w:p>
</w:hdr>
public class HeaderFooter {
private static ObjectFactory objectFactory = new ObjectFactory();
public static void main(String[] args) throws Docx4JException,
FileNotFoundException, JAXBException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.createPackage();
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
createHeaderPart(wordMLPackage);
wordMLPackage.save(new File(System.getProperty("user.dir"),
"headerFooter.docx"));
// mainDocumentPart.marshal(new FileOutputStream(new File(System
// .getProperty("user.dir"), "headerfooter.xml")));
}
public static void createHeaderPart(
WordprocessingMLPackage wordprocessingMLPackage)
throws InvalidFormatException {
HeaderPart headerPart = new HeaderPart();
// headerPart.setContentType(new ContentType(
// "application/vnd.openxmlformats-package.relationships+xml"));
//
// headerPart
// .setRelationshipType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/header");
headerPart.setJaxbElement(getHdr());
Relationship relationship = wordprocessingMLPackage.getMainDocumentPart()
.addTargetPart(headerPart);
SectPr sectPr = objectFactory.createSectPr();
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);// add header or
// footer references
// RelationshipsPart relationshipsPart = wordprocessingMLPackage
// .getMainDocumentPart().getRelationshipsPart();
// relationshipsPart.getRelationships().getRelationship()
// .add(relationship);
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
// wordprocessingMLPackage.getMainDocumentPart().addObject(getP());
}
public static Hdr getHdr() {
Hdr hdr = objectFactory.createHdr();
hdr.getEGBlockLevelElts().add(getP());
return hdr;
}
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;
}
}
Can you please explain how to add binary image part to headerpart?.
How to add logo image to the headerpart?
headerPart.addTargetPart(imagePart)
/**
* Create an image part from the provided byte array, attach it to the source part
* (eg the main document part, a header part etc), and return it. */
public static BinaryPartAbstractImage createImagePart(WordprocessingMLPackage wordMLPackage,
Part sourcePart, byte[] bytes)
package org.docx4j.samples;
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.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.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.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);
createHeaderReference(wordMLPackage, relationship);
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 Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
Part sourcePart) throws Exception {
Hdr hdr = objectFactory.createHdr();
hdr.getEGBlockLevelElts().add(
newImage(wordprocessingMLPackage,
sourcePart, getBytes(), "filename", "alttext", 1, 2
)
);
return hdr;
}
public static byte[] getBytes() throws Exception {
File file = new File("/home/dev/test.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 createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship relationship )
throws InvalidFormatException {
SectPr sectPr = objectFactory.createSectPr();
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);// add header or
// footer references
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
}
}
Users browsing this forum: No registered users and 23 guests