doc/pdf convert using java program


import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class ConvertDocToPDF {
public static void main(String[] args) {
File file = null;
WordExtractor extractor = null ;
try {

file = new File("New.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
String [] fileData = extractor.getParagraphText();
String st="";
for(int i=0;i<fileData.length;i++){
if(fileData[i] != null)
st+=fileData[i]+" ";
}
Document doc=new Document();
PdfWriter.getInstance(doc,new FileOutputStream("Hello.pdf"));
doc.open();
doc.add(new Paragraph(st));
doc.close();
file.delete();
}
catch(Exception e){}
}
}

Hope that it will be helpful for you.
Thanks

Comments