我希望Java代码将word文件(包含文本,图像,表等的doc文件)转换为pdf文件。

时间:2013-09-11 04:39:28

标签: java itext

我添加了所有必要的jar文件,包括itextpdf-5.1.0.jar,但仍然会出错。 请参考以下代码。 我在网上搜索了它,但它没有用。

导入时出错

com.lowagie.text.Document; 
com.lowagie.text.Paragraph; 
com.lowagie.text.pdf.PdfWriter;

不明白出了什么问题。我添加了iText jar文件的最新版本,但未获得解决方案。

请给我正确的解决方案或代码。 请逐步提一下。 因为我第一次这样做......

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

3 个答案:

答案 0 :(得分:3)

您正在使用高于5的iText版本(包com.itextpdf),但您正在从包com.lowagie导入类(是的,这是我的名字;我是原作者iText只存在于iText版本之前的iText 5.因此,找不到你正在使用的类是正常的。您应该将com.lowagie替换为com.itextpdf

顺便说一下:问题的标题与问题不符,因为iText不会将Word文档转换为PDF格式。

答案 1 :(得分:0)

您需要在java构建路径中添加最新的jar。检查项目构建路径并确保jar存在于那里。做一个干净的构建和干净的发布,它应该工作。如果没有,那么您甚至可以尝试直接将jar粘贴到项目部署位置(LIB文件夹)中。

答案 2 :(得分:0)

您可以浏览如何将数据写入pdf的教程

Generate Pdf

creating pdf

create pdf sample hello program

并阅读doc文件Apache Tika是最好的:

在java中使用apache tika来查看doc文件:

我正在阅读doc文件中的内容并写入文本文件,但在学习之后,您可以将数据写入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();
    }

}
相关问题