Color applied for BAckground is not applied for all

I am trying to add background color to this document but not applying for all content

Input.docx (17.6 KB)

Shading shading = paragraph.ParagraphFormat.Shading;
shading.Texture = TextureIndex.TextureDiagonalCross;
shading.BackgroundPatternColor = System.Drawing.Color.Ivory;
shading.ForegroundPatternColor = System.Drawing.Color.FromArgb(255, 250, 205);

Output
OutPut.docx (368.2 KB)

I want to add background for all

@Raghul214 The problem occurs because there are Run nodes with explicitly set white background. Please try resetting it:

Document doc = new Document(@"C:\Temp\in.docx");
// Set page color.
doc.PageColor = System.Drawing.Color.FromArgb(255, 250, 205);
// Reset explicitely set shading of runs.
doc.GetChildNodes(NodeType.Run, true).Cast<Run>().ToList()
    .ForEach(r => r.Font.Shading.ClearFormatting());
doc.Save(@"C:\Temp\out.docx");

Will this work for all case like table image …?

@Raghul214 No, this will not work in all possible cases, for example for images with opaque background.

What should be the solution for all case…?

@Raghul214 I am afraid, there is no general solution for all possible cases.

Is there a way wherein I can highlight the entire body of a document

@Raghul214 The above suggested code does exactly this.

1 Like