我想要 Java 代码将 word 文件(包含文本、图像、表格等的 doc 文件)转换为 pdf 文件。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18732808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:34:11  来源:igfitidea点击:

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

javaitext

提问by Nitesh

I have added all the necessary jar files including itextpdf-5.1.0.jarbut still it gives errors.. please refer below code. I searched it on net but it's not working.

我已经添加了所有必要的 jar 文件,包括itextpdf-5.1.0.jar但它仍然给出错误..请参考下面的代码。我在网上搜索了它,但它不起作用。

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 jarfile but not getting the solution.

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

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

回答by Pratik Shelar

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).

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

回答by Rahul Kulhari

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

您可以阅读有关如何将数据写入 pdf 的教程

Generate Pdf

生成PDF

creating pdf

创建pdf

create pdf sample hello program

创建 pdf 示例 hello 程序

and to read doc file Apache Tika is best :

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

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

看看在java中使用apache tika读取doc文件:

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

我正在从 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();
    }

}

回答by Bruno Lowagie

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.lowagiewith com.itextpdf.

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

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

顺便说一句:您的问题的标题与问题不匹配,因为 iText 不会将 Word 文档转换为 PDF。