docx4j设置ppr段前及断后空白距离
时间: 2023-05-30 15:01:46 浏览: 118
最新 docx4j-master
使用docx4j可以通过以下步骤设置ppr段前及断后空白距离:
1. 获取段落属性(PPR)对象
```java
PPr ppr = para.getParagraph().getPPr();
if (ppr == null) {
ppr = Context.getWmlObjectFactory().createPPr();
para.getParagraph().setPPr(ppr);
}
```
2. 创建段前和断后的间距对象(JAXBElement对象)
```java
// 段前间距
JAXBElement<BigInteger> spacingBefore = createSpacingObject(BigInteger.valueOf(200));
// 断后间距
JAXBElement<BigInteger> spacingAfter = createSpacingObject(BigInteger.valueOf(200));
```
3. 将间距对象添加到ppr对象中
```java
ppr.setSpacingBefore(spacingBefore);
ppr.setSpacingAfter(spacingAfter);
```
完整代码示例:
```java
private static void setParagraphSpacing(WordprocessingMLPackage wordMLPackage, P para) {
// 获取段落属性对象
PPr ppr = para.getParagraph().getPPr();
if (ppr == null) {
ppr = Context.getWmlObjectFactory().createPPr();
para.getParagraph().setPPr(ppr);
}
// 创建段前和段后间距对象
JAXBElement<BigInteger> spacingBefore = createSpacingObject(BigInteger.valueOf(200));
JAXBElement<BigInteger> spacingAfter = createSpacingObject(BigInteger.valueOf(200));
// 设置段前和段后间距
ppr.setSpacingBefore(spacingBefore);
ppr.setSpacingAfter(spacingAfter);
}
private static JAXBElement<BigInteger> createSpacingObject(BigInteger value) {
CTSpacing spacing = Context.getWmlObjectFactory().createCTSpacing();
spacing.setVal(value);
spacing.setLine(BigInteger.valueOf(240)); // 设置行距
return Context.getWmlObjectFactory().createPPrBaseSpacingBefore(spacing);
}
```
阅读全文