Apache POI Word - 邊框

2018-12-27 18:41 更新

在本章中,您將學習如何使用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 
   {
   //Blank Document
   XWPFDocument document= new XWPFDocument(); 
        
   //Write the Document in file system
   FileOutputStream out = new FileOutputStream(
   new File("applyingborder.docx"));
        
   //create paragraph
   XWPFParagraph paragraph = document.createParagraph();
        
   //Set bottom border to paragraph
   paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
        
   //Set left border to paragraph
   paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
        
   //Set right border to paragraph
   paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
        
   //Set top border to paragraph
   paragraph.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的文件中,從命令提示符處編譯并執(zhí)行它,如下所示:

$javac ApplyingBorder.java
$java ApplyingBorder

如果系統(tǒng)配置了POI庫,那么它將編譯并執(zhí)行,以在當前目錄中生成名為 appliedborder.docx 的Word文檔,并顯示以下輸出:

applyingborder.docx written successfully

appliedborder.docx 文件如下所示:


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號