从.docx转换为pdf时,图像看起来不太好 [英] Images are not appearing good when converted from .docx to pdf

查看:254
本文介绍了从.docx转换为pdf时,图像看起来不太好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I converted .docx file to .pdf file, the text is converting fine, but the images in the .docx file is not appearing, instead it is represented as some special characters, below is my code:

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


推荐答案

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

I'm not sure what it could be, but for some alternatives have a look at:


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

  • Apose.Words Library for Java it has some really cool features one of them being docx to pdf conversion by a few simple lines (and it's reliable):

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链接):

  • Docx4j which can be used to convert docx and many others to PDF, it does this by first using HTML/XML based on IText then converts it to a PDF (All libararies are included within docx4j, just added the itext link for completeness):

    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
    


  • 如果这还不够,它有示例源代码供您试用。

    If that's not enough it has sample source code for you to try.


    • xdocreport 还附带了很多示例进行转换(尚未下载,但它应该有doc / docx到PDF转换器源)

    • xdocreport also comes with a lot of samples for conversion (haven't downloaded them, but it should have the doc/docx to PDF converter source)

    这篇关于从.docx转换为pdf时,图像看起来不太好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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