ตัวอย่างโคด
package com.java.poi.word;
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 SetWordAlignment {
public static void main(String[] args) {
try {
// path ของไฟล์
String fileName = "C:\\poi\\SetWordAlignment.docx";
// สร้างเอกสารเปล่าๆ
XWPFDocument document = new XWPFDocument();
// บันทึกเอกสารไว้ในระบบตาม path ที่ระบุ
FileOutputStream out = new FileOutputStream(fileName);
// สร้าง พารากราฟ
XWPFParagraph paragraphRight = document.createParagraph();
// วางตำแหน่งข้อความจากขวา
paragraphRight.setAlignment(ParagraphAlignment.RIGHT);
XWPFRun runRight = paragraphRight.createRun();
runRight.setText("LOVE IS... "
+ "being willing to have your life complicated "
+ "by the needs and struggles of others without impatience "
+ "or anger.");
// สร้าง พารากราฟ
XWPFParagraph paragraphCenter = document.createParagraph();
// วางตำแหน่งข้อความจากตรงกลาง
paragraphCenter.setAlignment(ParagraphAlignment.CENTER);
XWPFRun runCenter = paragraphCenter.createRun();
runCenter.setText("LOVE IS... "
+ "being willing to have your life complicated "
+ "by the needs and struggles of others without impatience "
+ "or anger.");
document.write(out);
out.close();
System.out.println("Word document Alignment.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ไฟล์ที่ถูกสร้าง
ผลลัพธ์ที่ได้


0 comments:
Post a Comment