ตัวอย่างโคด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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