1.请教iText生成word文档的小问题
使用iText生成word,struts2框架,使用iText-2.1.7.jar和iText-rtf-2.1.7.jar 方法如下:import java.io.*;import java.awt.Color;import com.lowagie.text.*;import com.lowagie.text.rtf.RtfWriter2;import com.lowagie.text.rtf.headerfooter.*;import com.lowagie.text.rtf.field.*;import com.lowagie.text.rtf.table.*;import com.lowagie.text.rtf.style.RtfFont;public class RtfClass { public static String rtf() { com.lowagie.text.Document document = new com.lowagie.text.Document(); try {System.out.println("try1"); RtfWriter2.getInstance(document, newFileOutputStream("C:\rhDocs\testRTFdocument.rtf")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } document.open(); try {System.out.println("try2"); document.add(new Paragraph("Hello World!")); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } document.close(); return "ok"; }}我用这个生成绝对不会有问题。
2.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(); } } 看看这个有没有帮助.。
3.java itext 导出word 设置字符边框
亲,这个是我给你写的设置边框和背景色的例子,请参考package org.kodejava.example.itextpdf;import com.itextpdf.text.BaseColor;import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Phrase;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class TableCellBorderColor { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("TableCellBorder.doc")); document.open(); PdfPTable table = new PdfPTable(3); PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1")); cell1.setUseBorderPadding(true); // // Setting cell's border width and color // cell1.setBorderWidth(5f); cell1.setBorderColor(BaseColor.BLUE); table.addCell(cell1); PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2")); cell2.setUseBorderPadding(true); // // Setting cell's background color // cell2.setBackgroundColor(BaseColor.GRAY); // // Setting cell's individual border color // cell2.setBorderWidthTop(1f); cell2.setBorderColorTop(BaseColor.RED); cell2.setBorderColorRight(BaseColor.GREEN); cell2.setBorderColorBottom(BaseColor.BLUE); cell2.setBorderColorLeft(BaseColor.BLACK); table.addCell(cell2); PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3")); cell3.setUseBorderPadding(true); // // Setting cell's individual border width // cell3.setBorderWidthTop(2f); cell3.setBorderWidthRight(1f); cell3.setBorderWidthBottom(2f); cell3.setBorderWidthLeft(1f); table.addCell(cell3); table.completeRow(); document.add(table); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } finally { document.close(); } }}希望可以帮助到你,记得采纳哦。
4.java itext 创建pdf,直接返回输出流做成下载,怎么做
一、iText介绍 iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。
通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,在 - download 网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。
二、建立第一个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, int marginRight, int marginTop, int marginBottom); 构建函数的参数pageSize是文档页面的大小,对于第一个构建函数,页面的大小为A4,同Document(PageSize.A4)的效果一样;对于第三个构建函数,参数marginLeft、marginRight、marginTop、marginBottom分别为左、右、上、下的页边距。 通过参数pageSize可以设定页面大小、面背景色、以及页面横向/纵向等属性。
iText定义了A0-A10、AL、LETTER、HALFLETTER、_11x17、LEDGER、NOTE、B0-B5、ARCH_A-ARCH_E、FLSA 和FLSE等纸张类型,也可以通过Rectangle pageSize = new Rectangle(144, 720);自定义纸张。通过Rectangle方法rotate()可以将页面设置成横向。
书写器(Writer)对象 一旦文档(document)对象建立好之后,需要建立一个或多个书写器(Writer)对象与之关联。通过书写器(Writer)对象可以将具体文档存盘成需要的格式,如com.lowagie.text.PDF.PDFWriter可以将文档存成PDF文件, com.lowagie.text.html.HtmlWriter可以将文档存成html文件。
设定文档属性 在文档打开之前,可以设定文档的标题、主题、作者、关键字、装订方式、创建者、生产者、创建日期等属性,调用的方法分别是: public boolean addTitle(String title) public boolean addSubject(String subject) public boolean addKeywords(String keywords) public boolean addAuthor(String author) public boolean addCreator(String creator) public boolean addProducer() public boolean addCreationDate() public boolean addHeader(String name, String content) 其中方法addHeader对于PDF文档无效,addHeader仅对html文档有效,用于添加文档的头信息。 当新的页面产生之前,可以设定页面的大小、书签、脚注(HeaderFooter)等信息,调用的方法是: public boolean setPageSize(Rectangle pageSize) public boolean add(Watermark watermark) public void removeWatermark() public void setHeader(HeaderFooter header) public void resetHeader() public void setFooter(HeaderFooter footer) public void resetFooter() public void resetPageCount() public void setPageCount(int pageN) 如果要设定第一页的页面属性,这些方法必须在文档打开之前调用。
对于PDF文档,iText还提供了文档的显示属性,通过调用书写器的setViewerPreferences方法可以控制文档打开时Acrobat Reader的显示属性,如是否单页显示、是否全屏显示、是否隐藏状态条等属性。 另外,iText也提供了对PDF文件的安全保护,通过书写器(Writer)的setEncryption方法,可以设定文档的用户口令、只读、可打印等属性。
添加文档内容 所有向文档添加的内容都是以对象为单位的,如Phrase、Paragraph、Table、Graphic对象等。比较常用的是段落(Paragraph)对象,用于向文档中添加一段文字。
三、文本处理 iText中用文本块(Chunk)、短语(Phrase)和段落(paragraph)处理文本。 文本块(Chunk)是处理文本的最小单位,有一串带格式(包括字体、颜色、大小)的字符串组成。
如以下代码就是产生一个字体为HELVETICA、大小为10、带下划线的字符串: Chunk chunk1 = new Chunk("This text is underlined", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE)); 短语(Phrase)由一个或多个文本块(Chunk)组成,短语(Phrase)也可以设定字体,但对于其中以设定过字体的文本块 (Chunk)无效。通过短语(Phrase)成员函数add可以将一个文本块(Chunk)加到短语(Phrase)中,如:phrase6.add(chunk); 段落(paragraph)由一个或多个文本块(Chunk)或短语(Phrase)组成,相当于WORD文档中的段落概念,同样可以设定段落的字体大小、颜色等属性。
另外也可以。
5.可以把你用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包,我整理一份后会发给你了。
6.怎样用iText读取pdf文件
用iText读取pdf文件举例:(1)在Eclipse中新建一个Java工程。
(2)下载相应的iText-5.0.2.jar并放到对应的lib目录下。在工程中创建包并创建测试类,该类包含一个inspect方法用于从一个PDF中获取文本,它接受两个参数,分别是PDF文件路径和输出流,指定要提取的PDF文件的路径和读取PDF所用的输出流,比如:PDF路径为E://text.pdf。
然后调用iText提供的PdfReader类和PdfTextExtractor类,将PDF格式的文本提取出来并写入txt文件中。部分代码如下:import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintWriter;import com.itextpdf.text.DocumentException;。
转载请注明出处51数据库 » itextpdf输出成word