Apache POI doesn't find highlighted text












0














I have a file saved in doc format, and I need to extract highlighted text.
I have code like in following:



HWPFDocument document = new HWPFDocument(fis);
Range r = document.getRange();
for (int i=0;i<5;i++) {
CharacterRun t = r.getCharacterRun(i);
System.out.println(t.isHighlighted());
System.out.println(t.getHighlightedColor());
System.out.println(r.getCharacterRun(i).SPRM_HIGHLIGHT);
System.out.println(r.getCharacterRun(i));
}


None of the above methods show that text is highlighted, but when I open it, it is highlighted.
What can be the reason, and how to find if the text is highlighted or not?










share|improve this question




















  • 1




    Is really the character run highlighted as described in support.office.com/en-us/article/…? Or is there applied shading on a word or paragraph as described in support.office.com/en-us/article/…?
    – Axel Richter
    Nov 21 '18 at 6:23










  • I do not really how was it highlighted, but for example if I want to detect both of them?
    – ardakshalkar
    Nov 21 '18 at 11:31
















0














I have a file saved in doc format, and I need to extract highlighted text.
I have code like in following:



HWPFDocument document = new HWPFDocument(fis);
Range r = document.getRange();
for (int i=0;i<5;i++) {
CharacterRun t = r.getCharacterRun(i);
System.out.println(t.isHighlighted());
System.out.println(t.getHighlightedColor());
System.out.println(r.getCharacterRun(i).SPRM_HIGHLIGHT);
System.out.println(r.getCharacterRun(i));
}


None of the above methods show that text is highlighted, but when I open it, it is highlighted.
What can be the reason, and how to find if the text is highlighted or not?










share|improve this question




















  • 1




    Is really the character run highlighted as described in support.office.com/en-us/article/…? Or is there applied shading on a word or paragraph as described in support.office.com/en-us/article/…?
    – Axel Richter
    Nov 21 '18 at 6:23










  • I do not really how was it highlighted, but for example if I want to detect both of them?
    – ardakshalkar
    Nov 21 '18 at 11:31














0












0








0







I have a file saved in doc format, and I need to extract highlighted text.
I have code like in following:



HWPFDocument document = new HWPFDocument(fis);
Range r = document.getRange();
for (int i=0;i<5;i++) {
CharacterRun t = r.getCharacterRun(i);
System.out.println(t.isHighlighted());
System.out.println(t.getHighlightedColor());
System.out.println(r.getCharacterRun(i).SPRM_HIGHLIGHT);
System.out.println(r.getCharacterRun(i));
}


None of the above methods show that text is highlighted, but when I open it, it is highlighted.
What can be the reason, and how to find if the text is highlighted or not?










share|improve this question















I have a file saved in doc format, and I need to extract highlighted text.
I have code like in following:



HWPFDocument document = new HWPFDocument(fis);
Range r = document.getRange();
for (int i=0;i<5;i++) {
CharacterRun t = r.getCharacterRun(i);
System.out.println(t.isHighlighted());
System.out.println(t.getHighlightedColor());
System.out.println(r.getCharacterRun(i).SPRM_HIGHLIGHT);
System.out.println(r.getCharacterRun(i));
}


None of the above methods show that text is highlighted, but when I open it, it is highlighted.
What can be the reason, and how to find if the text is highlighted or not?







java apache-poi highlight doc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 12:18









Axel Richter

24.6k21935




24.6k21935










asked Nov 21 '18 at 3:52









ardakshalkar

3101317




3101317








  • 1




    Is really the character run highlighted as described in support.office.com/en-us/article/…? Or is there applied shading on a word or paragraph as described in support.office.com/en-us/article/…?
    – Axel Richter
    Nov 21 '18 at 6:23










  • I do not really how was it highlighted, but for example if I want to detect both of them?
    – ardakshalkar
    Nov 21 '18 at 11:31














  • 1




    Is really the character run highlighted as described in support.office.com/en-us/article/…? Or is there applied shading on a word or paragraph as described in support.office.com/en-us/article/…?
    – Axel Richter
    Nov 21 '18 at 6:23










  • I do not really how was it highlighted, but for example if I want to detect both of them?
    – ardakshalkar
    Nov 21 '18 at 11:31








1




1




Is really the character run highlighted as described in support.office.com/en-us/article/…? Or is there applied shading on a word or paragraph as described in support.office.com/en-us/article/…?
– Axel Richter
Nov 21 '18 at 6:23




Is really the character run highlighted as described in support.office.com/en-us/article/…? Or is there applied shading on a word or paragraph as described in support.office.com/en-us/article/…?
– Axel Richter
Nov 21 '18 at 6:23












I do not really how was it highlighted, but for example if I want to detect both of them?
– ardakshalkar
Nov 21 '18 at 11:31




I do not really how was it highlighted, but for example if I want to detect both of them?
– ardakshalkar
Nov 21 '18 at 11:31












1 Answer
1






active

oldest

votes


















1














Highlighting text in Word is possible using two different methods. First is applying highlighting to text runs. Second is applying shading to words or paragraphs.



For the first and using *.doc, the Word binary file format, apache poi provides methods in CharacterRun. For the second apache poi provides Paragraph.getShading. But this is only set if the shading applies to the whole paragraph. If the shading is applied only to single runs, then apache poi provides nothing for that. So using the underlying SprmOperations is needed.



Microsoft's documentation 2.6.1 Character Properties describes sprmCShd80 (0x4866) which is "A Shd80 structure that specifies the background shading for the text.". So we need searching for that.



Example:



import java.io.FileInputStream;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;

import org.apache.poi.hwpf.sprm.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class HWPFInspectBgColor {

private static void showCharacterRunInternals(CharacterRun run) throws Exception {
Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
_chpx.setAccessible(true);
SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
SprmOperation sprmOperation = sprmIterator.next();
System.out.println(sprmOperation);
}
}

static SprmOperation getCharacterRunShading(CharacterRun run) throws Exception {
SprmOperation shd80Operation = null;
Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
_chpx.setAccessible(true);
Field _value = SprmOperation.class.getDeclaredField("_value");
_value.setAccessible(true);
SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
SprmOperation sprmOperation = sprmIterator.next();
short sprmValue = (short)_value.get(sprmOperation);
if (sprmValue == (short)0x4866) { // we have a Shd80 structure, see https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx
shd80Operation = sprmOperation;
}
}
return shd80Operation;
}

public static void main(String args) throws Exception {
HWPFDocument document = new HWPFDocument(new FileInputStream("sample.doc"));
Range range = document.getRange();
for (int p = 0; p < range.numParagraphs(); p++) {
Paragraph paragraph = range.getParagraph(p);
System.out.println(paragraph);
if (!paragraph.getShading().isEmpty()) {
System.out.println("Paragraph's shading: " + paragraph.getShading());
}

for (int r = 0; r < paragraph.numCharacterRuns(); r++) {
CharacterRun run = paragraph.getCharacterRun(r);
System.out.println(run);
if (run.isHighlighted()) {
System.out.println("Run's highlighted color: " + run.getHighlightedColor());
}
if (getCharacterRunShading(run) != null) {
System.out.println("Run's Shd80 structure: " + getCharacterRunShading(run));
}
}
}
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405023%2fapache-poi-doesnt-find-highlighted-text%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Highlighting text in Word is possible using two different methods. First is applying highlighting to text runs. Second is applying shading to words or paragraphs.



    For the first and using *.doc, the Word binary file format, apache poi provides methods in CharacterRun. For the second apache poi provides Paragraph.getShading. But this is only set if the shading applies to the whole paragraph. If the shading is applied only to single runs, then apache poi provides nothing for that. So using the underlying SprmOperations is needed.



    Microsoft's documentation 2.6.1 Character Properties describes sprmCShd80 (0x4866) which is "A Shd80 structure that specifies the background shading for the text.". So we need searching for that.



    Example:



    import java.io.FileInputStream;

    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.usermodel.*;

    import org.apache.poi.hwpf.sprm.*;

    import java.lang.reflect.Field;
    import java.lang.reflect.Method;

    public class HWPFInspectBgColor {

    private static void showCharacterRunInternals(CharacterRun run) throws Exception {
    Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
    _chpx.setAccessible(true);
    SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
    for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
    SprmOperation sprmOperation = sprmIterator.next();
    System.out.println(sprmOperation);
    }
    }

    static SprmOperation getCharacterRunShading(CharacterRun run) throws Exception {
    SprmOperation shd80Operation = null;
    Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
    _chpx.setAccessible(true);
    Field _value = SprmOperation.class.getDeclaredField("_value");
    _value.setAccessible(true);
    SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
    for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
    SprmOperation sprmOperation = sprmIterator.next();
    short sprmValue = (short)_value.get(sprmOperation);
    if (sprmValue == (short)0x4866) { // we have a Shd80 structure, see https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx
    shd80Operation = sprmOperation;
    }
    }
    return shd80Operation;
    }

    public static void main(String args) throws Exception {
    HWPFDocument document = new HWPFDocument(new FileInputStream("sample.doc"));
    Range range = document.getRange();
    for (int p = 0; p < range.numParagraphs(); p++) {
    Paragraph paragraph = range.getParagraph(p);
    System.out.println(paragraph);
    if (!paragraph.getShading().isEmpty()) {
    System.out.println("Paragraph's shading: " + paragraph.getShading());
    }

    for (int r = 0; r < paragraph.numCharacterRuns(); r++) {
    CharacterRun run = paragraph.getCharacterRun(r);
    System.out.println(run);
    if (run.isHighlighted()) {
    System.out.println("Run's highlighted color: " + run.getHighlightedColor());
    }
    if (getCharacterRunShading(run) != null) {
    System.out.println("Run's Shd80 structure: " + getCharacterRunShading(run));
    }
    }
    }
    }
    }





    share|improve this answer


























      1














      Highlighting text in Word is possible using two different methods. First is applying highlighting to text runs. Second is applying shading to words or paragraphs.



      For the first and using *.doc, the Word binary file format, apache poi provides methods in CharacterRun. For the second apache poi provides Paragraph.getShading. But this is only set if the shading applies to the whole paragraph. If the shading is applied only to single runs, then apache poi provides nothing for that. So using the underlying SprmOperations is needed.



      Microsoft's documentation 2.6.1 Character Properties describes sprmCShd80 (0x4866) which is "A Shd80 structure that specifies the background shading for the text.". So we need searching for that.



      Example:



      import java.io.FileInputStream;

      import org.apache.poi.hwpf.HWPFDocument;
      import org.apache.poi.hwpf.usermodel.*;

      import org.apache.poi.hwpf.sprm.*;

      import java.lang.reflect.Field;
      import java.lang.reflect.Method;

      public class HWPFInspectBgColor {

      private static void showCharacterRunInternals(CharacterRun run) throws Exception {
      Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
      _chpx.setAccessible(true);
      SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
      for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
      SprmOperation sprmOperation = sprmIterator.next();
      System.out.println(sprmOperation);
      }
      }

      static SprmOperation getCharacterRunShading(CharacterRun run) throws Exception {
      SprmOperation shd80Operation = null;
      Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
      _chpx.setAccessible(true);
      Field _value = SprmOperation.class.getDeclaredField("_value");
      _value.setAccessible(true);
      SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
      for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
      SprmOperation sprmOperation = sprmIterator.next();
      short sprmValue = (short)_value.get(sprmOperation);
      if (sprmValue == (short)0x4866) { // we have a Shd80 structure, see https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx
      shd80Operation = sprmOperation;
      }
      }
      return shd80Operation;
      }

      public static void main(String args) throws Exception {
      HWPFDocument document = new HWPFDocument(new FileInputStream("sample.doc"));
      Range range = document.getRange();
      for (int p = 0; p < range.numParagraphs(); p++) {
      Paragraph paragraph = range.getParagraph(p);
      System.out.println(paragraph);
      if (!paragraph.getShading().isEmpty()) {
      System.out.println("Paragraph's shading: " + paragraph.getShading());
      }

      for (int r = 0; r < paragraph.numCharacterRuns(); r++) {
      CharacterRun run = paragraph.getCharacterRun(r);
      System.out.println(run);
      if (run.isHighlighted()) {
      System.out.println("Run's highlighted color: " + run.getHighlightedColor());
      }
      if (getCharacterRunShading(run) != null) {
      System.out.println("Run's Shd80 structure: " + getCharacterRunShading(run));
      }
      }
      }
      }
      }





      share|improve this answer
























        1












        1








        1






        Highlighting text in Word is possible using two different methods. First is applying highlighting to text runs. Second is applying shading to words or paragraphs.



        For the first and using *.doc, the Word binary file format, apache poi provides methods in CharacterRun. For the second apache poi provides Paragraph.getShading. But this is only set if the shading applies to the whole paragraph. If the shading is applied only to single runs, then apache poi provides nothing for that. So using the underlying SprmOperations is needed.



        Microsoft's documentation 2.6.1 Character Properties describes sprmCShd80 (0x4866) which is "A Shd80 structure that specifies the background shading for the text.". So we need searching for that.



        Example:



        import java.io.FileInputStream;

        import org.apache.poi.hwpf.HWPFDocument;
        import org.apache.poi.hwpf.usermodel.*;

        import org.apache.poi.hwpf.sprm.*;

        import java.lang.reflect.Field;
        import java.lang.reflect.Method;

        public class HWPFInspectBgColor {

        private static void showCharacterRunInternals(CharacterRun run) throws Exception {
        Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
        _chpx.setAccessible(true);
        SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
        for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
        SprmOperation sprmOperation = sprmIterator.next();
        System.out.println(sprmOperation);
        }
        }

        static SprmOperation getCharacterRunShading(CharacterRun run) throws Exception {
        SprmOperation shd80Operation = null;
        Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
        _chpx.setAccessible(true);
        Field _value = SprmOperation.class.getDeclaredField("_value");
        _value.setAccessible(true);
        SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
        for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
        SprmOperation sprmOperation = sprmIterator.next();
        short sprmValue = (short)_value.get(sprmOperation);
        if (sprmValue == (short)0x4866) { // we have a Shd80 structure, see https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx
        shd80Operation = sprmOperation;
        }
        }
        return shd80Operation;
        }

        public static void main(String args) throws Exception {
        HWPFDocument document = new HWPFDocument(new FileInputStream("sample.doc"));
        Range range = document.getRange();
        for (int p = 0; p < range.numParagraphs(); p++) {
        Paragraph paragraph = range.getParagraph(p);
        System.out.println(paragraph);
        if (!paragraph.getShading().isEmpty()) {
        System.out.println("Paragraph's shading: " + paragraph.getShading());
        }

        for (int r = 0; r < paragraph.numCharacterRuns(); r++) {
        CharacterRun run = paragraph.getCharacterRun(r);
        System.out.println(run);
        if (run.isHighlighted()) {
        System.out.println("Run's highlighted color: " + run.getHighlightedColor());
        }
        if (getCharacterRunShading(run) != null) {
        System.out.println("Run's Shd80 structure: " + getCharacterRunShading(run));
        }
        }
        }
        }
        }





        share|improve this answer












        Highlighting text in Word is possible using two different methods. First is applying highlighting to text runs. Second is applying shading to words or paragraphs.



        For the first and using *.doc, the Word binary file format, apache poi provides methods in CharacterRun. For the second apache poi provides Paragraph.getShading. But this is only set if the shading applies to the whole paragraph. If the shading is applied only to single runs, then apache poi provides nothing for that. So using the underlying SprmOperations is needed.



        Microsoft's documentation 2.6.1 Character Properties describes sprmCShd80 (0x4866) which is "A Shd80 structure that specifies the background shading for the text.". So we need searching for that.



        Example:



        import java.io.FileInputStream;

        import org.apache.poi.hwpf.HWPFDocument;
        import org.apache.poi.hwpf.usermodel.*;

        import org.apache.poi.hwpf.sprm.*;

        import java.lang.reflect.Field;
        import java.lang.reflect.Method;

        public class HWPFInspectBgColor {

        private static void showCharacterRunInternals(CharacterRun run) throws Exception {
        Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
        _chpx.setAccessible(true);
        SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
        for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
        SprmOperation sprmOperation = sprmIterator.next();
        System.out.println(sprmOperation);
        }
        }

        static SprmOperation getCharacterRunShading(CharacterRun run) throws Exception {
        SprmOperation shd80Operation = null;
        Field _chpx = CharacterRun.class.getDeclaredField("_chpx");
        _chpx.setAccessible(true);
        Field _value = SprmOperation.class.getDeclaredField("_value");
        _value.setAccessible(true);
        SprmBuffer sprmBuffer = (SprmBuffer) _chpx.get(run);
        for (SprmIterator sprmIterator = sprmBuffer.iterator(); sprmIterator.hasNext(); ) {
        SprmOperation sprmOperation = sprmIterator.next();
        short sprmValue = (short)_value.get(sprmOperation);
        if (sprmValue == (short)0x4866) { // we have a Shd80 structure, see https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx
        shd80Operation = sprmOperation;
        }
        }
        return shd80Operation;
        }

        public static void main(String args) throws Exception {
        HWPFDocument document = new HWPFDocument(new FileInputStream("sample.doc"));
        Range range = document.getRange();
        for (int p = 0; p < range.numParagraphs(); p++) {
        Paragraph paragraph = range.getParagraph(p);
        System.out.println(paragraph);
        if (!paragraph.getShading().isEmpty()) {
        System.out.println("Paragraph's shading: " + paragraph.getShading());
        }

        for (int r = 0; r < paragraph.numCharacterRuns(); r++) {
        CharacterRun run = paragraph.getCharacterRun(r);
        System.out.println(run);
        if (run.isHighlighted()) {
        System.out.println("Run's highlighted color: " + run.getHighlightedColor());
        }
        if (getCharacterRunShading(run) != null) {
        System.out.println("Run's Shd80 structure: " + getCharacterRunShading(run));
        }
        }
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 12:17









        Axel Richter

        24.6k21935




        24.6k21935






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405023%2fapache-poi-doesnt-find-highlighted-text%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin