2017-04-12 166 views
6

我试图将包含表格和图像的docx文件转换为pdf格式的文件。在doc中将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.<init>(PackageRelationshipCollection.java:162) 
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:130) 
at org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:559) 
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:112) 
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:83) 
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:128) 
at org.apache.poi.openxml4j.opc.ZipPackagePart.<init>(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.<init>(XWPFDocument.java:121) 
at test.TestCon.ConvertToPDF(TestCon.java:31) 
at test.TestCon.main(TestCon.java:25) 

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

请建议。 罐使用:Updated jars

编辑:我搜索这个API,https://cloudconvert.com/api,任何人都知道如何在Java代码中实现这一点。

+0

的〔如何转换MS DOC到PDF](http://stackoverflow.com/questions/3022376/how-to-convert-ms-doc-to-pdf) –

+0

@KrzysztofCichocki可以是可能的复制,但我没有从这个问题得到帮助。 –

+1

这里也是另一个答案,如果你坚持在ApachePOI:http://stackoverflow.com/questions/6201736/javausing-apache-poi-how-to-convert-ms-word-file-to-pdf –

回答

8

您错过了一些图书馆。

我能够通过添加以下库运行代码:

 
    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-schemas-1.3.jar 

我已成功将6页长的Word文档(.docx)与表格,图片和各种格式。

+0

只需注意,即使使用软件包名称,也不会使用POI来执行转换。只有来自Apache POI的'ooxml-schemas-1.3.jar'。其余的来自'opensagres'和'itext'项目。 – jmarkmurphy

+0

@jmarkmurphy我的方法不是重新发明轮子,只是为了让问题的代码工作。 – VivekRatanSinha

+0

你的答案没有错,只是不想让任何人误解'org.apache.poi.xwpf.converter。*'包是Apache POI的一部分。 – jmarkmurphy

12

除了VivekRatanSinha answer之外,我还想发布完整的代码,并为将来需要的人员提供所需的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()); 
     } 
    } 
} 

和罐子:

required jars

享受:)

0

我用这个代码。

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