我希望Java代码将word文件(具有文本,图像,表等的doc文件)转换为pdf文件。 [英] I want Java code to convert word file (doc file having text, images, tables etc. ) into pdf file.

查看:157
本文介绍了我希望Java代码将word文件(具有文本,图像,表等的doc文件)转换为pdf文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经添加了所有必要的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;
公共类Doc2Pdf2 {
/ **
*此方法用于将给定文件转换为PDF格式
*
* @param inputFile
* - 名称和文件的路径
* @param outputFile
* - 名称和要保存的PDF文件的路径
* @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 = new File(inputFile);
pdfDocument.add(new Paragraph(org.apache.commons.io.FileUtils
.readFileToString(file)));
}
pdfDocument.close();
writer.close();
} catch(异常异常){
System.out.println(文档异常!+异常);
}
}
public static void main(String args []){
PDFConversion pdfConversion = new PDFConversion();
pdfConversion.createPdf(C:/demo.doc,C:/demopdf.pdf,true);
}
}


解决方案

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



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


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

解决方案

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.

这篇关于我希望Java代码将word文件(具有文本,图像,表等的doc文件)转换为pdf文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆