Independentsoft
Professional software libraries for developers
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Paragraph borders
The following example shows you how to add borders to a paragraph.
import com.independentsoft.office.word.BottomBorder; import com.independentsoft.office.word.Color; import com.independentsoft.office.word.LeftBorder; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.RightBorder; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.StandardBorderStyle; import com.independentsoft.office.word.TopBorder; import com.independentsoft.office.word.WordDocument; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); // Create paragraph with borders Run run = new Run(); run.addText("This paragraph has borders on all sides."); Paragraph paragraph = new Paragraph(); paragraph.add(run); // Create borders Color borderColor = new Color("#000000"); int borderWidth = 4; // size in eighths of a point TopBorder topBorder = new TopBorder(); topBorder.setStyle(StandardBorderStyle.SINGLE_LINE); topBorder.setColor(borderColor); topBorder.setWidth(borderWidth); BottomBorder bottomBorder = new BottomBorder(); bottomBorder.setStyle(StandardBorderStyle.SINGLE_LINE); bottomBorder.setColor(borderColor); bottomBorder.setWidth(borderWidth); LeftBorder leftBorder = new LeftBorder(); leftBorder.setStyle(StandardBorderStyle.SINGLE_LINE); leftBorder.setColor(borderColor); leftBorder.setWidth(borderWidth); RightBorder rightBorder = new RightBorder(); rightBorder.setStyle(StandardBorderStyle.SINGLE_LINE); rightBorder.setColor(borderColor); rightBorder.setWidth(borderWidth); paragraph.setTopBorder(topBorder); paragraph.setBottomBorder(bottomBorder); paragraph.setLeftBorder(leftBorder); paragraph.setRightBorder(rightBorder); // Create paragraph with bottom border only (horizontal line effect) Run run2 = new Run(); run2.addText("This paragraph has only a bottom border."); Paragraph paragraph2 = new Paragraph(); paragraph2.add(run2); BottomBorder bottomOnly = new BottomBorder(); bottomOnly.setStyle(StandardBorderStyle.SINGLE_LINE); bottomOnly.setColor(new Color("#0000FF")); bottomOnly.setWidth(8); paragraph2.setBottomBorder(bottomOnly); doc.getBody().add(paragraph); doc.getBody().add(paragraph2); 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*