2014-02-12 100 views
1

下午好一切,錯誤DOCX轉換爲PDF在Java

來我的情況下,我產生的docx文檔結等2個DOCX,我做了合併。

public static void main(String[] args) throws Exception { 
    InputStream in1 = new FileInputStream(new File("C:\\Clientes\\Constremac\\Repositorio_DOCS\\UPLOAD\\LAYOUT_PAGINA_VERSAO_FINAL.docx")); 
    InputStream in2 = new FileInputStream(new File("C:\\Clientes\\Constremac\\Repositorio_DOCS\\UPLOAD\\modeloContratoSocial.docx")); 
    OutputStream out = new FileOutputStream(new File("C:\\Clientes\\Constremac\\Repositorio_DOCS\\UPLOAD\\modeloContratoSocialMerge.docx")); 
    mergeDocx(in1,in2,out); 
} 

public static void mergeDocx(InputStream s1, InputStream s2, OutputStream os) throws Exception { 
    WordprocessingMLPackage target = WordprocessingMLPackage.load(s1); 
    insertDocx(target.getMainDocumentPart(), IOUtils.toByteArray(s2)); 
    SaveToZipFile saver = new SaveToZipFile(target); 
    saver.save(os); 
} 
private static void insertDocx(MainDocumentPart main, byte[] bytes) throws Exception { 
     AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + (chunk++) + ".docx")); 
     afiPart.setContentType(new ContentType(CONTENT_TYPE)); 
     afiPart.setBinaryData(bytes); 
     Relationship altChunkRel = main.addTargetPart(afiPart); 
     //convertAltChunks() 
     CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk(); 
     chunk.setId(altChunkRel.getId()); 

     main.addObject(chunk); 
} 

我的最終文檔(docx)是好的,我可以正常打開它。當我將這個生成的文件轉換爲PDF時,會出現以下錯誤:NOT IMPLEMENTED:support for w:altChunk - 。

public boolean createPDF(String nomeArquivo) { 
    try  { 
     long start = System.currentTimeMillis(); 
     Configuration confg = new Configuration(); 

     System.out.println(Configuration.repositorioUpload + nomeArquivo + ".docx"); 
     InputStream is = new FileInputStream(new File(Configuration.repositorioUpload + nomeArquivo + ".docx")); 
     WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is); 

     PdfSettings pdfSettings = new PdfSettings(); 

     OutputStream out = new FileOutputStream(new File(Configuration.repositorioUpload + nomeArquivo + ".pdf")); 
     PdfConversion converter = new Conversion(wordMLPackage); 
     converter.output(out, pdfSettings); 

     System.err.println("Generate " + Configuration.repositorioUpload + nomeArquivo + ".pdf" + " with " + (
       System.currentTimeMillis() - start) + "ms"); 
    } 
    catch (Throwable e) { 
     e.printStackTrace(); 
    } 
    return false; 
} 

我要送我用Java代碼,有一陣子我想生成這個PDF格式,如果有人能幫助我,我很感激。

謝謝大家。

擁抱!


我找到了一種方法來使用AltChunck,但即使沒有超出正常運行時導出爲PDF沒有出現合併圖像的頁腳和頭。

public static void main(String[] args) throws Exception { 

    boolean ADD_TO_HEADER = true; 
    HeaderPart hp = null; 

    String inputfilepath = "C:\\Clientes\\Constremac\\Repositorio_DOCS\\UPLOAD\\default_template.xml"; 

    String chunkPath = "C:\\Clientes\\Constremac\\Repositorio_DOCS\\UPLOAD\\sample.docx"; 

    boolean save = true; 
    String outputfilepath = "C:\\Clientes\\Constremac\\Repositorio_DOCS\\UPLOAD\\altChunk_out.docx"; 


    // Open a document from the file system 
    // 1. Load the Package 
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); 
    //proce 
    MainDocumentPart main = wordMLPackage.getMainDocumentPart(); 

    if (ADD_TO_HEADER) { 
     hp = wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getDefaultHeader(); 
    } 

    AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/chunk.docx")); 
    afiPart.setBinaryData(new FileInputStream(chunkPath)); 

    afiPart.setContentType(new ContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml")); //docx 
    //afiPart.setContentType(new ContentType("application/xhtml+xml")); //xhtml 

    Relationship altChunkRel = null; 
    if (ADD_TO_HEADER) { 
     altChunkRel = hp.addTargetPart(afiPart); 
    } else { 
     altChunkRel = main.addTargetPart(afiPart);   
    } 

    CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk(); 
    ac.setId(altChunkRel.getId()); 

    if (ADD_TO_HEADER) { 
     hp.getJaxbElement().getEGBlockLevelElts().add(ac); 
    } else { 
     main.addObject(ac); 
    } 

    // Save it 

    if (save) {  
     SaveToZipFile saver = new SaveToZipFile(wordMLPackage); 
     saver.save(outputfilepath); 
     System.out.println("Saved " + outputfilepath); 
    } 
} 

我在做什麼錯?

回答

2

altChunk不是「真正」的docx內容。

之前,它可以在PDF輸出,它需要與正常的WordML段落,表格等

你可以試試這個自己做,這是很容易做到,如果內容不包括任何關係(圖片被替換,超鏈接等),或衝突的風格或編號。請參閱進一步http://www.docx4java.org/blog/2010/11/merging-word-documents/ ..或者我公司的網站plutext.com

+0

嗨JasonPlutext,感謝您的答覆,我更像是一個疑問,我將在行後執行此過程: 轉換器。輸出(Oct,pdfSettings); 而我聲明這個公共過程WordprocessingMLPackage(WordprocessingMLPackage srcPackage)? 你能舉個例子嗎? 感謝您的幫助。 擁抱! –

0

這是可以解決的

的altChunk是不是「真正」的docx內容。

使用Java,我們可以altchunk轉換成原始內容的字標籤,

轉換document.xml中內的docx

Docx4jProperties.setProperty(「docx4j.Convert.Out.HTML.OutputMethodXML」, 
true); 
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL); 

打開鏈接,完整的代碼。

[轉換AltChunk和原始內容] [1]

https://kishankichi.wordpress.com/2016/05/26/convert-altchunk-to-original-content-or-convert-to-real-docx-format-using-java

https://kishankichi.wordpress.com/2016/05/26/convert-altchunk-to-original-content-or-convert-to-real-docx-format-using-java/

注:

請忽略& NBSP等這樣的標籤在你的HTML內容。 我只檢查了& nbsp。

感謝您的重播...

+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/12477815) –

+0

好的,先生。感謝您寶貴的重播。 –