Independentsoft
Any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Section break
The following example shows you how to insert section breaks.
import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.ParagraphProperties; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.sections.Section; import com.independentsoft.office.word.sections.SectionType; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); // First section content Run run1 = new Run(); run1.addText("This is the first section. It has default page orientation."); Paragraph paragraph1 = new Paragraph(); paragraph1.add(run1); // Add section break (next page) Section section1 = new Section(); section1.setType(SectionType.NEXT_PAGE); ParagraphProperties props1 = new ParagraphProperties(); props1.setSection(section1); paragraph1.setParagraphProperties(props1); // Second section content Run run2 = new Run(); run2.addText("This is the second section. It starts on a new page."); Paragraph paragraph2 = new Paragraph(); paragraph2.add(run2); // Add continuous section break Section section2 = new Section(); section2.setType(SectionType.CONTINUOUS); ParagraphProperties props2 = new ParagraphProperties(); props2.setSection(section2); paragraph2.setParagraphProperties(props2); // Third section content Run run3 = new Run(); run3.addText("This is the third section. It continues on the same page."); Paragraph paragraph3 = new Paragraph(); paragraph3.add(run3); doc.getBody().add(paragraph1); doc.getBody().add(paragraph2); doc.getBody().add(paragraph3); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*