一、引入mavenmaven
<dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext-rtf</artifactId> <version>2.1.7</version> </dependency>
二、大體流程字體
//建立一條文本對象(A4紙張) Document document = new Document(PageSize.A4); //建立一個DOC文檔(根據指定的路徑) RtfWriter2.getInstance(document, new FileOutputStream(file_url)); //打開文本對象 document.open(); //添加一個簡單的段落 document.add(new Paragraph("Hello World!")); //結束編寫 document.close();
三、字體url
//設置基礎字體 -- 宋體 BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //設置字體 (含有大小、顏色……) Font titleFont = new Font(bfChinese, 14.0F, 1); //字體:宋體 大小:14.0F 加粗 Font contextFont = new Font(bfChinese, 14.0F, 0); //字體:宋體 大小:14.0F 不加粗 Font contextFont_red = new Font(bfChinese, 14.0F, 0, Color.red); //字體:宋體 大小:14.0F 不加粗 字體顏色:紅色 Font contextFont_black = new Font(bfChinese, 14.0F, 0, Color.black);//字體:宋體 大小:14.0F 不加粗 字體顏色:黑色 Font headerFooterFont = new Font(bfChinese, 9.0F, 0);//字體:宋體 大小:9.0F 不加粗
四、設置頭 和 頁碼數spa
全局變量code
private static String TECH_INFO = "這裏是頂部"; //頭部標語 private static String FOOT_INFO = "這裏是底部"; //底部標語
頭部orm
Table header = new Table(2); header.setBorder(0); header.setWidth(100.0F); Paragraph address = new Paragraph(TECH_INFO); address.setFont(headerFooterFont); Cell cell01 = new Cell(address); cell01.setBorder(0); header.addCell(cell01); Paragraph date = new Paragraph("生成日期: " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())); date.setAlignment(2); date.setFont(headerFooterFont); cell01 = new Cell(date); cell01.setBorder(0); header.addCell(cell01); document.setHeader(new RtfHeaderFooter(header));
底部(頁碼數)對象
Table footer = new Table(2); footer.setBorder(0); footer.setWidth(100.0F); Paragraph company = new Paragraph(FOOT_INFO); company.setFont(headerFooterFont); Cell cell02 = new Cell(company); cell02.setBorder(0); footer.addCell(cell02); Paragraph pageNumber = new Paragraph("第 "); pageNumber.add(new RtfPageNumber()); pageNumber.add(new Chunk(" 頁")); pageNumber.setAlignment(2); pageNumber.setFont(headerFooterFont); cell02 = new Cell(pageNumber); cell02.setBorder(0); footer.addCell(cell02); document.setFooter(new RtfHeaderFooter(footer));
五、表格blog
Table table1 = new Table(4);//建立表格 int width[] = {18,32,18,32};//設置每列寬度比例 table1.setWidths(width);//設置每列寬度比例 table1.setWidth(95);//佔頁面寬度比例 //第一行 第一個 Cell cell11 = new Cell(new Paragraph("11",contextFont)); cell11.setVerticalAlignment(Element.ALIGN_TOP); //垂直 居上 cell11.setHorizontalAlignment(Element.ALIGN_RIGHT); //水平 居右 cell11.setBorder(0); //第一行 第二個 Cell cell12 = new Cell("12"); cell12.setVerticalAlignment(Element.ALIGN_BOTTOM); //垂直 居下 cell12.setHorizontalAlignment(Element.ALIGN_LEFT); //水平 居左 cell12.setBorder(0); //第一行 第三個 Cell cell13 = new Cell(new Paragraph("13",contextFont)); cell13.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直 居中 cell13.setHorizontalAlignment(Element.ALIGN_RIGHT); //水平 居右 cell13.setBorder(0); //第一行 第四個 Cell cell14 = new Cell(new Paragraph("14",contextFont)); cell14.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直 居中 cell14.setHorizontalAlignment(Element.ALIGN_LEFT); //水平 居左 cell14.setBorder(0); table1.addCell(cell11); table1.addCell(cell12); table1.addCell(cell13); table1.addCell(cell14); document.add(table1);
六、插入圖片圖片
//圖片路徑 String URL = "http://www.baidu.com/img/bd_logo1.png"; //建立圖片 Image img = null; try { img = Image.getInstance(URL); img.scalePercent(10); } catch (BadElementException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //內容 Paragraph context = new Paragraph(); context.setAlignment(1); context.setFont(contextFont); if(img == null){ context.add("沒有圖片"); }else{ context.add(img); } document.add(context);