Class Document
java.lang.Object
io.leandev.appfuse.document.Document
- All Implemented Interfaces:
AutoCloseable
Word 文件(.docx)
文件產生入口,封裝底層 Apache POI 的 XWPFDocument,不洩漏 POI/OOXML 型別。
一般以 open(InputStream) 載入既有 .docx 範本,於其上編排後 write(OutputStream) 輸出。
範本套印
try (Document doc = Document.open(templateStream)) {
doc.body().findParagraphByText("[customer]").ifPresent(p -> p.setText("玫瑰花坊"));
Table items = doc.body().tables().get(0);
for (OrderLine line : order.getLines()) {
TableRow row = items.appendRow();
row.cell(0).setText(line.name());
row.cell(1).setText(line.amount());
}
doc.write(outputStream);
}
- Since:
- 4.0.0
-
Method Summary
Modifier and TypeMethodDescriptionvoid在文件末端加入分頁void在文件末端加入分節(下一頁)body()取得文件本文容器voidclose()static Documentcreate()建立空白文件取得首頁頁尾(不存在則建立)取得首頁頁首(不存在則建立)footer()取得預設頁尾(不存在則建立)header()取得預設頁首(不存在則建立)static Documentopen(InputStream template) 從輸入串流載入既有文件(範本)voidrestartPageNumber(int from) 重設頁碼起始值取得當前章節頁尾(單章節文件等同footer())取得當前章節頁首(單章節文件等同header())void設定頁尾邊界void設定頁首邊界voidsetMargins(Length top, Length right, Length bottom, Length left) 設定頁面四邊邊界voidsetOrientation(Orientation orientation) 設定頁面方向voidsetPageSize(Length width, Length height) 設定頁面大小voidsetPageSize(Paper paper) 設定頁面大小(紙張預設)voidwrite(OutputStream out) 將文件寫出
-
Method Details
-
open
從輸入串流載入既有文件(範本)- Parameters:
template-.docx輸入串流- Returns:
- 文件
-
create
-
body
-
header
-
firstHeader
-
sectionHeader
-
setPageSize
-
setPageSize
-
setOrientation
-
setMargins
-
setMarginHeader
-
addPageBreak
public void addPageBreak()在文件末端加入分頁 -
addSectionBreak
public void addSectionBreak()在文件末端加入分節(下一頁) -
restartPageNumber
public void restartPageNumber(int from) 重設頁碼起始值- Parameters:
from- 起始頁碼
-
write
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-