In this chapter, you will learn how to apply border to a paragraph using Java programming.

Applying Border

The following code is used to apply Borders in a Document −

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import org.apache.poi.xwpf.usermodel.Borders;
  4. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  5. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  6. import org.apache.poi.xwpf.usermodel.XWPFRun;
  7.  
  8. public class ApplyingBorder {
  9.  
  10. public static void main(String[] args)throws Exception {
  11.  
  12. //Blank Document
  13. XWPFDocument document = new XWPFDocument();
  14.  
  15. //Write the Document in file system
  16. FileOutputStream out = new FileOutputStream(new File(“applyingborder.docx”));
  17.  
  18. //create paragraph
  19. XWPFParagraph paragraph = document.createParagraph();
  20.  
  21. //Set bottom border to paragraph
  22. paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
  23.  
  24. //Set left border to paragraph
  25. paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
  26.  
  27. //Set right border to paragraph
  28. paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
  29.  
  30. //Set top border to paragraph
  31. paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);
  32.  
  33. XWPFRun run = paragraph.createRun();
  34. run.setText(“At tutorialspoint.com, we strive hard to +
  35. provide quality tutorials for self-learning +
  36. purpose in the domains of Academics, Information +
  37. Technology, Management and Computer Programming +
  38. Languages.”);
  39.  
  40. document.write(out);
  41. out.close();
  42. System.out.println(“applyingborder.docx written successully”);
  43. }
  44. }

Save the above code in a file named ApplyingBorder.java, compile and execute it from the command prompt as follows −

  1. $javac ApplyingBorder.java
  2. $java ApplyingBorder

If your system is configured with the POI library, then it will compile and execute to generate a Word document named applyingborder.docx in your current directory and display the following output −

  1. applyingborder.docx written successfully

The applyingborder.docx file looks as follows −

borderparagraph.jpg