public static void main(String[] args) throws Exception {
Docx4J_例子2 t = new Docx4J_例子2();
WordprocessingMLPackage wordMLPackage = t
.createWordprocessingMLPackage();
MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
ObjectFactory factory = Context.getWmlObjectFactory();
Relationship relationship = t.createHeaderPart(wordMLPackage, mp,
factory, false, "3");
relationship = t.createTextHeaderPart(wordMLPackage, mp, factory,
"我是页眉,独乐乐不如众乐乐", true, "3", JcEnumeration.CENTER);
t.addParagraphTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory, STBrType.PAGE);
t.createHeaderReference(wordMLPackage, mp, factory, relationship);
t.createNormalTableTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
t.createTableTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
P paragraph=factory.createP();
CTBorder topBorder=new CTBorder() ;
topBorder.setSpace(new BigInteger("1"));
topBorder.setSz(new BigInteger("2"));
topBorder.setVal(STBorder.WAVE);
t.createParagraghLine(wordMLPackage, mp, factory, paragraph, topBorder, topBorder, topBorder, topBorder);
mp.addObject(paragraph);
t.createHyperlink(wordMLPackage, mp, factory,paragraph,
"mailto:[email protected]?subject=docx4j测试", "联系我","微软雅黑","24",JcEnumeration.CENTER);
// 页脚
// relationship = t.createFooterPart(wordMLPackage, mp, factory,
// false,"3");
// relationship = t.createTextFooterPart(wordMLPackage, mp,
// factory,"我是页脚", true, "3", JcEnumeration.CENTER);
relationship = t.createFooterPageNumPart(wordMLPackage, mp, factory,
false, "3", JcEnumeration.CENTER);
t.createFooterReference(wordMLPackage, mp, factory, relationship);
t.saveWordPackage(wordMLPackage, new File(
"f:/saveFile/temp/s7_simple.docx"));
}
public void addPageBreak(WordprocessingMLPackage wordMLPackage,
ObjectFactory factory, STBrType sTBrType) {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Br breakObj = new Br();
breakObj.setType(sTBrType);
P paragraph = factory.createP();
paragraph.getContent().add(breakObj);
documentPart.addObject(paragraph);
}
public void addPageBreak(WordprocessingMLPackage wordMLPackage,
ObjectFactory factory) {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Br breakObj = new Br();
breakObj.setType(STBrType.PAGE);
P paragraph = factory.createP();
paragraph.getContent().add(breakObj);
documentPart.addObject(paragraph);
}
/**
* 向文档添加一个换行符
*/
private static void addPageBreak() {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Br breakObj = new Br();
breakObj.setType(STBrType.PAGE);
P paragraph = factory.createP();
paragraph.getContent().add(breakObj);
documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
/**
* Adds a page break to the document.
*
* @param documentPart
*/
private static void addPageBreak(MainDocumentPart documentPart) {
Br breakObj = new Br();
breakObj.setType(STBrType.PAGE);
P paragraph = factory.createP();
paragraph.getContent().add(breakObj);
documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
public void addPageBreak(WordprocessingMLPackage wordMLPackage,
ObjectFactory factory) {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Br breakObj = new Br();
breakObj.setType(STBrType.PAGE);
P paragraph = factory.createP();
paragraph.getContent().add(breakObj);
documentPart.addObject(paragraph);
}
/**
* @Description: 段落添加Br 页面Break(分页符)
*/
public static void addPageBreak(P para, STBrType sTBrType) {
Br breakObj = new Br();
breakObj.setType(sTBrType);
para.getContent().add(breakObj);
}
/**
* @Description: 段落添加Br 页面Break(分页符)
*/
public void addPageBreak(P para, STBrType sTBrType) {
Br breakObj = new Br();
breakObj.setType(sTBrType);
para.getContent().add(breakObj);
}
@Override
public Node toNode(AbstractWmlConversionContext context, Object unmarshalledNode,
Node modelContent, TransformState state, Document doc)
throws TransformerException {
Br modelData = (Br)unmarshalledNode;
Element ret;
if (modelData.getType()!=null
&& modelData.getType().equals(STBrType.PAGE)) {
ret = doc.createElementNS(XSL_FO, "block");
ret.setAttribute("break-before", "page");
} else {
/* w:br is converted to fo:block with @line-height="0pt", to get rid of
* unwanted vertical whitespace. If effect, it results in a carriage return.
*
* However, some users use contiguous w:br to create vertical spacing
* which works in Word. But FOP only does a single carriage return
* for contiguous fo:block/@line-height="0pt"
*
* So we need to work around this. Possible approaches:
*
* 1. pre-process to convert the w:br to a w:p. This is difficult,
* for example, numbering.
*
* 2. set TransformState, so the attribute is not set for the 2nd w:br
*
* 3. set some custom attribute on the br
*
* 4. post-process the XSL FO, so in contiguous fo:block/@line-height="0pt"
* the attributes on 2nd and subsequent are changed.
*
* eg in org/docx4j/convert/out/fo/renderers/AbstractFORenderer_POSTPROCESSING.xslt
*
* It turns out the easiest approach is to catch the special case in the XSLT
*
*/
ret = doc.createElementNS(XSL_FO, "block"); // yuck .. block/inline/block
// see http://stackoverflow.com/a/3664468/1031689 answer
// at http://stackoverflow.com/questions/3661483/inserting-a-line-break-in-a-pdf-generated-from-xsl-fo-using-xslvalue-of
ret.setAttribute("linefeed-treatment", "preserve");
ret.setAttribute("white-space-treatment", "preserve");
//ret.setAttribute("space-after", "0in"); // doesn't help
ret.setAttribute("line-height", "0pt"); // suits FOP 1.1
ret.setTextContent("\n");
}
return ret;
}