Page 1 of 1

DocDefaults Fonts not applying

PostPosted: Wed Aug 17, 2016 5:48 am
by Roil
Hello,

I am working on upgrading docx4j from 3.0.0 to 3.3.0, and I am also updating docx4j-ImportXHTML-nightly-2013-1024 to docx4j-ImportXHTML-3.2.2. I did not change any of the code on my end, as I was hoping it would just work. My code just takes a large string of XHTML and converts it to Word.

After installing the jars, it looks like most of my code still works fine. The only issue I am having is that it is not applying the fonts correctly. I am trying to use Arial Unicode MS, but it keeps it at Times New Roman. I have tried a number of different solutions I've found online, but nothing seems to work. I would rather not change the default font in the source code, as I feel I'm pretty close already.

Here is the relevant part of my code.

Code: Select all
  var sdp = pkg.getMainDocumentPart().getStyleDefinitionsPart();
  var styles = sdp.getJaxbElement();
  var docDefaults = styles.getDocDefaults();
  var defaultRPr = docDefaults.getRPrDefault();
  var defaultPPr = docDefaults.getPPrDefault();
  var rPr = defaultRPr.getRPr();
  var pPr = defaultPPr.getPPr();

  var rFonts = rPr.getRFonts();
  if(rFonts == null) {
    rFonts = factory.createRFonts();
    rPr.setRFonts(rFonts);
  }

  var sz = factory.createHpsMeasure();
  var fontSize = new Packages.java.math.BigInteger(18);
  sz.setVal(fontSize);
  rPr.setSz(sz);

  rFonts.setAsciiTheme(null);
  rFonts.setEastAsiaTheme(null);
  rFonts.setHAnsiTheme(null);
  rFonts.setCstheme(null);
  rFonts.setAscii("Arial Unicode MS");
  rFonts.setEastAsia("Arial Unicode MS");
  rFonts.setHAnsi("Arial Unicode MS");
  rFonts.setCs("Arial Unicode MS");



After pulling the document and looking at the raw XML, I am noticing that the docDefaults is getting Arial Unicode MS properly, but it doesn't seem to be applying it to the document. Each of the individual rFonts on the rPr's are still getting set to Times New Roman. Is there something that I am missing here? Is there some new way that I have to force the docDefaults to apply?

Thanks in advance.

Re: DocDefaults Fonts not applying

PostPosted: Thu Aug 18, 2016 4:36 am
by Roil
I found docx4j-ImportXHML-3.3.0.jar, so I tried installing that to see if maybe that would help. Results are the same.

When I manually change the rFonts of each the rPr's in the raw XML of the word document to "Arial Unicode MS", it is changing the font I just need to apply the docDefaults rFonts to the entire document or prevent "Times New Roman" from being applied to all of the rPr's.

Re: DocDefaults Fonts not applying

PostPosted: Thu Aug 18, 2016 5:01 pm
by jason
I guess the following setting is overriding your DocDefaults: https://github.com/plutext/docx4j-Impor ... r.java#L32

To alter, you'll need to configure that in docx4j-ImportXHTML.properties; see https://github.com/plutext/docx4j-Impor ... s.java#L22

Otherwise, could you please post a sample string of XHTML?

Re: DocDefaults Fonts not applying

PostPosted: Thu Aug 18, 2016 11:28 pm
by Roil
I saw the use of the properties file in the code, but I was not sure where to place it. I tried dropping into the root directory of the jar, and it is now applying the correct font to the entire document.

Thanks for your help!