Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

itext - I want Java code to convert word file (doc file having text, images, tables etc. ) into pdf file.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using a version of iText that is higher than 5 (with packages com.itextpdf), yet you are importing classes from packages com.lowagie (yes, that's my name; I'm the original author of iText) that only exist in versions of iText predating iText 5. Hence it is normal that the classes you're using aren't found. You should replace com.lowagie with com.itextpdf.

By the way: the title of your question doesn't match the question because iText doesn't convert Word documents to PDF.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...