【问题标题】:Images are not appearing good when converted from .docx to pdf从 .docx 转换为 pdf 时,图像看起来不太好
【发布时间】:2012-07-17 19:29:08
【问题描述】:

我将 .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);
    }
}

【问题讨论】:

    标签: java pdf apache-commons-io


    【解决方案1】:

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

    • 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的转换器源码)

    【讨论】:

    • +1 优秀的伙伴,我使用 docx4j 并且能够完美地创建 pdf 文件。我必须等到赏金结束,是否有任何机构提供更多建议。
    猜你喜欢
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2019-01-26
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多