Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

java - How to add continuous section break to XWPFDocument?

I would like to have 2 sections on the same page in XWPFDocument. First section should have only 1 column, second section should have 2 columns. Currently, I am using following code:

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

This produces 2 sections with correct number of columns, but the sections are not on the same page. How to apply continuous section break instead of next page section break?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The CTSectPr needs to be of CTSectType CONTINUOUS.

Example:

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);
  out.close();
  document.close();

 }
}

Up to apache poi 4.1.2 this code needs the full jar of all of the schemas ooxml-schemas-*.jar as mentioned in FAQ-N10025.


The principle of sections having multiple columns in Word are as follows:

Per default - using no special settings - Word uses a one column section.

If section settings shall change, then a paragraph with section settings for the section above is needed. All body elements above that paragraph use those settings. All body elements after that paragraph are in a new section and use settings of next paragraph having section settings or use sections settings in document body.

At last section settings for last section above needs to be in body settings.


Using apache poi 5.0.0 the org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff was removed in poi-ooxml-full-5.0.0.jar. And CTColumns.setEqualWidth and CTColumns.setSep uses java.langObject as attribute type now.

So it would must be now:

  //ctColumns.setEqualWidth(STOnOff.OFF);
  ctColumns.setEqualWidth("0");
  //ctColumns.setSep(STOnOff.ON);
  ctColumns.setSep("1");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...