【问题标题】:Error convert docx to pdf in java在java中将docx转换为pdf时出错
【发布时间】:2014-02-12 20:13:31
【问题描述】:

大家下午好,

来吧,我正在生成一个 docx 文档 Junction 2 other 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 时出现问题,出现以下错误:未实现:支持 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);
    }
}

我做错了什么?

【问题讨论】:

    标签: java apache-poi docx4j


    【解决方案1】:

    altChunk 不是“真实”的 docx 内容。

    在输出为PDF之前,需要用普通的WordML段落、表格等替换。

    您可以自己尝试这样做,如果内容不包含任何关系(图像、超链接等)或冲突的样式或编号,这很容易。进一步请看http://www.docx4java.org/blog/2010/11/merging-word-documents/..或我公司网站plutext.com

    【讨论】:

    • 嗨 JasonPlutext,感谢您的回复,我更怀疑,我将在此行之后执行此过程:converter.output (Oct, pdfSettings);而当我宣布这个公共进程 WordprocessingMLPackage(WordprocessingMLPackage srcPackage)?你能给我一个例子吗?谢谢您的帮助。拥抱!
    【解决方案2】:

    这个可以解决

    altChunk 不是“真实”的 docx 内容。

    使用java我们可以将altchunk转换为原始内容词标签,

    在docx中转换document.xml

    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/

    注意:

    请忽略 html 内容中的 &nbsp 和其他此类标签。 我只检查了&nbsp。

    感谢重播...

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 2017-04-14
      • 1970-01-01
      相关资源
      最近更新 更多