在java中将docx转换为pdf

我试图将包含表格和图像的docx文件转换为pdf格式文件。

我一直在寻找,但没有得到适当的解决方案,要求提供正确和正确的解决方案:

在这里我尝试过:

 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; public class TestCon { public static void main(String[] args) { TestCon cwoWord = new TestCon(); System.out.println("Start"); cwoWord.ConvertToPDF("D:\\Test.docx", "D:\\Test1.pdf"); } public void ConvertToPDF(String docPath, String pdfPath) { try { InputStream doc = new FileInputStream(new File(docPath)); XWPFDocument document = new XWPFDocument(doc); PdfOptions options = PdfOptions.create(); OutputStream out = new FileOutputStream(new File(pdfPath)); PdfConverter.getInstance().convert(document, out, options); System.out.println("Done"); } catch (FileNotFoundException ex) { System.out.println(ex.getMessage()); } catch (IOException ex) { System.out.println(ex.getMessage()); } } } 

例外:

 Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.apache.poi.util.POILogger.log(ILjava/lang/Object;)V from class org.apache.poi.openxml4j.opc.PackageRelationshipCollection at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:313) at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.(PackageRelationshipCollection.java:162) at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.(PackageRelationshipCollection.java:130) at org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:559) at org.apache.poi.openxml4j.opc.PackagePart.(PackagePart.java:112) at org.apache.poi.openxml4j.opc.PackagePart.(PackagePart.java:83) at org.apache.poi.openxml4j.opc.PackagePart.(PackagePart.java:128) at org.apache.poi.openxml4j.opc.ZipPackagePart.(ZipPackagePart.java:78) at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:239) at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:665) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39) at org.apache.poi.xwpf.usermodel.XWPFDocument.(XWPFDocument.java:121) at test.TestCon.ConvertToPDF(TestCon.java:31) at test.TestCon.main(TestCon.java:25) 

我的要求是创建一个java代码,将现有的docx转换为具有适当格式和对齐的pdf。

请建议。

使用的jar子:

更新的罐子

你错过了一些图书馆。

我可以通过添加以下库来运行您的代码:

     Apache POI 3.15
     org.apache.poi.xwpf.converter.core-1.0.6.jar
     org.apache.poi.xwpf.converter.pdf-1.0.6.jar
     fr.opensagres.xdocreport.itext.extension-2.0.0.jar
     iText的 -  2.1.7.jar
     OOXML-模式-1.3.jar

我已成功转换了6页长的Word文档(.docx),其中包含表格,图像和各种格式。

除了VivekRatanSinha的答案 ,我还想为将来需要它的人发布完整的代码和必需的jar子。

码:

 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; public class WordConvertPDF { public static void main(String[] args) { WordConvertPDF cwoWord = new WordConvertPDF(); cwoWord.ConvertToPDF("D:/Test.docx", "D:/Test.pdf"); } public void ConvertToPDF(String docPath, String pdfPath) { try { InputStream doc = new FileInputStream(new File(docPath)); XWPFDocument document = new XWPFDocument(doc); PdfOptions options = PdfOptions.create(); OutputStream out = new FileOutputStream(new File(pdfPath)); PdfConverter.getInstance().convert(document, out, options); } catch (IOException ex) { System.out.println(ex.getMessage()); } } } 

和JARS:

必需的罐子

请享用 :)

我用这个代码。

 private byte[] toPdf(ByteArrayOutputStream docx) { InputStream isFromFirstData = new ByteArrayInputStream(docx.toByteArray()); XWPFDocument document = new XWPFDocument(isFromFirstData); PdfOptions options = PdfOptions.create(); //make new file in c:\temp\ OutputStream out = new FileOutputStream(new File("c:\\tmp\\HelloWord.pdf")); PdfConverter.getInstance().convert(document, out, options); //return byte array for return in http request. ByteArrayOutputStream pdf = new ByteArrayOutputStream(); PdfConverter.getInstance().convert(document, pdf, options); document.write(pdf); document.close(); return pdf.toByteArray(); }