Thursday, November 18, 2010

Covert doc, excel, text and images to PDF

To convert the text, images and html we can use iText api but that cannot be lonely use to convert the word and excel files into PDF. with the combination of apache poi and iText can covert the word and excel files into PDF format.

downlaod iText from http://itextpdf.com/
downlaod apache poi from http://poi.apache.org/download.html

package pdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.filesystem.*;

public class pdfconvert {

public static void main(String args[]){

pdfconvert pdfConversion = new pdfconvert();

// arguments are differnt for the situation
pdfConversion.createPdf("E:/L4_project/sample/images.jpg","E:/L4_project/sample/images.pdf", "image");

}

// generate the pdf file according to the input file format
// inputFile - input file path
// outputFile - output file path
// type - type of the file
private void createPdf(String inputFile, String outputFile, String type){

Document pdfdoc=new Document();
String pdfFilePath = outputFile;
WordExtractor Wextractor = null ; // word documents
ExcelExtractor Eextractor = null ; // excel documents

try{
FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath);
PdfWriter writer = null;
writer = PdfWriter.getInstance(pdfdoc, fileOutputStream);
writer.open();
pdfdoc.open();


System.out.println("Creating pdf document.....");
if (type.equals("image")){

pdfdoc.add(com.itextpdf.text.Image.getInstance(inputFile));

} else if (type.equals("text")){

File file = new File(inputFile);
pdfdoc.add(new Paragraph(org.apache.commons.io.FileUtils.readFileToString(file)));

} else if(type.equals("word")){

File file = new File(inputFile);
POIFSFileSystem fis=new POIFSFileSystem(new FileInputStream(file)) ;
HWPFDocument document=new HWPFDocument(fis);
Wextractor = new WordExtractor(document);
String [] fileData = Wextractor.getParagraphText();
String st="";

for(int i=0;i
if(fileData[i] != null)
st+=fileData[i]+" ";
}
pdfdoc.add(new Paragraph(st));

} else if(type.equals("excel")){

File file = new File(inputFile);
POIFSFileSystem fis=new POIFSFileSystem(new FileInputStream(file)) ;
HSSFWorkbook book = new HSSFWorkbook(fis);
Eextractor = new ExcelExtractor(book);
String filedata = Eextractor.getText();
pdfdoc.add(new Paragraph(filedata));

}
System.out.println("finish...");
pdfdoc.close();
writer.close();
}catch(Exception exception){
exception.printStackTrace();
System.out.println("Document Exception!" + exception);

}
}

}

4 comments:

  1. Aspose is another online converter that can convert MS Office documents to other forms in many languages like .Net , Java etc and user can create their documents online with this API they don't need MS Office installed on their computers to use this API

    ReplyDelete
  2. but this program is not giving exact pdf to given word doc

    ReplyDelete
  3. fine make regarded. i was checking at all times this blog and Im inspired! extraordinarily useful info in particular the last portion I care for such mention a lot. i was searching for this certain information for a protracted times. thanks and suitable good fortune. convert to doc

    ReplyDelete