ตัวอย่างโคด
package com.java.poi.word;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class SetFontStyleInWord {
public static void main(String[] args) {
try {
// path ของไฟล์
String fileName = "C:\\poi\\SetFontStyleInWord.docx";
// สร้างเอกสารเปล่าๆ
XWPFDocument document = new XWPFDocument();
// บันทึกเอกสารไว้ในระบบตาม path ที่ระบุ
FileOutputStream out = new FileOutputStream(fileName);
// สร้าง พารากราฟ
XWPFParagraph paragraph = document.createParagraph();
// ทำตัวหนา
XWPFRun textBold = paragraph.createRun();
textBold.setBold(true);
textBold.setText("Font Bold");
textBold.addBreak();
// ทำตัวเอียง
XWPFRun textItalic = paragraph.createRun();
textItalic.setItalic(true);
textItalic.setText("Font Italic");
textItalic.addBreak();
// ทำตัวหนาและเอียง
XWPFRun textBoldItalic = paragraph.createRun();
textBoldItalic.setBold(true);
textBoldItalic.setItalic(true);
textBoldItalic.setText("Font Bold And Italic");
textBoldItalic.addBreak();
// ขีดเส้นใต้หนึ่งเส้น
XWPFRun textUnderlineOne = paragraph.createRun();
textUnderlineOne.setUnderline(UnderlinePatterns.SINGLE);
textUnderlineOne.setText("Font Underline One");
textUnderlineOne.addBreak();
// ขีดเส้นใต้สองเส้น
XWPFRun textUnderlineTwo = paragraph.createRun();
textUnderlineTwo.setUnderline(UnderlinePatterns.DOUBLE);
textUnderlineTwo.setText("Font Underline Two");
textUnderlineTwo.addBreak();
// กำหนดขนาด
XWPFRun textSize = paragraph.createRun();
textSize.setFontSize(30);
textSize.setText("Font Size");
textSize.addBreak();
// กำหนดตำแหน่งของข้อความ
XWPFRun textPosition = paragraph.createRun();
textPosition.setText("Font Position");
textPosition.setTextPosition(100);
// ทำเส้นขีดฆ่า
XWPFRun textStrike = paragraph.createRun();
textStrike.setStrikeThrough(true);
textStrike.setText("Text Strike");
document.write(out);
out.close();
System.out.println("Word document font style.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ไฟล์ที่ถูกสร้าง
ผลลัพธ์ที่ได้


0 comments:
Post a Comment