- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Font;
- import com.lowagie.text.HeaderFooter;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.Phrase;
- import com.lowagie.text.Rectangle;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.rtf.RtfWriter2;
- /**
- * @blog http://reymont.iteye.com/
- * @author reymont.li
- * @version create time:2011-7-21 下午04:02:59
- */
- public class Rules {
@RequestMapping("downLoadByWord")
public ModelAndView downLoadByWord(HttpServletRequest request,
HttpServletResponse resp, @RequestParam String rowId) {
String content = "";
try {
EmailXssResult emailXssResult = emailXssResultService
.getByRowId(rowId);
byte[] bytes = emailXssResult.getContent().getBytes();
content = new String(Base64.decodeBase64(bytes));
resp.setContentType("application/msword;charset=UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename="
+ rowId + ".doc");
byte [] byteContent = createDocContext(content).toByteArray();
ServletOutputStream os = resp.getOutputStream();
os.write(byteContent, 0, byteContent.length);
os.flush();
os.close();
} catch (Exception e) {
}
return null;
}
- public static void main(String[] args) throws DocumentException, IOException {
- Document document = new Document(PageSize.A4);
- RtfWriter2.getInstance(document, new FileOutputStream("e:/1.doc"));
- document.open();
- // 新增頁首
- HeaderFooter header = new HeaderFooter(new Phrase("header"), false);
- header.setAlignment(Rectangle.ALIGN_CENTER);
- document.setHeader(header);
- // 新增頁尾
- HeaderFooter footer = new HeaderFooter(new Phrase("footer"), false);
- footer.setAlignment(Rectangle.ALIGN_CENTER);
- document.setFooter(footer);
- // 設定中文字型
- BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
- "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
- Paragraph context = new Paragraph("");
- context.setFont(contextFont);
- //Image png = Image.getInstance("D:/busy.gif");
- //png.setAbsolutePosition(0, 0);
- //png.setAlignment(Image.TEXTWRAP);
- context.add("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
- //context.add(new Phrase(new Chunk(png, 0, 0, true)));
- context.add("bbbbbbbbbbbbbbbbbbbbbbbbbbb");
- //context.add(new Phrase(new Chunk(png, 0, 0, true)));
- context.add("ccccccccccccccccccccccccccc");
- document.add(context);
- document.close();
- }
- }
################################################################################
public void createDocContext(String file) throws DocumentException,
IOException {
// 設定紙張大小
Document document = new Document(PageSize.A4);
// 建立一個書寫器(Writer)與document物件關聯,通過書寫器(Writer)可以將文件寫入到磁碟中
RtfWriter2.getInstance(document, new FileOutputStream(file));
document.open();
// 設定中文字型
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 標題字型風格
Font titleFont = new Font(bfChinese, 12, Font.BOLD);
// 正文字型風格
Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
Paragraph title = new Paragraph("標題");
// 設定標題格式對齊方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
String contextString = "iText是一個能夠快速產生PDF檔案的java類庫。"
+ " \n"// 換行
+ "iText的java類對於那些要產生包含文字,"
+ "表格,圖形的只讀文件是很有用的。它的類庫尤其與java Servlet有很好的給合。"
+ "使用iText與PDF能夠使你正確的控制Servlet的輸出。";
Paragraph context = new Paragraph(contextString);
// 正文格式左對齊
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
// 離上一段落(標題)空的行數
context.setSpacingBefore(5);
// 設定第一行空的列數
context.setFirstLineIndent(20);
document.add(context);
//利用類FontFactory結合Font和Color可以設定各種各樣字型樣式
/**
* Font.UNDERLINE 下劃線,Font.BOLD 粗體
*/
Paragraph underline = new Paragraph("下劃線的實現", FontFactory.getFont(
FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE,
new Color(0, 0, 255)));
document.add(underline);
// 設定 Table 表格
Table aTable = new Table(3);
int width[] = {25,25,50};
aTable.setWidths(width);//設定每列所佔比例
aTable.setWidth(90); // 佔頁面寬度 90%
aTable.setAlignment(Element.ALIGN_CENTER);//居中顯示
aTable.setAlignment(Element.ALIGN_MIDDLE);//縱向居中顯示
aTable.setAutoFillEmptyCells(true); //自動填滿
aTable.setBorderWidth(1); //邊框寬度
aTable.setBorderColor(new Color(0, 125, 255)); //邊框顏色
aTable.setPadding(0);//襯距,看效果就知道什麼意思了
aTable.setSpacing(0);//即單元格之間的間距
aTable.setBorder(2);//邊框
//設定表頭
/**
* cell.setHeader(true);是將該單元格作為表頭資訊顯示;
* cell.setColspan(3);指定了該單元格佔3列;
* 為表格新增表頭資訊時,要注意的是一旦表頭資訊新增完了之後, \
* 必須呼叫 endHeaders()方法,否則當表格跨頁後,表頭資訊不會再顯示
*/
Cell haderCell = new Cell("表格表頭");
haderCell.setHeader(true);
haderCell.setColspan(3);
aTable.addCell(haderCell);
aTable.endHeaders();
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 資料", fontChinese ));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBorderColor(new Color(255, 0, 0));
cell.setRowspan(2);
aTable.addCell(cell);
aTable.addCell(new Cell("#1"));
aTable.addCell(new Cell("#2"));
aTable.addCell(new Cell("#3"));
aTable.addCell(new Cell("#4"));
Cell cell3 = new Cell(new Phrase("一行三列資料", fontChinese ));
cell3.setColspan(3);
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell3);
document.add(aTable);
document.add(new Paragraph("\n"));
//新增圖片
// Image img=Image.getInstance("http://127.0.0.1:8080/testSystem/images/1_r1_c1.png");
// img.setAbsolutePosition(0, 0);
// img.setAlignment(Image.RIGHT);//設定圖片顯示位置
// img.scaleAbsolute(12,35);//直接設定顯示尺寸
// img.scalePercent(50);//表示顯示的大小為原尺寸的50%
// img.scalePercent(25, 12);//影像高寬的顯示比例
// img.setRotation(30);//影像旋轉一定角度
// document.add(img);
document.close();
}
public static void main(String[] args){
WordTools b=new WordTools();
try {
b.createDocContext("d:/demo.doc");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}