public class SectionStart
Example: Example:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Add text to the first section and that comes with a blank document,
// then add a new section that starts a new page and give it text as well
builder.writeln("This text is in section 1.");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.writeln("This text is in section 2.");
// Section break types determine how a new section gets split from the previous section
// By inserting a "SectionBreakNewPage" type section break, we've set this section's SectionStart value to "NewPage"
Assert.assertEquals(SectionStart.NEW_PAGE, doc.getSections().get(1).getPageSetup().getSectionStart());
// Insert a new column section the same way
builder.insertBreak(BreakType.SECTION_BREAK_NEW_COLUMN);
builder.writeln("This text is in section 3.");
Assert.assertEquals(SectionStart.NEW_COLUMN, doc.getSections().get(2).getPageSetup().getSectionStart());
// We can change the types of section breaks by assigning different values to each section's SectionStart
// Setting their values to "Continuous" will put no visible breaks between sections
// and will leave all the content of this document on one page
doc.getSections().get(1).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
doc.getSections().get(2).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
doc.save(getArtifactsDir() + "PageSetup.SetSectionStart.docx");
Document doc = new Document();
// A newly created blank document still comes one section, one body and one paragraph
// Calling this method will remove all those nodes to completely empty the document
doc.removeAllChildren();
// This document now has no composite nodes that content can be added to
// If we wish to edit it, we will need to repopulate its node collection,
// which we will start to do with by creating a new Section node
Section section = new Section(doc);
// Append the section to the document
doc.appendChild(section);
// Lets set some properties for the section
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// A section needs a body, which will contain all other nodes that can be edited
Body body = new Body(doc);
section.appendChild(body);
// The body needs to have at least one paragraph
// Note that the paragraph has not yet been added to the document, but we have to specify the parent document
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information
Paragraph para = new Paragraph(doc);
body.appendChild(para);
// We can set some formatting for the paragraph
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// Now we can begin adding content to the document
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
Assert.assertEquals("Hello World!" + ControlChar.SECTION_BREAK_CHAR, doc.getText());
doc.save(getArtifactsDir() + "Section.CreateFromScratch.docx");
Field Summary | ||
---|---|---|
static final int | CONTINUOUS | |
The new section starts on the same page as the previous section.
|
||
static final int | NEW_COLUMN | |
The section starts from a new column.
|
||
static final int | NEW_PAGE | |
The section starts from a new page.
|
||
static final int | EVEN_PAGE | |
The section starts on a new even page.
|
||
static final int | ODD_PAGE | |
The section starts on a new odd page.
|
public static final int CONTINUOUS
public static final int NEW_COLUMN
public static final int NEW_PAGE
public static final int EVEN_PAGE
public static final int ODD_PAGE