Apache-poi-word-borders

提供:Dev Guides
移動先:案内検索

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 finddevguides.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* ファイルは次のようになります-

境界線段落