1 package cn.com.wzf; 2 3 import java.io.FileOutputStream; 4 import java.io.OutputStream; 5 import java.io.StringReader; 6 import java.util.List; 7 8 import com.lowagie.text.Document; 9 import com.lowagie.text.PageSize; 10 import com.lowagie.text.Paragraph; 11 import com.lowagie.text.html.simpleparser.HTMLWorker; 12 import com.lowagie.text.html.simpleparser.StyleSheet; 13 import com.lowagie.text.rtf.RtfWriter2; 14 15 public class ItextCreateRTF { 16 public static void main(String[] args) throws Exception { 17 OutputStream out = new FileOutputStream("c://a.doc"); 18 Document document = new Document(PageSize.A4); 19 RtfWriter2.getInstance(document, out); 20 document.open(); 21 Paragraph context = new Paragraph(); 22 String s = "上传的图片<img width=\"800\" height=\"600\" alt=\"\" src=\"http://localhost:8081/zhmobileexp/upload/activity/photos/Image/waterlilies.jpg\" />"; 23 System.out.println(s); 24 // Image img = Image.getInstance("D:\\图片\\2.jpg"); 25 // img.setAbsolutePosition(0, 0);// 26 // document.add(img); 27 StyleSheet ss = new StyleSheet(); 28 List htmlList = HTMLWorker.parseToList(new StringReader(s), ss); 29 for (int i = 0; i < htmlList.size(); i++) { 30 com.lowagie.text.Element e = (com.lowagie.text.Element) htmlList 31 .get(i); 32 context.add(e); 33 } 34 document.add(context); 35 document.close(); 36 System.out.println("ok"); 37 } 38 }