效果:
代码:
package com.aaa.bbb; import java.awt.Color; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import javax.servlet.ServletOutputStream; import com.lowagie.text.BadElementException; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; import com.lowagie.text.Table; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.rtf.RtfWriter2; import com.lowagie.text.rtf.field.RtfPageNumber; import com.lowagie.text.rtf.field.RtfTotalPageNumber; import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter; import com.lowagie.text.rtf.style.RtfFont; import com.lowagie.text.rtf.style.RtfParagraphStyle; import com.opensymphony.xwork2.ActionSupport; import com.aaa.bbb.BaseAction; /** * itext 测试 */ public class WordAction extends BaseAction { RtfFont tableFont = new RtfFont("宋体_GB2312", 14, Font.COURIER, Color.BLACK); public void test() { try{ RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1; rtfGsBt1.setAlignment(Element.ALIGN_CENTER); rtfGsBt1.setStyle(Font.BOLD); rtfGsBt1.setSize(14); /* 设置标题2格式 */ RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2; rtfGsBt2.setAlignment(Element.ALIGN_LEFT); rtfGsBt2.setStyle(Font.NORMAL); rtfGsBt2.setSize(12); Document document = new Document(PageSize.A4.rotate()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); RtfWriter2.getInstance(document, baos); document.open(); Paragraph paragraph1 = new Paragraph("段落1"); paragraph1.setAlignment(Element.ALIGN_CENTER); paragraph1.setFont(rtfGsBt1); RtfFont titleFont = new RtfFont("宋体_GB2312", 16, Font.BOLD, Color.BLACK); // paragraph1.setFont(titleFont); document.add(paragraph1); paragraph1 = new Paragraph("1:图片"); paragraph1.setFont(rtfGsBt2);// 2级目录 document.add(paragraph1); Paragraph parafooter = new Paragraph(); Font footerFont = new RtfFont("宋体_GB2312", 12, Font.BOLD, Color.BLACK); parafooter.setFont(new Font(footerFont)); parafooter.add(new Phrase("第")); parafooter.add(new RtfPageNumber()); parafooter.add(new Phrase("页 共")); parafooter.add(new RtfTotalPageNumber()); parafooter.add(new Phrase("页")); HeaderFooter footer = new RtfHeaderFooter(parafooter); footer.setAlignment(Element.ALIGN_CENTER); footer.setBorder(Rectangle.NO_BORDER); document.setFooter(footer); int rows = 6; int columns = 5;// 列的个数 Table table = new Table(columns, rows); int[] widths = new int[columns]; for (int i = 0; i < widths.length; i++) { widths[i] = columns; } table.setWidths(widths);// 设置每列所占比例 // table.setWidth(100); // 占页面宽度100% table.setAlignment(Element.ALIGN_CENTER);// 居中显示 table.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示 table.setAutoFillEmptyCells(true); // 自动填满 table.setPadding(2);// 单元格内间距,即文字与单元格边的距离 table.setSpacing(0);// 单元格之间的间距 // 表格操作开始 addCell(table, "1,1"); addCell(table, "1,2"); addCell(table, "1,3"); Cell cell = new Cell(new Phrase("1,4", tableFont)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setRowspan(1); cell.setColspan(2);// 2 列 cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(Color.blue); table.addCell(cell); cell = new Cell(new Phrase("1,5", tableFont)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setRowspan(1); cell.setColspan(2);// 2 列 cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(Color.pink); table.addCell(cell); cell = new Cell(new Phrase("1,6", tableFont)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setRowspan(1); cell.setColspan(3);// 3 列 cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(Color.yellow); table.addCell(cell); cell = new Cell(new Phrase("1,7占4行", tableFont)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setRowspan(4);// 4行 cell.setColspan(1);// 1 列 cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(Color.red); table.addCell(cell); cell = new Cell(new Phrase("1,8占3列3行", tableFont)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setRowspan(3);// 4行 cell.setColspan(3);// 1 列 cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(Color.GREEN); table.addCell(cell); cell = new Cell(); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setRowspan(4);// 4行 cell.setColspan(1);// 1 列 cell.setBorderColor(new Color(0, 0, 0)); cell.setBackgroundColor(Color.magenta); Image img = Image.getInstance("D:\\me.jpg"); img.scaleAbsolute(80, 68); cell.add(img); table.addCell(cell); document.add(table);// 表格 paragraph1 = new Paragraph("2:描述"); paragraph1.setFont(rtfGsBt2);// 2级目录 paragraph1.add("图片显示表格的横向合并与纵向合并,并在合并的单元格插入图片。"); document.add(paragraph1); paragraph1 = new Paragraph("段落2"); paragraph1.setFont(rtfGsBt1);// 2级目录 document.add(paragraph1); paragraph1 = new Paragraph("1:Coding"); paragraph1.setFont(rtfGsBt2);// 2级目录 paragraph1.add("Coder 开始写代码了!"); document.add(paragraph1); paragraph1 = new Paragraph("2:吃饭"); paragraph1.setFont(rtfGsBt2);// 2级目录 paragraph1.add("Coder 吃饭了。"); document.add(paragraph1); paragraph1 = new Paragraph("3:睡觉"); paragraph1.setFont(rtfGsBt2);// 2级目录 paragraph1.add("Coder 睡觉了。"); document.add(paragraph1); document.add(new Paragraph("\n")); document.close(); response.reset(); // 清空buffer // application/msword --->2003 response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); String filePath = "itext 测试.doc"; filePath = java.net.URLEncoder.encode(filePath, "UTF-8"); response.setHeader("Content-Disposition", "attachment; filename="+ new String(filePath.getBytes("UTF-8"), "GBK"));// 定义文件名 response.setHeader("Pragma", "no-cache"); response.setHeader("Expires", " 0"); response.setContentLength(baos.size()); ServletOutputStream fOut = response.getOutputStream(); baos.writeTo(fOut); fOut.flush(); fOut.close(); }catch (Exception e) { System.out.println(e); } } public void addCell(Table table, String content) throws BadElementException { Cell cell = new Cell(new Phrase(content, tableFont)); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell.setRowspan(1); cell.setColspan(1); table.addCell(cell); } }
相关推荐
java 后端生成pdf模板合并单元格表格的案例.docx
用Itext操作word,可以在word里画表格,也可以往word里插入图片
itext 对JAVA的操作 纯JAVA 操作标准的 DOC文件
itext生成word文档,包含图片,其中图片有直接读取路径的,也有流直接转字节生成的图片
Itext 生成带图片,表格的word 文档Demo,Demo中含运行所需要的jar包
文档POI只支持往生成的中填入文本,对于图片根本就不支持。使用itext 生成rtf格式的直接保存为word
使用IText生成PDF和WORD文档 包括源码和IText所需jar包
Itext导出Word文档的例子 包含三个jar包
itextpdf包及复杂结构实现,几个例子运行一下,看看,基本上你就会了
各种合并单元格的方法,本文提供了六种合并的方法,大家可以按需选择试用自己的方法!
itext pdf word 中文 表格 图片,看了这个你就能用java生成word,pdf并插入图片,中文,表格。里面还有itext的API包括中文英文。
java 使用iText生成word文档,包中带有源码与所用jar包,生成文档格式标准。可分页,可带页眉,可生成表格、图片和文字等word元素内容,文字带字号,字体,颜色等。
itext 导出word itext 导出word
itext导出word整理
共享一下个人用java编写的生成word文件程序,通过itext生成word文件代码附itext-2.0.7.jar,iTextAsian.jar包
itext合并和拆分PDF实例,经过测试,绝对可用
java 用iText生成Word文件 java 用iText生成Word文件
本工程为MyEclipse创建的java工程,用于演示使用iText jar包如何实现对word文件的操作,这些操作是多方面的,包括对表格的创建合并等。 本工程编码方式:GBK。 下载下来解压拷贝到MyEclipse中即可运行,欢迎下载。
NULL 博文链接:https://x125858805.iteye.com/blog/2239773
使用itext到处word开箱即用,导入就可以直接运行,代码简单,可以参考。