- Java 程式設計示例
- 示例 - 主頁
- 示例 - 環境
- 示例 - 字串
- 示例 - 陣列
- 示例 - 日期和時間
- 示例 - 方法
- 示例 - 檔案
- 示例 - 目錄
- 示例 - 異常
- 示例 - 資料結構
- 示例 - 集合
- 示例 - 網路
- 示例 - 多執行緒
- 示例 - Applet
- 示例 - 簡單 GUI
- 示例 - JDBC
- 示例 - 正則表示式
- 示例 - Apache PDF Box
- 示例 - Apache POI PPT
- 示例 - Apache POI Excel
- 示例 - Apache POI Word
- 示例 - OpenCV
- 示例 - Apache Tika
- 示例 - iText
- Java 教程
- Java - 教程
- Java 實用資源
- Java - 快速指南
- Java - 實用資源
使用 Java 在 Word 文件中對齊文字
問題描述
使用 Java 在 Word 文件中對齊文字。
解決方案
以下是使用 Java 在 Word 文件中對齊文字的程式。
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 AlignTextInWord {
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
("C:/poiword/alignparagraph.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//Set alignment paragraph to RIGHT
paragraph.setAlignment(ParagraphAlignment.RIGHT);
XWPFRun run = paragraph.createRun();
run.setText("At tutorialspoint.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 tutorialspoint.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");
}
}
輸出
java_apache_poi_word
廣告