Ok, I found example on how to register font:
- Code: Select all
// Setup font mapping
RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
rfonts.setAscii("Century Gothic");
XHTMLImporterImpl.addFontMapping("Century Gothic", rfonts);
EDIT:
Ok, now it works - turned out that I had
- Code: Select all
xHTMLImporter.setParagraphFormatting(FormattingOption.IGNORE_CLASS);
plus some unnecessary lines. So the code in the end looks like this:
- Code: Select all
String regex = null;
// Windows:
// String
// regex=".*(calibri|camb|cour|arial|symb|times|Times|zapf).*";
regex = ".*(arial|times).*";
// Mac
// String
// regex=".*(Courier New|Arial|Times New Roman|Comic Sans|Georgia|Impact|Lucida Console|Lucida Sans Unicode|Palatino Linotype|Tahoma|Trebuchet|Verdana|Symbol|Webdings|Wingdings|MS Sans Serif|MS Serif).*";
PhysicalFonts.setRegex(regex);
// Document loading (required)
WordprocessingMLPackage wordMLPackage;
System.out.println("Loading file from " + inputfilepath);
// wordMLPackage = Docx4J.load(new java.io.File(inputfilepath));
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setImageDirPath(inputfilepath + "_files");
htmlSettings.setImageTargetUri(inputfilepath
.substring(inputfilepath.lastIndexOf("/") + 1) + "_files");
String userCSS = "html, body, div, span, h1, h2, h3, h4, h5, h6, p, a, img, ol, ul, li, table, caption, tbody, tfoot, thead, tr, th, td "
+ "{ margin: 0; padding: 0; border: 0;}"
+ "body {line-height: 1;} ";
htmlSettings.setUserCSS(userCSS);
wordMLPackage = WordprocessingMLPackage.createPackage();
RFonts arialRFonts = Context.getWmlObjectFactory().createRFonts();
arialRFonts.setAscii("Arial");
arialRFonts.setHint(org.docx4j.wml.STHint.DEFAULT);
arialRFonts.setHAnsi("Arial");
XHTMLImporterImpl.addFontMapping("Arial", arialRFonts);
RFonts timesRFonts = Context.getWmlObjectFactory().createRFonts();
timesRFonts.setAscii("Times");
timesRFonts.setHint(org.docx4j.wml.STHint.DEFAULT);
timesRFonts.setHAnsi("Times");
XHTMLImporterImpl.addFontMapping("Times New Roman", timesRFonts);
XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(
wordMLPackage);
xHTMLImporter.setHyperlinkStyle("Hyperlink");
// xHTMLImporter.setParagraphFormatting(FormattingOption.IGNORE_CLASS);
wordMLPackage.getDocumentModel().getSections().get(0)
.getPageDimensions().setPgSize(PageSizePaper.A4, true);
wordMLPackage.getDocumentModel().getSections().get(0)
.getPageDimensions().setMargins(MarginsWellKnown.NARROW);
wordMLPackage.getMainDocumentPart().getContent()
.addAll(xHTMLImporter.convert(tempHtmlFile, null));
wordMLPackage.save(tempDocxFile);
System.out.println("Saved: " + "report.docx");