Search code examples
javaapache-poidocx4j

Is there a way how to disable spell checking for Word document in java?


I'm creating MS Word document using docx4j and in MS Word all texts are marked with spell checker as text is attributed as english but it is different language. Is there a way how to disable spell checking using docx4j or Apache POI?


Solution

  • Disclosure: I maintain docx4j

    When you use XHTMLImporter, you should be importing into a docx with suitable language settings.

    Typically this is done in the styles part, w:styles/w:docDefaults/w:rPrDefault/w:rPr:

    <w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:docDefaults>
        <w:rPrDefault>
          <w:rPr>
            <w:lang w:val="en-US" w:eastAsia="ko-KR" w:bidi="ar-SA"/>
          </w:rPr>
        </w:rPrDefault>
      </w:docDefaults>
    

    This value will be effective unless overridden in a style, or in the direct formatting which some of the other answers discuss.

    Also, in /word/settings.xml, check w:themeFontLang:

    <w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" >
      <w:themeFontLang w:val="en-US" w:eastAsia="ko-KR"/>
    

    You can either maintain per-language templates, or you can use docx4j to alter these settings dynamically. If you want to do that and have any trouble, please post a separate question.

    Regarding hiding spelling/grammar errors, assuming MainDocumentPart mdp:

    DocumentSettingsPart dsp = mdp.getDocumentSettingsPart();
    dsp.getContents().setHideGrammaticalErrors(new BooleanDefaultTrue());
    dsp.getContents().setHideSpellingErrors(new BooleanDefaultTrue());