iText 处理html格式邮件

import java.io.InputStream;

import org.xml.sax.InputSource;

import com.lowagie.text.DocListener;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.html.SAXmyHtmlHandler;
import com.lowagie.text.pdf.BaseFont;

public class ITextSurportHtmlParser extends HtmlParser {
    public ITextSurportHtmlParser(){
        super();
    }
   
    public void goGB(DocListener document, InputStream is) throws Exception{
       
			BaseFont bfChinese = BaseFont.createFont("/MSYH.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    

            parser.parse(new InputSource(is), new SAXmyHtmlHandler(document, bfChinese));
       
 
    }
}
 
Document document = new Document(); 
		try {
			FileOutputStream f=new FileOutputStream(filePath+File.separator
					+ "email.pdf");
			PdfWriter.getInstance(document,f);
			document.open();
			BaseFont bfChinese = BaseFont.createFont("/MSYH.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    
			document.add(new Paragraph("Subject:"+email.getStrsubject(), new Font(bfChinese, 10, Font.NORMAL)));
			document.add(new Paragraph("From:"+email.getStrfrom(), new Font(bfChinese, 10, Font.NORMAL)));
			document.add(new Paragraph("To:"+email.getStrto(), new Font(bfChinese, 10, Font.NORMAL)));
			document.add(new Paragraph("Dtattime:"+email.getDtattime(), new Font(bfChinese, 10, Font.NORMAL)));
			document.add(new Paragraph("Content:", new Font(bfChinese, 10, Font.NORMAL)));
			try{
				String htmText = new String(("<html>"+email.getStrcontent()+"</html>").getBytes("UTF-8"),"ISO8859-1");
	        	StringBufferInputStream html = new StringBufferInputStream(htmText);
	        	ITextSurportHtmlParser p = new ITextSurportHtmlParser();               
	            p.goGB(document, html);
			}catch(Exception e){
				System.out.println("邮件内容不是标准的XHTML,原文输出!");
				document.add(new Paragraph(email.getStrcontent(), new Font(bfChinese, 10, Font.NORMAL)));
			}
			document.close(); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
 

你可能感兴趣的:(html,xml,XHTML,F#)