如何向XWPFDocument添加连续分节符?

时间:2016-12-15 14:43:11

标签: java apache-poi

我想在XWPFDocument中的同一页面上有2个部分。第一节应该只有1列,第二节应该有2列。目前,我使用以下代码:

CTBody body = document.getDocument().getBody();

// 1-column section
section = body.addNewSectPr();
columns = CTColumns.Factory.newInstance();
columns.setNum(new BigInteger("1"));
section.setCols(columns);

paragraph = document.createParagraph();
paragraph.getCTP().addNewPPr().setSectPr(section);
run = paragraph.createRun();
run.setText(firstSectionContent);

//2-column section
section = body.addNewSectPr();
columns = CTColumns.Factory.newInstance();
columns.setNum(new BigInteger("2"));
section.setCols(columns);

paragraph = document.createParagraph();
paragraph.getCTP().addNewPPr().setSectPr(section);
run = paragraph.createRun();
run.setText(secondSectionContent);

这会产生2个具有正确列数的部分,但这些部分不在同一页面上。 如何应用连续分节符而不是下一页分节符?

1 个答案:

答案 0 :(得分:1)

CTSectPr必须是CTSectType CONTINUOUS

示例:

import java.io.File;
import java.io.FileOutputStream;

import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.Borders;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColumns;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColumn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STSectionMark;

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

  XWPFDocument document= new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("One column on top. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.");

  paragraph = document.createParagraph();

  //paragraph with section setting for one column section above
  paragraph = document.createParagraph();
  CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
  CTColumns ctColumns = ctSectPr.addNewCols();
  ctColumns.setNum(BigInteger.valueOf(1));

  //left column

  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.setText("The left side");
  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.");

  paragraph = document.createParagraph();

  // right column 

  //paragraph with column break
  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.addBreak(BreakType.COLUMN);
  run=paragraph.createRun();  
  run.setText("The right side");
  //left border for the paragrapphs on right side
  paragraph.setBorderLeft(Borders.THREE_D_EMBOSS);
  paragraph.getCTP().getPPr().getPBdr().getLeft().setSz(BigInteger.valueOf(20));

  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.");
  paragraph.setBorderLeft(Borders.THREE_D_EMBOSS);
  paragraph.getCTP().getPPr().getPBdr().getLeft().setSz(BigInteger.valueOf(20));

  paragraph = document.createParagraph();
  paragraph.setBorderLeft(Borders.THREE_D_EMBOSS);
  paragraph.getCTP().getPPr().getPBdr().getLeft().setSz(BigInteger.valueOf(20));

  //paragraph with section break continuous for two column section above
  paragraph = document.createParagraph();
  ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
  ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
  ctColumns = ctSectPr.addNewCols();
  ctColumns.setNum(BigInteger.valueOf(2));
  ctColumns.setEqualWidth(STOnOff.OFF);
  ctColumns.setSep(STOnOff.ON);
  CTColumn ctColumn = ctColumns.addNewCol();
  ctColumn.setW(BigInteger.valueOf(6000));
  ctColumn.setSpace(BigInteger.valueOf(300));
  ctColumn = ctColumns.addNewCol();
  ctColumn.setW(BigInteger.valueOf(3000));
  paragraph.setBorderLeft(Borders.THREE_D_EMBOSS);
  paragraph.getCTP().getPPr().getPBdr().getLeft().setSz(BigInteger.valueOf(20));

  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.setText("One column on bottom");

  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.");

  //section setting continuous for one column section above
  CTDocument1 ctDocument = document.getDocument();
  CTBody ctBody = ctDocument.getBody();
  ctSectPr = ctBody.addNewSectPr();
  ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
  ctColumns = ctSectPr.addNewCols();
  ctColumns.setNum(BigInteger.valueOf(1));

  FileOutputStream out = new FileOutputStream("Word2ColumnPage.docx");  
  document.write(out);

 }
}

此代码需要FAQ-N10025中提到的所有模式ooxml-schemas-1.3.jar的完整jar。