Itext中 根据html生成Word文件,包含图片

package cn.com.wzf;  
  
import java.io.FileOutputStream;  
import java.io.OutputStream;  
import java.io.StringReader;  
import java.util.List;  
  
import com.lowagie.text.Document;  
import com.lowagie.text.PageSize;  
import com.lowagie.text.Paragraph;  
import com.lowagie.text.html.simpleparser.HTMLWorker;  
import com.lowagie.text.html.simpleparser.StyleSheet;  
import com.lowagie.text.rtf.RtfWriter2;  
  
public class ItextCreateRTF {  
    public static void main(String[] args) throws Exception {  
        OutputStream out = new FileOutputStream("c://a.doc");  
        Document document = new Document(PageSize.A4);  
        RtfWriter2.getInstance(document, out);  
        document.open();  
        Paragraph context = new Paragraph();  
        String s = "上传的图片<img width=\"800\" height=\"600\" alt=\"\" src=\"http://localhost:8081/zhmobileexp/upload/activity/photos/Image/waterlilies.jpg\" />";  
        System.out.println(s);  
        // Image img = Image.getInstance("D:\\图片\\2.jpg");  
        // img.setAbsolutePosition(0, 0);//  
        // document.add(img);  
        StyleSheet ss = new StyleSheet();  
        List htmlList = HTMLWorker.parseToList(new StringReader(s), ss);  
        for (int i = 0; i < htmlList.size(); i++) {  
            com.lowagie.text.Element e = (com.lowagie.text.Element) htmlList  
                    .get(i);  
            context.add(e);  
        }  
        document.add(context);  
        document.close();  
        System.out.println("ok");  
    }  
}

 

 

posted on 2016-09-20 10:26  竟少  阅读(881)  评论(0编辑  收藏  举报

导航