以下为代码,文档可以随便建一个。package com.dexpre.test;import java.io.FileInputStream;
import java.io.FileOutputStream;
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;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;public class FormatTest {
public static void main(String[] args) throws Exception {
out();
} /**
 * xiangyong 
 * @throws Exception 
 */
private static void out() throws Exception {
String docPath="F:\\Test\\Test.doc";
System.err.println(docPath);
  InputStream doc = new FileInputStream(docPath);
  String[] pdfPath1 = docPath.split(".doc");
  String docx = pdfPath1[0]+".docx";
  System.err.println(docx);
  XWPFDocument documentPdf = new XWPFDocument(doc);
OutputStream outPdf = new FileOutputStream(docx);
XWPFTable table = documentPdf.createTable();
table.setWidth(1000);
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("×·¼Ó±í¸ñ");
documentPdf.write(outPdf);
doc.close();
outPdf.close();

//docxתpdf
doc2pdf(docx);

} private static void doc2pdf(String docx) throws Exception {
InputStream doc = new FileInputStream(docx);
  String[] pdfPath1 = docx.split(".docx");
  String pdfString = pdfPath1[0]+".pdf";
  System.err.println(pdfString);
  XWPFDocument documentPdf = new XWPFDocument(doc);
OutputStream outPdf = new FileOutputStream(pdfString);
documentPdf.write(outPdf);
PdfOptions pdfOptions = PdfOptions.create();
PdfConverter.getInstance().convert(documentPdf, outPdf, pdfOptions); 
}

}

解决方案 »

  1.   

    报错
    exception in thread "main org. apache poi . xwpf converter .core XWPFConverterException: java.lang. NullPoi nterExceptionat org- apache poi xwpf.converter. pdf . PdfConverter .doConvert (PdfConverter. java:59)at org. apache . poi . xwpf . converter. pdf . PdfConverter . doConvert (PdfConverter. java:37)at org. apache . poi . xwpf . converter.core . AbstractXWPFConverter . convert(Abstra tXWPFConverter . java:45)at com amansoft .dexpre . test. FormatTest. out(FormatTest. java:154)at com. amarsoft . dexpre . test .FormatTest . main(FormatTest. java:45)caused by: java.lang . NullPointerExceptionat org.apache poi . xwpf . converter . core . utils .XWPFTableUtil . getGridColList(XWPFTableUtil : java:183)at org.apache .poi .xwpf . converter . core .utils .XWPFTableUtil. computeColwidths (XWPFTableutil. java:116)at org. apache . poi ,xwpf . converter . core . XWPFDocumentVisitor ,visitTable(XWPFDocumentVisitor java:828)at org.apache . poi . xwpf . converter . core .XWPFDocumentVisitor .visitBodyElements (XWPFDocumentVisitor ava:246)at org. apache . poi . xwpf . converter . core . XWPFDoc umentvisitor .start (XNPFDocumentVisitor. iava:194)at org. apache poi . xwpf . converter .pdf .PdfConverter .doConvert(PdfConverter .java:55)      4 more