Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Add text to the second page
The following example shows you how to add text to the second page.
import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.sections.PageMargins; import com.independentsoft.office.word.sections.PageSize; import com.independentsoft.office.word.sections.Section; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); PageMargins margins = new PageMargins(); margins.setBottom(1440); // 1 inch margins.setLeft(1440); // 1 inch margins.setRight(1440); // 1 inch margins.setTop(1440); // 1 inch margins.setFooter(720); // 1/2 inch margins.setHeader(720); // 1/2 inch Section section = new Section(); section.setPageSize(new PageSize(12240, 15840)); //8.5 x 11 in section.setPageMargins(margins); Run run1 = new Run(); run1.addText("Some text on the first page."); Paragraph paragraph1 = new Paragraph(); paragraph1.add(run1); Paragraph paragraph2 = new Paragraph(); paragraph2.add(run1); Paragraph paragraph3 = new Paragraph(); paragraph3.add(run1); paragraph3.setSection(section); //last paragraph on the first page //first page doc.getBody().add(paragraph1); doc.getBody().add(paragraph2); doc.getBody().add(paragraph3); Run run2 = new Run(); run2.addText("Some text on the second page."); Paragraph paragraph4 = new Paragraph(); paragraph4.add(run2); //second page doc.getBody().add(paragraph4); 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*