1

For example in the run where the sentence is:

"My name is something and i do something."

I want to apply different colour to word "name"and "something". How can I do this?

Ramsha Khan
  • 99
  • 1
  • 8
  • 2
    Each string part which shall be styled separately must be in its own run. This is because only runs can be styled. So you must split this run into multiple ones. – Axel Richter Oct 30 '16 at 08:41
  • @AxelRichter thanks a lot. I got how to do it (splitting the runs) by your reply in my other post. Thank you! – Ramsha Khan Oct 30 '16 at 11:48
  • For anyone who is looking for how to split the run, refer to the example [here](http://stackoverflow.com/questions/40318507/how-do-i-change-color-of-a-particular-word-document-using-apache-poi) given by @AxelRichter – Ramsha Khan Oct 30 '16 at 11:54
  • Glad I could help you. But your other post was about splitting according to a single character. This is totally different from splitting according to words or possibly even parts of words. The latter will be much more difficult since the words or parts of words could be in different runs already. A character cannot be splitted into different runs already. Microsoft Word is a monster in those things ;-). – Axel Richter Oct 30 '16 at 11:57
  • actually my main target is to split according to single character. But i was just wondering if its possible to split according to words as well. The main challenge for me was splitting the run, but now that i know how to do it, i think i play around with the code and get the solution for split according to words or part of words. All thanks to you! @AxelRichter – Ramsha Khan Oct 30 '16 at 12:17

1 Answers1

3

You can't! Due to the way that the .doc and .docx formats work, everything in a given run must have the same formatting

A given paragraph may contain any number of runs, each with their own differing stylings/formattings. So, you need to split that one run into multiple ones, and style the different new runs as required

Gagravarr
  • 47,320
  • 10
  • 111
  • 156