可以把你用IText生成word文档例子发给我吗?现在我需要这样的东西!
你好,很意外的收到你的求助,其实导出word,很少有人使用IText,不过我是一个例外,呵呵。
对于IText,我也仅仅是自学认识一些,并试着使用它,在导出的上,与大多数网络上的做法不同,先告诉你我的做法的基本思路,然后我再通过邮箱传给你。1)在我搭建的工程中,一般除了主工程以外,还会有一个配置文件夹,用于存放配置文件.2)先将表格以XML文件的形式保存在配置文件中,主要模仿html中的table,保留table、tr、td等属性。
3)在项目启动的时候,加入监听,去加载xml文件。当你在页面需要点击按钮需要使用的时候,在后台的action(我的项目框架使用的是structs+jdbc)中解析xml,并将数据封装到tablebean中4)最后在实现类中,解析tablebean,使用IText内部的合并行列等方法,对xml表格进行完全解析。
这样开发有一个好处,也有一个不好的地方,好处是你想要的开发效果,你都可以根据自己的需求对后台进行改造,不像有些依赖报表引擎等,不能达到自己满意的效果。坏处是,由于可借鉴资料基本没有,你能够依赖的,只有自己,所以在前期的开发效率上,会比较耗时,特别是项目紧急的时候,经不起失败的考验。
例子和jar包,我整理一份后会发给你了。
java itext 生成word 如何控制word 横向和分栏,可以实现么
要加一些文本到一个矩形框的内部,希望文本到达右边界时自动换行。超出矩形部分将不显示,可以通过ColumnText类实现。
举个例子:
为显示一个指定的短句在坐标(100, 300)和(200,500)间的矩形内居中,使用下面的代码:
PdfContentByte cb =writer.DirectContent;
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(phrase,60, 300, 100, 500, 15, Element.ALIGN_CENTER);
ct.go();
itext导出Word的问题,能解答的有高分
1: 可以设置字体带下划线,也就是 new Phrase里面带上设置字体下划线 2: 用表格实现,把表格的下边框线显示出来 3: 直接画线也可以 package test; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.rtf.RtfWriter2; import java.awt.Color; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import javax.swing.JOptionPane; /** * * @author julycn */ public class RTFCreate { /** * Creates a new instance of RTFCreate */ public RTFCreate() { } public static void main(String[] args){ RTFCreate rtfCreate=new RTFCreate(); try { rtfCreate.createRTF(); JOptionPane.showMessageDialog(null,"表格已经成功创建"); } catch (MalformedURLException ex) { JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!"); ex.printStackTrace(); } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!"); ex.printStackTrace(); } catch (IOException ex) { JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!"); ex.printStackTrace(); } catch (DocumentException ex) { JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!"); ex.printStackTrace(); } } public void createRTF() throws FileNotFoundException, DocumentException, MalformedURLException, IOException{ //创建word文档 Document document=new Document(PageSize.A4); //输入word文档 RtfWriter2.getInstance(document,new FileOutputStream("d:\\word.rtf")); document.open(); //中文字体 BaseFont bfChinese=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); Font fontChinese=new Font(bfChinese,12,Font.HELVETICA); //创建有3列的表格 Table table=new Table(3); document.add(new Paragraph("生成rft文档!",fontChinese)); table.setBorderWidth(1); table.setBorderColor(new Color(0,0,255)); table.setPadding(5); table.setSpacing(5); //添加表头元素 Cell cell=new Cell("header"); cell.setHeader(true); cell.setColspan(3); table.addCell(cell); table.endHeaders();//表头结束 //表格主体 cell=new Cell("Example cell with colspan 1 and rowspan 2"); cell.setRowspan(2); cell.setBorderColor(new Color(255,0,0)); table.addCell(cell); table.addCell("1.1"); table.addCell("2.1"); table.addCell("1.2"); table.addCell("2.2"); table.addCell(new Paragraph("测试1",fontChinese)); table.addCell("big cell"); cell.setRowspan(2); cell.setColspan(2); table.addCell(cell); table.addCell(new Paragraph("测试2",fontChinese)); document.add(table); //在表格末尾添加图片 Image png=Image.getInstance("d:\\duck.jpg"); document.add(png); document.close(); } } 看看这个有没有帮助.。
itext html转word 字体 linux
一、iText介绍 iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
二、建立第一个PDF文档 用iText生成PDF文档需要5个步骤:
①建立com.lowagie.text.Document对象的实例。
Document document = new Document();
②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));
③打开文档。
document.open();
④向文档中添加内容。
document.add(new Paragraph("Hello World"));
⑤关闭文档。
document.close();
通过上面的5个步骤,就能产生一个Helloworld.PDF的文件,文件内容为"Hello World"。
建立com.lowagie.text.Document对象的实例
com.lowagie.text.Document对象的构建函数有三个,分别是:
public Document();
public Document(Rectangle pageSize);
public Document(Rectangle pageSize,
int marginLeft,
转载请注明出处51数据库 » itext预览word