我将.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);
}
}
我不确定它可能是什么,但对于一些替代方案,请查看:
-
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
如果这还不够的话,它有一些示例源代码供您尝试。
- xdocreport还附带了许多用于转换的示例(尚未下载,但它应该有doc/docx到PDF转换器的源代码)