docx4j 在页眉同时增加图片和文字的方法

HTML方式建上一遍文章,本文章直接用代码加入图片和文字,代码如下:

import org.docx4j.Docx4J;

import org.docx4j.dml.wordprocessingDrawing.Inline;

import org.docx4j.jaxb.Context;

import org.docx4j.model.structure.SectionWrapper;

import org.docx4j.openpackaging.exceptions.InvalidFormatException;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import org.docx4j.openpackaging.parts.Part;

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;

import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;

import org.docx4j.relationships.Relationship;

import org.docx4j.utils.BufferUtil;

import org.docx4j.wml.Hdr;

import org.docx4j.wml.HdrFtrRef;

import org.docx4j.wml.HeaderReference;

import org.docx4j.wml.Jc;

import org.docx4j.wml.JcEnumeration;

import org.docx4j.wml.ObjectFactory;

import org.docx4j.wml.P;

import org.docx4j.wml.PPr;

import org.docx4j.wml.PPrBase.PBdr;

import org.docx4j.wml.PPrBase.Spacing;

import org.docx4j.wml.R.Tab;

import org.docx4j.wml.STBorder;

import org.docx4j.wml.STLineSpacingRule;

import org.docx4j.wml.SectPr;

import org.docx4j.wml.Text;


public class HeaderFooterCreate {

private static ObjectFactory objectFactory = new ObjectFactory();


public static void main(String[] args) throws Exception {

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

ObjectFactory factory = Context.getWmlObjectFactory();

Relationship relationship = createHeaderPart(wordMLPackage);

createHeaderReference(wordMLPackage, relationship,factory);

Docx4J.save(wordMLPackage, new File("OUT_HeaderFooterCreate.docx") );

}

public static Relationship createHeaderPart(

WordprocessingMLPackage wordprocessingMLPackage)

throws Exception {

HeaderPart headerPart = new HeaderPart();

Relationship relwordprocessingMLPackage.getMainDocumentPart()

.addTargetPart(headerPart);

headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart));

return rel;

}


public static void createHeaderReference(

WordprocessingMLPackage wordprocessingMLPackage,

Relationship relationship, ObjectFactory factory)

throws InvalidFormatException {


List sections = wordprocessingMLPackage.getDocumentModel().getSections();

   

SectPr sectPr = sections.get(sections.size() - 1).getSectPr();

if (sectPr==null ) {

sectPr = objectFactory.createSectPr();

wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);

sections.get(sections.size() - 1).setSectPr(sectPr);

}


HeaderReference headerReference = objectFactory.createHeaderReference();

headerReference.setId(relationship.getId());

headerReference.setType(HdrFtrRef.DEFAULT);    

sectPr.getEGHdrFtrReferences().add(headerReference);// add header or


}

public static Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,

Part sourcePart) throws Exception {


Hdr hdr = objectFactory.createHdr();

File file = new File("./iyw.png" );

java.io.InputStream is = new java.io.FileInputStream(file );

hdr.getContent().add(

newImage(wordprocessingMLPackage,

sourcePart

BufferUtil.getBytesFromInputStream(is), 

"filename", "alttext", 1, 2

)

);


return hdr;


}

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, false);

org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();

org.docx4j.wml.P  p = factory.createP();

org.docx4j.wml.R  run = factory.createR();

p.getContent().add(run);        

org.docx4j.wml.Drawing drawing = factory.createDrawing();

run.getContent().add(drawing);

drawing.getAnchorOrInline().add(inline);

org.docx4j.wml.R  runText = factory.createR();

p.getContent().add(runText);

Tab rTab = new Tab();

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

runText.getContent().add(rTab);

org.docx4j.wml.R  runText1 = factory.createR();

p.getContent().add(runText1);

Text text1 = factory.createText();

runText1.getContent().add(text1);    

text1.setValue("页眉文字");

PPr pPr = p.getPPr();  

        if (pPr == null) {  

            pPr = factory.createPPr();  

        }  

        Jc jc = pPr.getJc();  

        if (jc == null) {  

            jc = new Jc();  

        }  

        jc.setVal(JcEnumeration.LEFT);  

        pPr.setJc(jc);  

          

        PBdr pBdr=pPr.getPBdr();  

        if(pBdr==null){  

            pBdr=factory.createPPrBasePBdr();  

        }  

          

        CTBorder value=new CTBorder();  

        value.setVal(STBorder.SINGLE);  

        value.setColor("000000");  

        value.setSpace(new BigInteger("0"));  

        value.setSz(new BigInteger("0"));  

        

        pBdr.setBetween(value);  

        pPr.setPBdr(pBdr);

        

        p.setPPr(pPr);  

        setParagraphSpacing(factory, p, JcEnumeration.BOTH, "0", "0");


return p;

}

//设置段落间距  

    public static void setParagraphSpacing(ObjectFactory factory, P p,  

            JcEnumeration jcEnumeration,String before,String after) {  

        PPr pPr = p.getPPr();  

        if (pPr == null) {  

            pPr = factory.createPPr();  

        }  

        Jc jc = pPr.getJc();  

        if (jc == null) {  

            jc = new Jc();  

        }  

        jc.setVal(jcEnumeration);  

        pPr.setJc(jc);  

          

        Spacing spacing=new Spacing();  

        spacing.setBefore(new BigInteger(before));  

        spacing.setAfter(new BigInteger(after));  

        spacing.setLineRule(STLineSpacingRule.AUTO);  

        pPr.setSpacing(spacing);  

        p.setPPr(pPr);  

    }

    

  

}




你可能感兴趣的:(Docx4j)