Kata Apache POI - Perbatasan

Pada bab ini, Anda akan belajar bagaimana menerapkan border ke paragraf menggunakan pemrograman Java.

Menerapkan Perbatasan

Kode berikut digunakan untuk menerapkan batas dalam dokumen -

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");
   }
}

Simpan kode di atas dalam sebuah file bernama ApplyingBorder.java, kompilasi dan jalankan dari command prompt sebagai berikut -

$javac ApplyingBorder.java
$java ApplyingBorder

Jika sistem Anda dikonfigurasi dengan pustaka POI, maka sistem akan mengkompilasi dan mengeksekusi untuk menghasilkan dokumen Word bernama applyingborder.docx di direktori Anda saat ini dan tampilkan output berikut -

applyingborder.docx written successfully

Itu applyingborder.docx file terlihat sebagai berikut -