自己动手写CSDN博客提取器源码分析之二:处理网页保存为doc文件

转载自: http://blog.csdn.net/w397090770/article/details/7768089

 

下面是把抓取到的网页用itext包保存为doc文件,过程很简单的。下面是这个类的实现:

/**
 * 
 */
package com.wyp.html2doc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.rtf.RtfWriter2;

/**
 * @author w397090770
 * Create Data: 2012-7-18
 * Email: wyphao.2007@163.com
 * 
 * 版权所有,翻版不究,但是在修改本程序的时候务必加上这些注释。谢谢
 * 仅用于学习交流之用
 */
public class saveAsDOC {
	@SuppressWarnings("unchecked")
	public  void getDoc(File file, String contents, String title) throws Exception {
		//file是保存文件的文件夹路径,contests是前面处理好的网页源码,title是帖子的标题,用于标记生成的文件名		
		File saveFileName = new File(file.getAbsoluteFile() + File.separator + title + ".doc");
		if(!saveFileName.exists()){
			saveFileName.createNewFile();
		}else{		//和以前处理txt文件一样,如果先前这个文件已经存在,我们就不打算再去生成doc文件了,直接返回
			return;
		}
		// 设置纸张大小
		Document document = new Document(PageSize.A4);
		// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
		RtfWriter2.getInstance(document,new FileOutputStream(saveFileName));
		document.open();
		// 设置中文字体
		//BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 正文字体风格
		//Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
		
		// 添加页眉
		HeaderFooter header = new HeaderFooter(new Phrase(title), false);
		header.setAlignment(Rectangle.ALIGN_CENTER);
		document.setHeader(header);
		// 添加页脚
		/*HeaderFooter footer = new HeaderFooter(new Phrase(), false);
		footer.setAlignment(Rectangle.ALIGN_CENTER);
		document.setFooter(footer);*/
		
		
		Paragraph paragraph = new Paragraph();
		paragraph.setAlignment(Element.ALIGN_LEFT);
		paragraph.setFirstLineIndent(20);

		StyleSheet ss = new StyleSheet();
		// 添加网页里面的东西
		List<Element>htmlList = HTMLWorker.parseToList(new StringReader(contents),ss);
		for (int i = 0; i < htmlList.size(); i++) {
			Element e = htmlList.get(i);
			paragraph.add(e);
		}

		document.add(paragraph);
		document.close();
	}
}


 

代码很简单吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值