Java產生rtf格式文檔

來源:互聯網
上載者:User

package com.newbee.brooder;<br />import java.io.FileNotFoundException;<br />import java.io.FileOutputStream;<br />import java.io.IOException;<br />import java.text.SimpleDateFormat;<br />import java.util.Date;<br />import org.apache.log4j.Logger;<br />import com.lowagie.text.Cell;<br />import com.lowagie.text.Chunk;<br />import com.lowagie.text.Document;<br />import com.lowagie.text.DocumentException;<br />import com.lowagie.text.Element;<br />import com.lowagie.text.Font;<br />import com.lowagie.text.Image;<br />import com.lowagie.text.PageSize;<br />import com.lowagie.text.Paragraph;<br />import com.lowagie.text.Table;<br />import com.lowagie.text.pdf.BaseFont;<br />import com.lowagie.text.rtf.RtfWriter2;<br />import com.lowagie.text.rtf.field.RtfPageNumber;<br />import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;<br />import com.lowagie.text.rtf.table.RtfCell;<br />import com.newbee.util.NumberToChinese;<br />/**<br /> * @author new<br /> */<br />public class RtfMain {</p><p>private static final String FILE_NAME = "/home/guo/itext_rtf.rtf";<br />/**<br /> * @param args<br /> */<br />public static void main(String[] args) {</p><p>try {</p><p>RtfMain rtfMain = new RtfMain();<br />rtfMain.createRTFContext(FILE_NAME);<br />} catch (FileNotFoundException e) {<br />Logger.getLogger(RtfMain.class).debug(e);<br />} catch (DocumentException e) {<br />Logger.getLogger(RtfMain.class).debug(e);<br />} catch (IOException e) {<br />Logger.getLogger(RtfMain.class).debug(e);<br />}<br />}<br />public void createRTFContext(String path) throws DocumentException, IOException {</p><p>Document document = new Document(PageSize.A4);<br />RtfWriter2.getInstance(document, new FileOutputStream(path));<br />document.open();</p><p>// 設定中文字型<br />BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);<br />// 標題字型風格<br />Font titleFont = new Font(bfChinese, 12, Font.BOLD);<br />// 小標題字型風格<br />Font subTitleFont = new Font(bfChinese, 11, Font.BOLD);<br />// 本文字型風格<br />Font contextFont = new Font(bfChinese, 10, Font.NORMAL);<br />// 頁首頁尾字型風格<br />Font headerFooterFont = new Font(bfChinese, 10, Font.BOLD);</p><p>// header<br />Table header = new Table(2);<br />header.setBorder(0);<br />header.setWidth(100);</p><p>Paragraph address = new Paragraph("支援人員: xxxx/n" + "聯絡電話: +086 1358190xxxx");<br />address.setFont(headerFooterFont);<br />Cell cell01 = new Cell(address);<br />cell01.setBorder(0);<br />header.addCell(cell01);</p><p>Paragraph date = new Paragraph("產生日期: "+new SimpleDateFormat("yyyy-MM-dd").format(new Date()));<br />date.setAlignment(Paragraph.ALIGN_RIGHT);<br />date.setFont(headerFooterFont);<br />cell01 = new Cell(date);<br />cell01.setBorder(0);<br />header.addCell(cell01);<br />document.setHeader(new RtfHeaderFooter(header));</p><p>// footer<br />Table footer = new Table(2);<br />footer.setBorder(0);<br />footer.setWidth(100);</p><p>Paragraph company = new Paragraph("(c) New Bee");<br />company.setFont(headerFooterFont);<br />Cell cell02 = new Cell(company);<br />cell02.setBorder(0);<br />footer.addCell(cell02);</p><p>Paragraph pageNumber = new Paragraph("第 ");<br />pageNumber.add(new RtfPageNumber());<br />pageNumber.add(new Chunk(" 頁"));<br />pageNumber.setAlignment(Paragraph.ALIGN_RIGHT);<br />pageNumber.setFont(headerFooterFont);<br />cell02 = new Cell(pageNumber);<br />cell02.setBorder(0);<br />footer.addCell(cell02);</p><p>document.setFooter(new RtfHeaderFooter(footer));<br />// Title<br />Paragraph title = new Paragraph("標題");<br />title.setAlignment(Element.ALIGN_CENTER);<br />title.setFont(titleFont);<br />document.add(title);<br />// Content<br />for (int i = 0; i < 5; i++) {</p><p>Paragraph subTitle = new Paragraph(NumberToChinese.getChineseNumber((i+1))+"、標題"+(i+1));<br />subTitle.setFont(subTitleFont);<br />subTitle.setSpacingBefore(10);// 本段與上一段之間的空行<br />subTitle.setFirstLineIndent(0);// 本段行首縮排<br />document.add(subTitle);</p><p>for (int j = 0; j < 3; j++) {</p><p>String contextString = (j+1)+"."+"iText是一個能夠快速產生PDF檔案的java類庫。iText的java類對於那些要產生包含文本,表格,圖形的唯讀文檔是很有用的。";<br />Paragraph context = new Paragraph(contextString);<br />context.setAlignment(Element.ALIGN_LEFT);<br />context.setFont(contextFont);<br />context.setSpacingBefore(10);<br />context.setFirstLineIndent(0);<br />document.add(context);</p><p>for (short k = 0; k < 4; k++) {</p><p>char enString = 'A';<br />String answerString = ((char)(enString+k))+"."+"表格,圖形的唯讀文檔是很有用的。";<br />Paragraph answer = new Paragraph(answerString);<br />answer.setAlignment(Element.ALIGN_LEFT);<br />answer.setFont(contextFont);<br />answer.setSpacingBefore(10);<br />answer.setFirstLineIndent(20);<br />document.add(answer);</p><p>}<br />}<br />}</p><p>// //在表格末尾添加圖片<br />Image png=Image.getInstance(ClassLoader.getSystemResource("com/newbee/brooder/RtfMain.png"));</p><p>Table table = new Table(2);<br />table.setBorder(0);<br />table.addCell(new RtfCell(png));<br />table.addCell(new RtfCell(png));<br />table.addCell(new RtfCell(png));<br />table.addCell(new RtfCell());</p><p>document.add(table);<br />document.close();<br />}<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.