Apache POI Word - 边框

在本章中,您将学习如何使用 Java 编程将边框应用于段落。

应用边框

以下代码用于在文档中应用边框 −

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class ApplyingBorder {

   public static void main(String[] args)throws Exception {

    //空白文档
    XWPFDocument document = new XWPFDocument();
    
    //将文档写入文件系统
    FileOutputStream out = new FileOutputStream(new File("applyingborder.docx"));
    
    //创建段落
    XWPFParagraph passage = document.createParagraph();
    
    //设置段落的下边框
    passage.setBorderBottom(Borders.BASIC_BLACK_DASHES);
    
    //设置段落的左边框
    passage.setBorderLeft(Borders.BASIC_BLACK_DASHES);
    
    //设置段落的右边框
    passage.setBorderRight(Borders.BASIC_BLACK_DASHES);
    
    //设置段落的上边框
    passage.setBorderTop(Borders.BASIC_BLACK_DASHES);
        
      XWPFRun run = paragraph.createRun();
         run.setText("At tutorialspoint.com, we strive hard to " +
         "provide quality tutorials for self-learning " +
         "purpose in the domains of Academics, Information " +
         "Technology, Management and Computer Programming " +
         "Languages.");
        
      document.write(out);
      out.close();
      System.out.println("applyingborder.docx written successully");
   }
}

将上述代码保存在名为 ApplyingBorder.java 的文件中,然后从命令提示符编译并执行它,如下所示 −

$javac ApplyingBorder.java
$java ApplyingBorder

如果您的系统配置了 POI 库,则它将编译并执行以在当前目录中生成名为 applyingborder.docx 的 Word 文档,并显示以下输出 −

applyingborder.docx 写入成功

applyingborder.docx 文件如下所示 −

Border Paragraph