Apache-poi-word-font-style
提供:Dev Guides
Apache POI Word-フォントと配置
この章では、Javaを使用してWord文書にさまざまなフォントスタイルと配置を適用する方法を示します。 通常、フォントスタイルには、フォントサイズ、タイプ、太字、斜体、下線が含まれます。 また、配置は左、中央、右、および正当化に分類されます。
フォントスタイル
次のコードは、フォントの異なるスタイルを設定するために使用されます-
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class FontStyle {
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("fontstyle.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//Set Bold an Italic
XWPFRun paragraphOneRunOne = paragraph.createRun();
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setItalic(true);
paragraphOneRunOne.setText("Font Style");
paragraphOneRunOne.addBreak();
//Set text Position
XWPFRun paragraphOneRunTwo = paragraph.createRun();
paragraphOneRunTwo.setText("Font Style two");
paragraphOneRunTwo.setTextPosition(100);
//Set Strike through and Font Size and Subscript
XWPFRun paragraphOneRunThree = paragraph.createRun();
paragraphOneRunThree.setStrike(true);
paragraphOneRunThree.setFontSize(20);
paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
paragraphOneRunThree.setText(" Different Font Styles");
document.write(out);
out.close();
System.out.println("fontstyle.docx written successully");
}
}
上記のコードを FontStyle.java として保存し、次のようにコマンドプロンプトからコンパイルして実行します-
$javac FontStyle.java
$java FontStyle
現在のディレクトリに fontstyle.docx という名前のWordファイルが生成され、コマンドプロンプトに次の出力が表示されます-
fontstyle.docx written successfully
*fontstyle.docx* ファイルは次のようになります。
アライメント
次のコードは、段落テキストに配置を設定するために使用されます-
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class AlignParagraph {
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("alignparagraph.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//Set alignment paragraph to RIGHT
paragraph.setAlignment(ParagraphAlignment.RIGHT);
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.");
//Create Another paragraph
paragraph = document.createParagraph();
//Set alignment paragraph to CENTER
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("The endeavour started by Mohtashim, an AMU " +
"alumni, who is the founder and the managing director " +
"of Tutorials Point (I) Pvt. Ltd. He came up with the " +
"website finddevguides.com in year 2006 with the help" +
"of handpicked freelancers, with an array of tutorials" +
" for computer programming languages. ");
document.write(out);
out.close();
System.out.println("alignparagraph.docx written successfully");
}
}
上記のコードを AlignParagraph.java として保存し、コマンドプロンプトから次のようにコンパイルして実行します-
$javac AlignParagraph.java
$java AlignParagraph
現在のディレクトリに alignparagraph.docx という名前のWordファイルが生成され、コマンドプロンプトに次の出力が表示されます-
alignparagraph.docx written successfully
*alignparagraph.docx* ファイルは次のようになります-