/*
* Created on 2005-4-4
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.io.BufferedReader ;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException ;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class ConvertToPdf {
public ConvertToPdf(String FileName){
int i,subFix;
String s,s2=new String();
String s1;
try{
Document doc=new Document(PageSize.A4,36,36,36,36);//设置页面大小和边界宽度
// doc.addAuthor("张兴刚");
// doc.addHeader("wwwwwwsd","dddssss");
//去掉后缀名
subFix=FileName.lastIndexOf(".");
if (subFix!=-1)
s1=FileName.substring(0,subFix);
else
s1=FileName;
// if (FileName.endsWith(".txt")||(FileName.endsWith(".rtf")))
// s1=FileName.substring(0,FileName.indexOf("."));
// else
// s1=FileName;
PdfWriter.getInstance(doc, new FileOutputStream(s1+".pdf"));
FileReader in=new FileReader(FileName);
BufferedReader txt=new BufferedReader(in);
while((s=txt.readLine())!=null){
s2+=s+"\n";
}
doc.open() ;//打开文档
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
Paragraph para=new Paragraph(s2,FontChinese);
doc.add(para);
doc.close();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
if (args.length<0)
System.out.println("请给出文件名");
else
new ConvertToPdf(args[0]);
}
}