Hi All,
I have a requirement to convert any input files (txt,
doc,docx,jpg,html,xls etc) to PDF.
I am using the below code to convert the input file to PDF , it is working for txt file but not doc, xls etc as I am getting some special characters in the output file. Can anyone help me with code to accomplish the same.. Your help will be appreciated.. Thanks
private void createPdf(
String inputFile, String outputFile, boolean isPictureFile)
{
Document pdfDocument = new Document();
String pdfFilePath = outputFile;
try
{
FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath);
PdfWriter writer = null;
writer = PdfWriter.getInstance(pdfDocument, fileOutputStream);
writer.open();
pdfDocument.open();
/**
34.
* Proceed if the file given is a picture file
35.
*/
if (isPictureFile)
{
pdfDocument.add(com.lowagie.text.Image.getInstance(inputFile));
}
/**
* Proceed if the file given is (.txt,.html,.doc etc)
*/
else
{
File file = new File(inputFile);
String temp=null;
pdfDocument.add(new Paragraph(org.apache.commons.io.FileUtils.readFileToString(file,temp)));
}
pdfDocument.close();
writer.close();
}
catch (Exception exception)
{
System.out.println("Document Exception!" + exception);
}
}