import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class testWord{
public void createWord() throws DocumentException, IOException{
String excelPath = "e:/word.doc"; //word路徑設置
Document document =new Document(PageSize.A4);//設置導出大小
RtfWriter2.getInstance(document, new FileOutputStream(excelPath));
document.open();
//頁眉樣式設置
BaseFont bfChinese = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font titleFont = new Font(bfChinese);
Paragraph title = new Paragraph("智慧城市",titleFont);
title.font().setColor(Color.red);
title.font().setSize(48);
//設置標題格式對齊方式
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
//內容設置
String contextString = "智慧城市,是基於物聯網、雲計算、人工智能、數據挖掘等技術而形成的,一種新型信息化的城市形態,它被認爲是繼工業化、電氣化和信息化之後的“第四次浪潮。"
+ " \n"// 換行
+ "它讓城市越來越聰明,數字醫療、智能交通等電影中的情景都有望變成現實。"
+ "近日,中國有關部門公佈了首批90個國家智慧城市試點名單。";
Font contextFont = new Font(bfChinese);
Paragraph context = new Paragraph(contextString,contextFont);
context.setAlignment(Element.ALIGN_LEFT);
//離上一段落(標題)空的行數
context.setSpacingBefore(5);
// 設置第一行空的列數
context.setFirstLineIndent(20);
context.font().setColor(Color.black);//字體顏色
context.font().setSize(10); //字體大小
document.add(context);
// 設置 Table 表格
Table aTable = new Table(5);//5----代表表格列數
aTable.setAlignment(Element.ALIGN_CENTER);//居中顯示
aTable.setAlignment(Element.ALIGN_MIDDLE);//縱向居中顯示
aTable.setAutoFillEmptyCells(true); //自動填滿
aTable.setBorderWidth(1); //邊框寬度
aTable.setBorderColor(new Color(50, 125, 255)); //邊框顏色
aTable.setPadding(0);//單元格高度
aTable.setSpacing(0);//即單元格之間的間距
aTable.setBorder(2);//邊框
//設置表格行頭
Font font = new Font(bfChinese, 18, Font.NORMAL, Color.red);
Cell cell = new Cell(new Phrase("供應商",font));
Cell cella = new Cell(new Phrase("省",font));
Cell cellb = new Cell(new Phrase("市",font));
Cell cellc = new Cell(new Phrase("重量範圍",font));
Cell celld = new Cell(new Phrase("價格範圍",font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//字體居中
cella.setHorizontalAlignment(Element.ALIGN_CENTER);//字體居中
cellb.setHorizontalAlignment(Element.ALIGN_CENTER);//字體居中
cellc.setHorizontalAlignment(Element.ALIGN_CENTER);//字體居中
celld.setHorizontalAlignment(Element.ALIGN_CENTER);//字體居中
//cell.setHeader(true);
//cell.setColspan(2);
aTable.addCell(cell, 0, 0);
aTable.addCell(cella,0, 1);
aTable.addCell(cellb, 0, 2);
aTable.addCell(cellc, 0, 3);
aTable.addCell(celld, 0, 4);
//表格內容,可從數據庫取數據導出
for (int i = 1; i < 3; i++) {
aTable.addCell(new Cell("順風"), i, 0);
aTable.addCell(new Cell("浙江"),i, 1);
aTable.addCell(new Cell("杭州"), i, 2);
aTable.addCell(new Cell("20".toString()), i, 3);
aTable.addCell(new Cell("200".toString()), i, 4);
}
document.add(aTable);
document.add(new Paragraph("\n"));
Image img = Image.getInstance("d:\\1.jpg");
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.RIGHT);// 設置圖片顯示位置
img.scaleAbsolute(460, 460);// 直接設定顯示尺寸
// img.scalePercent(50);//表示顯示的大小爲原尺寸的50%
// img.scalePercent(25, 12);//圖像高寬的顯示比例
// img.setRotation(30);//圖像旋轉一定角度
document.add(img);
document.close();
}
public static void main(String[] args){
testWord b=new testWord();
try {
b.createWord();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}