I have added all the necessary jar files including itextpdf-5.1.0.jar
but still it gives errors..
please refer below code.
I searched it on net but it's not working.
It gives error while importing
com.lowagie.text.Document;
com.lowagie.text.Paragraph;
com.lowagie.text.pdf.PdfWriter;
Don't understand what is going wrong. I added latest version of iText jar
file but not getting the solution.
please give me correct solution or code.
please mention it stepwise.
because I'm doing this first time...
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
public class Doc2Pdf2 {
/**
* This method is used to convert the given file to a PDF format
*
* @param inputFile
* - Name and the path of the file
* @param outputFile
* - Name and the path where the PDF file to be saved
* @param isPictureFile
*/
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();
if (isPictureFile) { pdfDocument.add(com.lowagie.text.Image.getInstance(inputFile));
} else {
File file = new File(inputFile);
pdfDocument.add(new Paragraph(org.apache.commons.io.FileUtils
.readFileToString(file)));
}
pdfDocument.close();
writer.close();
} catch (Exception exception) {
System.out.println("Document Exception!" + exception);
}
}
public static void main(String args[]) {
PDFConversion pdfConversion = new PDFConversion();
pdfConversion.createPdf("C:/demo.doc", "C:/demopdf.pdf", true);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…