1 public class CreateWordDemo { 2 public void createDocContext(String file) throws DocumentException,IOException { 3 // 設置紙張大小 4 Document document = new Document(PageSize.A4); 5 // 創建一個書寫器(Writer)與document對象關聯,經過書寫器(Writer)能夠將文檔寫入到磁盤中 RtfWriter2.getInstance(document, new FileOutputStream(file)); 6 document.open(); 7 // 設置中文字體 8 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 9 // 標題字體風格 10 Font titleFont = new Font(bfChinese, 12, Font.BOLD); 11 // 正文字體風格 12 Font contextFont = new Font(bfChinese, 10, Font.NORMAL); 13 Paragraph title = new Paragraph("標題"); 14 // 設置標題格式對齊方式 15 title.setAlignment(Element.ALIGN_CENTER); 16 title.setFont(titleFont); 17 document.add(title); 18 String contextString = "iText是一個可以快速產生PDF文件的java類庫。" 19 + " \n"// 換行 + "iText的java類對於那些要產生包含文本," 20 + "表格,圖形的只讀文檔是頗有用的。它的類庫尤爲與java Servlet有很好的給合。" 21 + "使用iText與PDF可以使你正確的控制Servlet的輸出。"; 22 Paragraph context = new Paragraph(contextString); 23 // 正文格式左對齊 24 context.setAlignment(Element.ALIGN_LEFT); 25 context.setFont(contextFont); 26 // 離上一段落(標題)空的行數 27 context.setSpacingBefore(5); 28 // 設置第一行空的列數 29 context.setFirstLineIndent(20); 30 document.add(context); 31 // 利用類FontFactory結合Font和Color能夠設置各類各樣字體樣式 32 33 Paragraph underline = new Paragraph("下劃線的實現", FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE, new Color(0, 0, 255))); 34 document.add(underline); 35 // 設置 Table 表格 36 Table aTable = new Table(3); 37 int width[] = { 25, 25, 50 }; 38 aTable.setWidths(width);// 設置每列所佔比例 39 aTable.setWidth(90); // 佔頁面寬度 90% 40 aTable.setAlignment(Element.ALIGN_CENTER);// 居中顯示 41 aTable.setAlignment(Element.ALIGN_MIDDLE);// 縱向居中顯示 42 aTable.setAutoFillEmptyCells(true); // 自動填滿 43 aTable.setBorderWidth(1); // 邊框寬度 44 aTable.setBorderColor(new Color(0, 125, 255)); // 邊框顏色 45 aTable.setPadding(2);// 襯距,看效果就知道什麼意思了 46 aTable.setSpacing(3);// 即單元格之間的間距 47 aTable.setBorder(2);// 邊框 // 設置表頭 48 49 Cell haderCell = new Cell("表格表頭"); 50 haderCell.setHeader(true); 51 haderCell.setColspan(3); 52 aTable.addCell(haderCell); 53 aTable.endHeaders(); 54 Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN); 55 Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據", fontChinese)); cell.setVerticalAlignment(Element.ALIGN_TOP); 56 cell.setBorderColor(new Color(255, 0, 0)); 57 cell.setRowspan(2); 58 aTable.addCell(cell); 59 aTable.addCell(new Cell("#1")); 60 aTable.addCell(new Cell("#2")); 61 aTable.addCell(new Cell("#3")); 62 aTable.addCell(new Cell("#4")); 63 Cell cell3 = new Cell(new Phrase("一行三列數據", fontChinese)); 64 cell3.setColspan(3); 65 cell3.setVerticalAlignment(Element.ALIGN_CENTER); 66 aTable.addCell(cell3); 67 document.add(aTable); 68 document.add(new Paragraph("\n")); 69 // 添加圖片 Image.getInstance便可以放路徑又能夠放二進制字節流 70 Image img = Image.getInstance("d:\\img01800.jpg"); 71 img.setAbsolutePosition(0, 0); 72 img.setAlignment(Image.RIGHT);// 設置圖片顯示位置 73 img.scaleAbsolute(60, 60);// 直接設定顯示尺寸 74 // img.scalePercent(50);//表示顯示的大小爲原尺寸的50% 75 // img.scalePercent(25, 12);//圖像高寬的顯示比例 76 // img.setRotation(30);//圖像旋轉必定角度 77 document.add(img); 78 document.close(); 79 } 80 81 public static void main(String[] args) { 82 CreateWordDemo word = new CreateWordDemo(); 83 String file = "d:/demo1.doc"; 84 try { 85 word.createDocContext(file); 86 } catch (DocumentException e) { 87 e.printStackTrace(); 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } 91 } 92 }