Reputation: 395
I am using docx4j to convert html strings to docx.
Below is the code.
package docx4j;
import org.docx4j.convert.in.xhtml.FormattingOption;
import org.docx4j.convert.in.xhtml.XHTMLImporter;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class ConvertHTMLToDoc {
public static void main(String[] args) throws Docx4JException {
String outputfilepath = "style-example-OUT30.rtf";
String text = getHTMLString();
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporter xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
/* xHTMLImporter.setParagraphFormatting(FormattingOption.CLASS_PLUS_OTHER);
xHTMLImporter.setRunFormatting(FormattingOption.CLASS_PLUS_OTHER);
*/
wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(text, null));
/* wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Testing Title");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle", "Testing Subtitle");
*/
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
}
private static String getHTMLString() {
String text = "<html><head><title></title></head><body>" + "<p class=\"Title\">Testing Title</p>"
+ "<p class=\"Subtitle\">Testing Subtitle</p>" + "</body></html>";
return text;
}
}
When I run this, I get the error at the following line.
wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(text, null));
Below is the error message.
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) java.lang.NullPointerException
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.jaxb.NamespacePrefixMapperUtils.setProperty(NamespacePrefixMapperUtils.java:155)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:588)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:559)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart.createVirtualStylesForDocDefaults(StyleDefinitionsPart.java:369)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getStyleTree(MainDocumentPart.java:172)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getStyleTree(MainDocumentPart.java:161)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.stylesToCSS(XHTMLImporterImpl.java:446)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.getRenderer(XHTMLImporterImpl.java:253)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:645)
What could be the reason for the null pointer exception?
Upvotes: 1
Views: 1781
Reputation: 3046
There are 2 possibly ways to do this problem:
Less elegant way( You need to create this class in your project. Remember that package com.sun.xml.internal.bind.marshaller
name is needed for docx4j. You cannot change that):
package com.sun.xml.internal.bind.marshaller;
public abstract class NamespacePrefixMapper {
private static final String[] EMPTY_STRING = new String[0];
public abstract String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix);
public String[] getPreDeclaredNamespaceUris() {
return EMPTY_STRING;
}
public String[] getPreDeclaredNamespaceUris2() {
return EMPTY_STRING;
}
public String[] getContextualNamespaceDecls() {
return EMPTY_STRING;
}
}
More elegant way but i have no idea if it could work on JBOSS 6.2 since it's for version 7: Docx4j and JBoss 7
Upvotes: 1