【问题标题】:Convert HTML code to Word Using docx4j Image is not embedded in Word document使用 docx4j 将 HTML 代码转换为 Word 图像未嵌入 Word 文档中
【发布时间】:2015-02-23 14:21:57
【问题描述】:

示例程序...

import java.io.IOException;
import org.docx4j.Docx4jProperties;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.model.structure.PageSizePaper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTAltChunk;

public class HtmlToDoc {
    public static void main(String[] args) throws Docx4JException {
        String html="", s="", filepath="E://HtmlToDoc//";

        try {

            String html = "<html><head><title>Import me</title></head><body><p>Hello World! Sample Program</p><img src="E:/HtmlToDoc/LOGO.JPEG"/></body></html>";

            Docx4jProperties.getProperties().setProperty("docx4j.PageSize", "B4JIS");
            String papersize= Docx4jProperties.getProperties().getProperty("docx4j.PageSize", "B4JIS");
            String landscapeString = Docx4jProperties.getProperties().getProperty("docx4j.PageOrientationLandscape", "true");
            boolean landscape= Boolean.parseBoolean(landscapeString);

            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.valueOf(papersize), landscape);
            AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));

            afiPart.setBinaryData(html.getBytes());
            //afiPart.setBinaryData(fileContent);

            afiPart.setContentType(new ContentType("text/html"));
            Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);

            // .. the bit in document body
            CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
            ac.setId(altChunkRel.getId() );
            wordMLPackage.getMainDocumentPart().addObject(ac);

            // .. content type
            wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
            wordMLPackage.save(new java.io.File("E://HtmlToDoc//" + "test.docx"));

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

}

这在我的本地机器上工作正常。但是我将此代码移到了我的word文档中的服务器图像未嵌入但我提供了正确的图像路径[当我在服务器中将HTML转换为PDF时,相同的图像路径工作正常]。在服务器 [linux 机器和 IBM websphere App Server 和 ApacheWeb 服务器] 中运行时缺少图像的原因可能是什么。即使我所有的路径(word 文档、图像、html 文档)都是相同的。

【问题讨论】:

    标签: html image jpeg docx docx4j


    【解决方案1】:

    您的代码依赖 Word 将 altChunk 转换为 HTML,因此,如果您在本地计算机上打开 Word 文档,它将无法在 E:/HtmlToDoc/LOGO.JPEG 上看到图像服务器。

    您可以使用 URL 或数据 URI。

    或者,使用 docx4j-ImportXHTML,它会在不向 Word 留下任何内容的情况下进行转换。

    【讨论】:

    • 我使用的相对路径图像嵌入在 word 中,但是当我下载图像时,图像丢失,但是当我将 word 文档从服务器复制到本地图像时,word 文档中存在。我尝试了另一种方法数据 URI,我得到的是小十字标记而不是图像。我在哪里做错代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2023-02-24
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多