我使用docx4j从html生成Word文档 我想在我的word文档中显示页面边框 有没有样品可以做到这一点?
答案 0 :(得分:0)
页面边框位于sectPr元素中,例如:
<w:pgBorders w:offsetFrom="page">
<w:top w:val="single" w:color="auto" w:sz="4" w:space="24"/>
<w:left w:val="single" w:color="auto" w:sz="4" w:space="24"/>
<w:bottom w:val="single" w:color="auto" w:sz="4" w:space="24"/>
<w:right w:val="single" w:color="auto" w:sz="4" w:space="24"/>
</w:pgBorders>
您可以生成与首选边框属性匹配的代码,方法是在Word中根据需要设置docx样式,然后使用Docx4j Helper Word AddIn或docx4j webapp。
对于上述XML,它生成以下两种形式的代码。
// Create object for pgBorders
SectPr.PgBorders sectprpgborders = wmlObjectFactory.createSectPrPgBorders();
// Create object for top
CTBorder border = wmlObjectFactory.createCTBorder();
sectprpgborders.setTop(border);
border.setVal(org.docx4j.wml.STBorder.SINGLE);
border.setSz( BigInteger.valueOf( 4) );
border.setColor( "auto");
border.setSpace( BigInteger.valueOf( 24) );
// Create object for left
CTBorder border2 = wmlObjectFactory.createCTBorder();
sectprpgborders.setLeft(border2);
border2.setVal(org.docx4j.wml.STBorder.SINGLE);
border2.setSz( BigInteger.valueOf( 4) );
border2.setColor( "auto");
border2.setSpace( BigInteger.valueOf( 24) );
// Create object for bottom
CTBorder border3 = wmlObjectFactory.createCTBorder();
sectprpgborders.setBottom(border3);
border3.setVal(org.docx4j.wml.STBorder.SINGLE);
border3.setSz( BigInteger.valueOf( 4) );
border3.setColor( "auto");
border3.setSpace( BigInteger.valueOf( 24) );
// Create object for right
CTBorder border4 = wmlObjectFactory.createCTBorder();
sectprpgborders.setRight(border4);
border4.setVal(org.docx4j.wml.STBorder.SINGLE);
border4.setSz( BigInteger.valueOf( 4) );
border4.setColor( "auto");
border4.setSpace( BigInteger.valueOf( 24) );
sectprpgborders.setOffsetFrom(org.docx4j.wml.STPageBorderOffset.PAGE);
方法2:
String openXML
= "<w:pgBorders w:offsetFrom=\"page\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" >"
+ "<w:top w:color=\"auto\" w:space=\"24\" w:sz=\"4\" w:val=\"single\"/>"
+ "<w:left w:color=\"auto\" w:space=\"24\" w:sz=\"4\" w:val=\"single\"/>"
+ "<w:bottom w:color=\"auto\" w:space=\"24\" w:sz=\"4\" w:val=\"single\"/>"
+ "<w:right w:color=\"auto\" w:space=\"24\" w:sz=\"4\" w:val=\"single\"/>"
+ "</w:pgBorders>";
SectPr.PgBorders sectprpgborders = (SectPr.PgBorders)XmlUtils.unmarshalString(openXML);
// Hand edited; may need minor adjustment
然后它就是相关sectPr的sectpr.setPgBorders(sectprpgborders)。