We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to get background color for a Run or Paragraph?

Hello,


I would like to get background color for a Run or a Paragraph into my Word document.

Could you please, say me if Aspose.Words has method to do this ?

Many thanks,
Cédric.B

Hi Cédric,


Thanks for your inquiry. Please use Shading.BackgroundPatternColor property to get or set the color that’s applied to the background of the Shading object. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + “in.docx”);<o:p></o:p>

Paragraph para = doc.FirstSection.Body.FirstParagraph;

//Get the color applied to the background of the Shading object.

Console.WriteLine(para.ParagraphFormat.Shading.BackgroundPatternColor);

Console.WriteLine(para.Runs[0].Font.Shading.BackgroundPatternColor);

Hi,


It seems that it is not working, the result is alway R=0 ; G=0 ; B=0

Could you please check my document in attachment and the code below ?

System.out.println(“BEGIN TEST background color”);
ArrayList paragraphs = new ArrayList<>();
Document documentHighlighted = new Document(Main.class.getClassLoader().getResourceAsStream(“detect_background_color.docx”));
NodeCollection nodes = documentHighlighted.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph : (Iterable) nodes) {
paragraphs.add(paragraph);
}
System.out.println(“Test background color - Paragraphs”);
for(int i = 0; i< paragraphs.size(); i++){
System.out.println("Line " + i + " : " +paragraphs.get(i).getParagraphFormat().getShading().getForegroundPatternColor());
}
System.out.println(“Test background color - Runs”);
for(int i = 0; i< paragraphs.size(); i++){
for(Run run : paragraphs.get(i).getRuns())
System.out.println("Line " + i + " : " +run.getFont().getShading().getForegroundPatternColor());
}
System.out.println(“END TEST background color”);

Console output :

BEGIN TEST background color
Test background color - Paragraphs
Line 0 : java.awt.Color[r=0,g=0,b=0]
Line 1 : java.awt.Color[r=0,g=0,b=0]
Line 2 : java.awt.Color[r=0,g=0,b=0]
Line 3 : java.awt.Color[r=0,g=0,b=0]
Line 4 : java.awt.Color[r=0,g=0,b=0]
Line 5 : java.awt.Color[r=0,g=0,b=0]
Line 6 : java.awt.Color[r=0,g=0,b=0]
Line 7 : java.awt.Color[r=0,g=0,b=0]
Test background color - Runs
Line 0 : java.awt.Color[r=0,g=0,b=0]
Line 0 : java.awt.Color[r=0,g=0,b=0]
Line 0 : java.awt.Color[r=0,g=0,b=0]
Line 2 : java.awt.Color[r=0,g=0,b=0]
Line 2 : java.awt.Color[r=0,g=0,b=0]
Line 2 : java.awt.Color[r=0,g=0,b=0]
Line 3 : java.awt.Color[r=0,g=0,b=0]
Line 4 : java.awt.Color[r=0,g=0,b=0]
Line 4 : java.awt.Color[r=0,g=0,b=0]
Line 4 : java.awt.Color[r=0,g=0,b=0]
Line 5 : java.awt.Color[r=0,g=0,b=0]
Line 6 : java.awt.Color[r=0,g=0,b=0]
Line 6 : java.awt.Color[r=0,g=0,b=0]
Line 6 : java.awt.Color[r=0,g=0,b=0]
Line 6 : java.awt.Color[r=0,g=0,b=0]
END TEST background color

The goal is to detect lines where the bachground is grey.

Many thanks for your support.
Hi Cédric,

Thanks for your inquiry. Please use Shading.Texture property to get or set the shading texture. In your document some paragraphs have shading texture. Please check the attached image for detail.

Please use the following code snippet to get the desired output. Hope this helps you.

for(int i = 0; i< paragraphs.size(); i++){
System.
out.println("Line " + i + " : " +paragraphs.get(i).getParagraphFormat().getShading().getTexture());
if(paragraphs.get(i).getParagraphFormat().getShading().getTexture() == TextureIndex.TEXTURE_10_PERCENT)
{
System.out.println("Texture10Percent is applied to paragraph");
}

}

It is working perfectly, good job thanks !