2017-07-06 519 views
4

我想用apache poi將docx轉換爲pdf,使用docx4j正確生成docx。用簡單的文檔轉換工作很好,但是當我想轉換一個更程式化的文檔,POI會拋出異常:使用POI從docx生成pdf時出錯

org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException:工會值「0000FF「> HTTP: //schemas.openxmlformats.org/wordprocessingml/2006/main' 15點09分20秒org.apache.poi.xwpf.converter.core.XWPFConverterException:org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException:工會值「0000FF 「> http://schemas.openxmlformats.org/wordprocessingml/2006/main' 在org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:70)〜[org.apache.poi。 xwpf.converter.pdf-1.0.6.jar:1.0.6]

有此異常的原因:

<w:r> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> 
     <w:color w:val="0000FF"><span style="background-color: rgb(51, 153, 102);"><span style="background-color: rgb(255, 0, 0);"><font color="99CC00"/> 
     <w:sz w:val="20"/> 
     <w:szCs w:val="20"/> 
     <w:highlight w:val="red"/> 
    </w:rPr> 
    <w:t xml:space="preserve">Juillet-Aout</w:t> 
</w:r> 

Screen of my document

這是我的代碼:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 


import org.apache.poi.xwpf.usermodel.XWPFDocument; 

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter; 
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; 

public class ConvertDocxPdf 
{ 

    public static void main(String[] args) 
    { 
     long startTime = System.currentTimeMillis(); 

     try 
     { 
      // 1) Load docx with POI XWPFDocument 
      InputStream source = new FileInputStream("test.docx"); 
      XWPFDocument document = new XWPFDocument(source); 

      // 2) Convert POI XWPFDocument 2 PDF with iText 
      File outFile = new File("result.pdf"); 
      outFile.getParentFile().mkdirs(); 

      OutputStream out = new FileOutputStream(outFile); 
      PdfOptions options = null;// PDFViaITextOptions.create().fontEncoding("windows-1250"); 
      PdfConverter.getInstance().convert(document, out, options); 
     } 
     catch (Throwable e) 
     { 
      e.printStackTrace(); 
     } 

     System.out.println("Generate DocxStructures.pdf with " + (System.currentTimeMillis() - startTime) + " ms."); 
    } 
} 

這是XML行引起問題:

<w:r> 
    <w:rPr> 
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> 
     <w:color w:val="0000FF"><span style="background-color: rgb(51, 153, 102);"><span style="background-color: rgb(255, 0, 0);"><font color="99CC00"/> //<-- That line 
     <w:sz w:val="20"/> 
     <w:szCs w:val="20"/> 
     <w:highlight w:val="red"/> 
    </w:rPr> 
    <w:t xml:space="preserve">Juillet-Aout </w:t> 
</w:r> 
+1

基於stacktrace,您使用的是舊版本的XDocs Reports轉換器。嘗試升級 – Gagravarr

+0

我不明白要升級什麼,根據升級我的Org.apache.poi.xwpf.converter.pdf是最新版本 –

+0

您不應該使用該包名稱,這是不正確的和誤導性的。最新版本是2.0.1 – Gagravarr

回答