Java生成rtf格式文档

package com.newbee.brooder; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.log4j.Logger; import com.lowagie.text.Cell; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; 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 com.lowagie.text.rtf.field.RtfPageNumber; import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter; import com.lowagie.text.rtf.table.RtfCell; import com.newbee.util.NumberToChinese; /** * @author new */ public class RtfMain { private static final String FILE_NAME = "/home/guo/itext_rtf.rtf"; /** * @param args */ public static void main(String[] args) { try { RtfMain rtfMain = new RtfMain(); rtfMain.createRTFContext(FILE_NAME); } catch (FileNotFoundException e) { Logger.getLogger(RtfMain.class).debug(e); } catch (DocumentException e) { Logger.getLogger(RtfMain.class).debug(e); } catch (IOException e) { Logger.getLogger(RtfMain.class).debug(e); } } public void createRTFContext(String path) throws DocumentException, IOException { Document document = new Document(PageSize.A4); RtfWriter2.getInstance(document, new FileOutputStream(path)); document.open(); // 设置中文字体 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 标题字体风格 Font titleFont = new Font(bfChinese, 12, Font.BOLD); // 小标题字体风格 Font subTitleFont = new Font(bfChinese, 11, Font.BOLD); // 正文字体风格 Font contextFont = new Font(bfChinese, 10, Font.NORMAL); // 页眉页脚字体风格 Font headerFooterFont = new Font(bfChinese, 10, Font.BOLD); // header Table header = new Table(2); header.setBorder(0); header.setWidth(100); Paragraph address = new Paragraph("技术支持: xxxx/n" + "联络电话: +086 1358190xxxx"); 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(Paragraph.ALIGN_RIGHT); date.setFont(headerFooterFont); cell01 = new Cell(date); cell01.setBorder(0); header.addCell(cell01); document.setHeader(new RtfHeaderFooter(header)); // footer Table footer = new Table(2); footer.setBorder(0); footer.setWidth(100); Paragraph company = new Paragraph("(c) New Bee"); 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(Paragraph.ALIGN_RIGHT); pageNumber.setFont(headerFooterFont); cell02 = new Cell(pageNumber); cell02.setBorder(0); footer.addCell(cell02); document.setFooter(new RtfHeaderFooter(footer)); // Title Paragraph title = new Paragraph("标题"); title.setAlignment(Element.ALIGN_CENTER); title.setFont(titleFont); document.add(title); // Content for (int i = 0; i < 5; i++) { Paragraph subTitle = new Paragraph(NumberToChinese.getChineseNumber((i+1))+"、标题"+(i+1)); subTitle.setFont(subTitleFont); subTitle.setSpacingBefore(10); // 本段与上一段之间的空行 subTitle.setFirstLineIndent(0); // 本段行首缩进 document.add(subTitle); for (int j = 0; j < 3; j++) { String contextString = (j+1)+"."+"iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。"; Paragraph context = new Paragraph(contextString); context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); context.setSpacingBefore(10); context.setFirstLineIndent(0); document.add(context); for (short k = 0; k < 4; k++) { char enString = 'A'; String answerString = ((char)(enString+k))+"."+"表格,图形的只读文档是很有用的。"; Paragraph answer = new Paragraph(answerString); answer.setAlignment(Element.ALIGN_LEFT); answer.setFont(contextFont); answer.setSpacingBefore(10); answer.setFirstLineIndent(20); document.add(answer); } } } // //在表格末尾添加图片 Image png=Image.getInstance(ClassLoader.getSystemResource("com/newbee/brooder/RtfMain.png")); Table table = new Table(2); table.setBorder(0); table.addCell(new RtfCell(png)); table.addCell(new RtfCell(png)); table.addCell(new RtfCell(png)); table.addCell(new RtfCell()); document.add(table); document.close(); } }

你可能感兴趣的:(Java生成rtf格式文档)