简体   繁体   English

Docx4J 表格字体类型被忽略

[英]Docx4J Table font type ignored

I am trying to convert HTML to Docx using DOCX4J.我正在尝试使用 DOCX4J 将 HTML 转换为 Docx。 We are using the following code.我们正在使用以下代码。 All of the conversion works except the table text is always Calibiri in Final Docx file.除表格文本外,所有转换均在 Final Docx 文件中始终为 Calibiri。 We try to handle this within the code, CSS and Docx4J without any success for last couple of days.我们尝试在代码 CSS 和 Docx4J 中处理此问题,但过去几天没有任何成功。 It will be great if you can help.如果您能提供帮助,那就太好了。

public String xhtmlToDocx(List<String> fileData, File destinationFile) {

    try {
        InputStream in;
        in = new FileInputStream(new File(TCellUtil.TCELL_DATA_LOG_FOLDER_PATH + File.separator + "Header_Temp.docx"));
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(in);
        System.out.println("before");
        for (String s : wordMLPackage.getMainDocumentPart().fontsInUse())
            System.out.println(s);
        System.out.println("aFTER ");
        VariablePrepare.prepare(wordMLPackage);

        RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
        rfonts.setAscii("Times New Roman");
        rfonts.setHAnsi("Times New Roman");
        rfonts.setEastAsia("Times New Roman");
        XHTMLImporterImpl.addFontMapping("Times New Roman", rfonts);
        wordMLPackage.getMainDocumentPart().getPropertyResolver().getDocumentDefaultRPr().setRFonts(rfonts);

        int c = 1;
        for (String xhtml : fileData) {
            String file_data = "<html>" + xhtml + "</html>";
            AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw" + c + ".html"));
            afiPart.setContentType(new ContentType("text/html"));
            afiPart.setBinaryData(file_data.getBytes());

            Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart, AddPartBehaviour.REUSE_EXISTING);
            CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
            ac.setId(altChunkRel.getId());
            wordMLPackage.getMainDocumentPart().addObject(ac);
            wordMLPackage.getMainDocumentPart().addObject(createPageBreak());
            ((CTSettings) wordMLPackage.getMainDocumentPart().getDocumentSettingsPart().getJaxbElement()).setUpdateFields(new BooleanDefaultTrue());
            wordMLPackage.getMainDocumentPart().convertAltChunks();

            for (String s : wordMLPackage.getMainDocumentPart().fontsInUse())
                System.out.println(s);
            wordMLPackage.getContentTypeManager().addDefaultContentType("docx", "text/html");
            c++;
        }
        XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true);
        wordMLPackage.save(destinationFile);
        this.dest_filePath = destinationFile.getAbsolutePath();

        return this.dest_filePath;
    } catch (Docx4JException var12) {
        var12.printStackTrace();
    } catch (Exception var13) {
        var13.printStackTrace();
    }
    return null;
}

The first thing to understand is whether docx4j is doing the conversion, or Word is doing it.首先要了解的是 docx4j 是在做转换,还是 Word 在做。

docx4j would be trying to do it if you specifically invoked docx4j-ImportXHTML, or if you used convertAltChunks().如果您专门调用了 docx4j-ImportXHTML,或者您使用了 convertAltChunks(),则 docx4j 会尝试这样做。 Note that docx4j only converts an altChunk of type XHTML (ie not HTML).请注意,docx4j 只转换 XHTML 类型的 altChunk(即不是 HTML)。 So if it is an HTML altChunk, Word is doing the conversion.因此,如果它是 HTML altChunk,Word 正在执行转换。

In the code you posted:在您发布的代码中:

           AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw"+c+".html"));

                   afiPart.setContentType(new ContentType("text/html"));

            afiPart.setBinaryData(file_data.getBytes());

So it is Word which is doing the conversion (and using Calibri in tables).所以是 Word 进行转换(并在表格中使用 Calibri)。

Also, be sure to check that your (X)HTML is well-formed XML.此外,请务必检查您的 (X)HTML 格式是否正确 XML。 If it isn't, you'll need to correct your input, either in the system which is emitting it, or by using some "tidy" code.如果不是,您需要更正您的输入,无论是在发出它的系统中,还是使用一些“整洁”的代码。

As a first step, make sure https://github.com/plutext/docx4j-ImportXHTML/blob/master/src/samples/java/org/docx4j/samples/ConvertInXHTMLFile.java can process it.作为第一步,确保https://github.com/plutext/docx4j-ImportXHTML/blob/master/src/samples/java/org/docx4j/samples/ConvertInXHTMLFile.java可以处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM