Page 1 of 1

remove highlight from whole document

PostPosted: Tue Jun 21, 2022 9:35 pm
by last61474@gmail.com
Hi,

Is it possible to remove a specific color highlight from a docx file? For example, change gray highlight to no color if exists in docx.

Thanks for any help.

Re: remove highlight from whole document

PostPosted: Wed Jun 22, 2022 10:12 am
by jason
Sure. Note that there are at least 3 ways content can be highlighted.

1. run level shading: eg <w:highlight w:val="yellow"/>

2. paragraph level: eg <w:shd w:val="clear" w:color="auto" w:fill="FF0000"/>

3. table cell shading.

In addition, this can be applied directly to the relevant content, or indirectly via the style hierarchy.

Also, besides the main document part, your highlight could be in headers/footers, footnotes/endnotes.

So your code will depend on where you expect to find the highlight.

https://github.com/plutext/docx4j/blob/ ... anish.java will give you an idea of how to remove directly applied formatting from the main document part.

If the highlight is via styles, you might find it easier to override it with direct formatting, rather than remove it.

Some sample content:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p >
      <w:r>
        <w:t xml:space="preserve">Some </w:t>
      </w:r>
      <w:r >
        <w:rPr>
          <w:highlight w:val="yellow"/>
        </w:rPr>
        <w:t>character</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve"> shading</w:t>
      </w:r>
    </w:p>
    <w:p >
      <w:pPr>
        <w:shd w:val="clear" w:color="auto" w:fill="FF0000"/>
      </w:pPr>
      <w:r>
        <w:t>Paragraph shading</w:t>
      </w:r>
    </w:p>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4