从.docx转换为pdf时,图像看起来不太好

时间:2012-07-17 19:29:08

标签: java pdf apache-commons-io

我将 .docx 文件转换为 .pdf 文件,文本转换正常,但 .docx 文件中的图片不是出现,而不是它表示为一些特殊字符,下面是我的代码:

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 PDFConversion {

    /**
     * 14. This method is used to convert the given file to a PDF format 15.
     * 
     * @param inputFile
     *            - Name and the path of the file 16.
     * @param outputFile
     *            - Name and the path where the PDF file to be saved 17.
     * @param isPictureFile
     *            18.
     */

    private void createPdf(String inputFile, String outputFile, boolean isPictureFile) {
        /**
         * 22. Create a new instance for Document class 23.
         */
        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();

            /**
             * 34. Proceed if the file given is a picture file 35.
             */
            if (isPictureFile) {
                pdfDocument.add(com.lowagie.text.Image.getInstance(inputFile));
            }

            /**
             * 41. Proceed if the file given is (.txt,.html,.doc etc) 42.
             */

            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:/Users/LENOVO/Downloads/The_JFileChooser_Component.doc",
                "E:/The_JFileChooser_Component.pdf", false);
        // For other files
        // pdfConversion.createPdf("C:/shunmuga/sample.html",
        // "C:/shunmuga/sample.pdf", false);
    }
}

1 个答案:

答案 0 :(得分:6)

我不确定它可能是什么,但是对于一些替代方案,请看:

  • Apose.Words Library 对于Java来说,它有一些非常酷的功能,其中一个是通过几行简单的docx到pdf转换(并且它是可靠的):

    Document doc = new Document("d:/test/mydoc.docx");
    doc.Save("d:/test/Out.pdf", SaveFormat.Pdf);
    
  • <强> Docx4j 可以用来将docx和许多其他文件转换成PDF,它首先使用基于IText的HTML / XML然后将其转换为PDF(所有库都包含在docx4j中,只是为完整性添加了itext链接) ):

    org.docx4j.convert.out.pdf.PdfConversion c
     = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);//using xml
    // = new org.docx4j.convert.out.pdf.viaHTML.Conversion(wordMLPackage);//using html
    // = new org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage);//using itext libs
    

如果这还不够,那么你可以尝试sample source code

  • xdocreport 还附带了很多samples用于转换(尚未下载它们,但它应该有doc / docx到PDF转换器源)