用java代码把docx转换成pdf文件

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
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 Main {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        String docPath = "C:\\Users\\Administrator\\Documents\\Tencent Files\\815703271\\FileRecv\\MobileFile\\shangedaijiekuanxieyi.docx";
        String pdfPath = "C:\\Users\\Administrator\\Documents\\Tencent Files\\815703271\\FileRecv\\MobileFile\\shangedaijiekuanxieyi.pdf";
        XWPFDocument document;
        try (InputStream doc = Files.newInputStream(Paths.get(docPath))) {
            document = new XWPFDocument(doc);
        }
        PdfOptions options = PdfOptions.create();
        try (OutputStream out = Files.newOutputStream(Paths.get(pdfPath))) {
            PdfConverter.getInstance().convert(document, out, options);
        }

    }

}

 

posted @ 2016-05-30 14:52  吐槽村  阅读(10796)  评论(8编辑  收藏  举报