itext库产生word文档示例(.doc)
生活随笔
收集整理的這篇文章主要介紹了
itext库产生word文档示例(.doc)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
itext庫生產(chǎn)word文檔示例
1 // 設(shè)置中文字體------------------------------------------------2 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
3 "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
4 // 標(biāo)題字體風(fēng)格
5 Font titleFont = new Font(bfChinese, 12, Font.BOLD);
6 // 正文字體風(fēng)格
7 Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
8
9 // 新建標(biāo)題,設(shè)置格式對(duì)齊方式------------------------
10 Paragraph title = new Paragraph("標(biāo)題");
11 title.setAlignment(Element.ALIGN_CENTER);
12 title.setFont(titleFont);
13 document.add(title);
14
15 // 創(chuàng)建新段落,設(shè)置格式---------------------------
16 Paragraph context = new Paragraph("測試:測試:測試:測試:測試:測試:");
17 context.setAlignment(Element.ALIGN_LEFT);
18 context.setFont(contextFont);
19 context.setSpacingBefore(3);// 段間距
20 context.setFirstLineIndent(20);// 設(shè)置第一行空的列數(shù)
21 document.add(context);
22
23 // 創(chuàng)建一個(gè)三列的表格------------------------------
24 Table table = new Table(3);
25 int width[] = { 25, 25, 50 };// 設(shè)置每列寬度比例
26 table.setWidths(width);
27 table.setWidth(90);// 占頁面寬度比例
28 table.setAlignment(Element.ALIGN_CENTER);// 居中
29 table.setAlignment(Element.ALIGN_MIDDLE);// 垂直居中
30 table.setAutoFillEmptyCells(true);// 自動(dòng)填滿
31 table.setBorderWidth(1);// 邊框?qū)挾?/span>
32
33 Cell haderCell = new Cell("表格表頭");// 設(shè)置表頭
34 haderCell.setHeader(true);
35 haderCell.setColspan(3);
36 table.addCell(haderCell);
37 table.endHeaders();
38 Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
39 Cell cell = new Cell(new Paragraph("這是一個(gè)3*3測試表格數(shù)據(jù)", fontChinese));
40 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
41 table.addCell(cell);
42 table.addCell(new Cell("#1"));
43 table.addCell(new Cell("#2"));
44 table.addCell(new Cell("#3"));
45
46 document.add(table);
47 document.close();
48
49 }
(一)生產(chǎn)文件直接存儲(chǔ)到本地
(二)生產(chǎn)文件 存到數(shù)據(jù)庫中
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();RtfWriter2.getInstance(document, outputStream);
文件信息形式轉(zhuǎn)化:ByteArrayOutputStream->InputStream->Blob
如:ByteArrayOutputStream outStream=new ByteArrayOutputStream();InputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
Blob fileObject = Hibernate.createBlob(inStream);
詳見博文"?文檔上傳(通過網(wǎng)頁上傳到服務(wù)器)..."
(三)生產(chǎn)文件 供下載
OutputStream outputStream;RtfWriter2.getInstance(document, outputStream);
詳見博文"?文件下載(從數(shù)據(jù)庫下載到本地)…"
轉(zhuǎn)載于:https://www.cnblogs.com/windlaughing/archive/2012/03/24/2415623.html
總結(jié)
以上是生活随笔為你收集整理的itext库产生word文档示例(.doc)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用文件头判断文件类型
- 下一篇: Wpf拖动按钮实现(二)