I want Java code to convert word file (doc fil

2019-09-21 19:42发布

问题:

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);    
        }    
    }

回答1:

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.



回答2:

You need to add the latest jar in the java build path. Check your projects build path and make sure the jar is present there. Do a clean build and clean publish and it should work. If not then you can even try to directly paste the jar in your projects deployment location (LIB folder).



回答3:

you can go through tutorials of how to write data into pdf

Generate Pdf

creating pdf

create pdf sample hello program

and to read doc file Apache Tika is best :

have a look to read doc file using apache tika in java:

i am reading content from doc file and writing into text file but after learn you can write data into pdf .

public class Tikaconvrt {

    public static void main(String [] args) throws IOException, SAXException, TikaException
    {
        Tikaconvrt tc=new Tikaconvrt();


        File Re_F = new File("/home/rahul/Documents/212/ANIR.docx");

        String F_Name=Re_F.getName();
        int eof=F_Name.lastIndexOf('.');
        F_Name=F_Name.substring(0, eof); 

        String s1 = tc.contentEx(Re_F);
        tc.files(s1, F_Name);
        }


    public String contentEx(File f) throws IOException, SAXException,
            TikaException {

        InputStream is = new FileInputStream(f);

        Parser ps = new AutoDetectParser();

        BodyContentHandler bch = new BodyContentHandler();
        Metadata metadata = new Metadata();
        ps.parse(is, bch, metadata, new ParseContext());

        return bch.toString();
    }

    public void files(String st,String fname) throws IOException {
        FileWriter fw = new FileWriter("/home/rahul/Documents/txt/"+fname+".txt",
                true);
        BufferedWriter bufferWritter = new BufferedWriter(fw);
        bufferWritter.write(st + "\n");
        bufferWritter.close();
    }

}


标签: java itext