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 {

      //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 的檔案,並透過命令提示符按以下方式編譯和執行 −

$javac ApplyingBorder.java
$java ApplyingBorder

如果您的系統已配置 POI 庫,則它將進行編譯和執行,從而在當前目錄中生成名為 applyingborder.docx 的 Word 文件,並顯示以下輸出 −

applyingborder.docx written successfully

applyingborder.docx 檔案如下所示 −

Border Paragraph
廣告
© . All rights reserved.