首页 文章

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

提问于
浏览
-3

我已经添加了所有必要的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

    您正在使用高于5的iText版本(使用包 com.itextpdf ),但是您正在从iText 5之前的iText版本中仅存在的包 com.lowagie (是的,'s my name; I' m,iText的原始作者)中导入类 . 您发现的课程是正常的 . 您应该将 com.lowagie 替换为 com.itextpdf .

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

  • 0

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

  • 3

    您可以浏览如何将数据写入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();
        }
    
    }
    

相关问题