c docx html,java - Convert HTML to DOCX - Stack Overflow

My question is very specific and I hope that someone has done this conversion from HTMLto DOCX.

To do this I took a sample code from github and tried it in my local Eclipse Setup.

import java.io.File;

import java.io.FileNotFoundException;

import javax.xml.bind.JAXBException;

import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;

import org.docx4j.openpackaging.exceptions.Docx4JException;

import org.docx4j.openpackaging.exceptions.InvalidFormatException;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart;

public class HtmlToDocConvert {

/**

* @param args

* @throws FileNotFoundException

* @throws JAXBException

* @throws Docx4JException

*/

public static void main(String[] args) throws FileNotFoundException,

JAXBException, Docx4JException {

// TODO Auto-generated method stub

// File file = new File("C:\\TestWordToHtml\\html\\Test.html");

String inputfilepath = "C:\\TestWordToHtml\\html\\Test.html";

try {

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage

.createPackage();

NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();

wordMLPackage.getMainDocumentPart().addTargetPart(ndp);

ndp.unmarshalDefaultNumbering();

XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(

wordMLPackage);

xHTMLImporter.setHyperlinkStyle("Hyperlink");

wordMLPackage.getMainDocumentPart().getContent().addAll(

xHTMLImporter.convert(new File(inputfilepath), null));

File output = new java.io.File(System.getProperty("user.dir")

+ "/html_output.docx");

wordMLPackage.save(output);

System.out.println("done");

System.out.println("file path where it is stored is" + " "

+ output.getAbsolutePath());

}

catch (InvalidFormatException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Above code is giving me an error as follows

Exception in thread "main" java.lang.NoSuchMethodError: org.docx4j.org.xhtmlrenderer.docx.DocxRenderer.(Ljava/lang/String;)V

at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.getRenderer(XHTMLImporterImpl.java:252)

at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:466)

at HtmlToDocConvert.main(HtmlToDocConvert.java:41)

Jars in my projects to achieve this are as following.

docx4j-3.2.1.jar

docx4j-ImportXHTML-3.2.1.jar

slf4j-api-1.7.7.jar

slf4j-log4j12-1.7.7.jar

xhtmlrenderer-1.0.0.jar

log4j.jar

I have stripped the xhtmlrendere.jar file to view DOCRendered class and saw that there was no init method inside it.I have spent close to half a day to figure out this thing and I am not sure if this is correct way to do the conversion or this is even possible.

If someone has done this can he/she sent me correct xhtmlrenderer.jar file or anypother dependency to achieve this simple task.

Thanks in Advance

Regards,

Bhanu

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值