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

 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     }  

 

转载于:https://www.cnblogs.com/SK1995/p/5863912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解关于使用iText和FreeMarker来生成HTML并将其换为Word文档的内容。首先,您需要了解几个概念: 1. iText是一个用于生成PDF文档的Java库,但它也可以用于生成其他类型的文档,例如Word文档。 2. FreeMarker是一个用于生成动态内容的Java模板引擎,它可以将数据模型与模板文件结合使用,生成静态文本文件。 接下来,您可以按照以下步骤来实现将HTML换为Word文档: 1. 创建一个FreeMarker模板文件,其包含您要生成HTML内容,并将其保存为.ftl文件。 2. 在Java代码,使用FreeMarker来读取.ftl文件并将数据模型填充到模板生成HTML字符串。 3. 使用iTextHTML字符串换为Word文档。您可以使用iText的XMLWorkerHelper类来实现此功能。 以下是一个简单的示例代码,用于将HTML字符串换为Word文档: ``` // Step 1: Load the FreeMarker template Configuration cfg = new Configuration(Configuration.VERSION_2_3_28); cfg.setDirectoryForTemplateLoading(new File("templates")); Template template = cfg.getTemplate("my_template.ftl"); // Step 2: Generate HTML string from the template and data model Map<String, Object> data = new HashMap<>(); data.put("name", "John Doe"); StringWriter out = new StringWriter(); template.process(data, out); String html = out.getBuffer().toString(); // Step 3: Convert HTML to Word document using iText ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(html.getBytes())); document.close(); // Save the Word document to file FileOutputStream fos = new FileOutputStream("output.docx"); fos.write(baos.toByteArray()); fos.close(); ``` 请注意,此示例代码仅用于说明概念,并且可能需要进行修改以适合您的特定需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值