Java 实现word 中写入文字图片的解决方案

JAVA生成WORD文件的方法目前有以下两种方式:

一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案;

一种是poi但是他的excel处理很程序 word模块还局限于读取word的文本内容,写word文件就更弱项了.

用到的jar包: 
iText-2.1.5.jar
iText-rtf-2.1.4.jar
iTextAsian.jar

<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.lowagie/itext-rtf -->
    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext-rtf</artifactId>
        <version>2.1.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
    </dependency>

/**   

  * 创建word文档 步骤:    

  * 1,建立文档    

  * 2,创建一个书写器    

  * 3,打开文档    

  * 4,向文档中写入数据    

  * 5,关闭文档   

  */   

/**
    *
    * @Description: 将网页内容导出为word
    * @param @param file
    * @param @throws DocumentException
    * @param @throws IOException 设定文件
    * @return void 返回类型
    * @throws
    */
   public static String exportDoc() throws DocumentException, IOException {

       // 设置纸张大小

       Document document = new Document(PageSize.A4);

       // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
       // ByteArrayOutputStream baos = new ByteArrayOutputStream();

       File file = new File("D://report.doc");

       RtfWriter2.getInstance(document, new FileOutputStream(file));

       document.open();

       // 设置中文字体

       BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA,
                  BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

       // 标题字体风格

       Font titleFont = new Font(bfChinese, 12, Font.BOLD);

       // // 正文字体风格
       //
       Font contextFont = new Font(bfChinese, 10, Font.NORMAL);

       Paragraph title = new Paragraph("统计报告");
       //
       // 设置标题格式对齐方式

       title.setAlignment(Element.ALIGN_CENTER);

       // title.setFont(titleFont);

       document.add(title);

       String contextString = "iText是一个能够快速产生PDF文件的java类库。"

       + " \n"// 换行 + "iText的java类对于那些要产生包含文本,"

               + "表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。"

               + "使用iText与PDF能够使你正确的控制Servlet的输出。";

       Paragraph context = new Paragraph(contextString);

       // 正文格式左对齐

       context.setAlignment(Element.ALIGN_LEFT);

       // context.setFont(contextFont);

       // 离上一段落(标题)空的行数

       context.setSpacingBefore(5);

       // 设置第一行空的列数

       context.setFirstLineIndent(20);

       document.add(context);
       //
       // // 利用类FontFactory结合Font和Color可以设置各种各样字体样式
       //
       // Paragraph underline = new Paragraph("下划线的实现", FontFactory.getFont(
       // FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE,
       // new Color(0, 0, 255)));
       //
       // document.add(underline);
       //

       // // 添加图片 Image.getInstance即可以放路径又可以放二进制字节流
       //


       Image img = Image.getInstance("D:\\eclipseWorkspace\\wordTest\\01055378_0.jpg");

       img.setAbsolutePosition(0, 0);

       img.setAlignment(Image.LEFT);// 设置图片显示位置

       //
       // img.scaleAbsolute(60, 60);// 直接设定显示尺寸
       //
       // // img.scalePercent(50);//表示显示的大小为原尺寸的50%
       //
       // // img.scalePercent(25, 12);//图像高宽的显示比例
       //
       // // img.setRotation(30);//图像旋转一定角度
       //
       document.add(img);

       document.close();

       // 得到输入流
       // wordFile = new ByteArrayInputStream(baos.toByteArray());
       // baos.close();
       return "";

   }

转自:http://www.cnblogs.com/lionelwu-qianqian/archive/2013/05/31/3110581.html

posted @ 2016-07-26 11:48  风雨的叶  阅读(17568)  评论(2编辑  收藏  举报