Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Feb 03, 2021 8:37 am

Hi,

I want to clear texture for all my document

I tried to use BackGroundColor and TextBackGroundColor for TextRange but only ranges with text are ok, everything else stays with grey.

In word document, we used : wdTexture10Percent, wdTexture30Percent

Thanks

My code :

Code: Select all
        public static void RemoveTexture(this Document docBlobRtf)
        {
            docBlobRtf.Background.Type = BackgroundType.Gradient;
            docBlobRtf.Background.Gradient.Color1 = Color.White;
            docBlobRtf.Background.Gradient.Color2 = Color.White;
            docBlobRtf.Background.Color = Color.White;
            foreach (Section section in docBlobRtf.Sections)
            {
                foreach (Paragraph para in section.Paragraphs)
                {
                    for (int i = 0; i < para.ChildObjects.Count; i++)
                    {
                        if (para.ChildObjects[i].DocumentObjectType == DocumentObjectType.TextRange)
                        {
                            (para.ChildObjects[i] as TextRange).CharacterFormat.TextBackgroundColor = Color.White;
                            (para.ChildObjects[i] as TextRange).CharacterFormat.HighlightColor = Color.White;
                        }
                    }
                    para.Format.BackColor = Color.White;
                }
            }
        }

alexdocpdf
 
Posts: 2
Joined: Tue Feb 02, 2021 6:28 pm

Wed Feb 03, 2021 9:28 am

Hello,

Thanks for your inquiry.
Please refer to the following code clear the texture or background color. If there are still any issues, please provide your source Word file to help us investigate further. You could send it to us ([email protected]) via email.
Code: Select all
        public static void RemoveTexture(Document docBlobRtf)
        {
            docBlobRtf.Background.Type = BackgroundType.NoBackground;
            //docBlobRtf.Background.Gradient.Color1 = Color.White;
            //docBlobRtf.Background.Gradient.Color2 = Color.White;
            //docBlobRtf.Background.Color = Color.White;
            foreach (Section section in docBlobRtf.Sections)
            {
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        RemoveParagraphBgc(obj as Paragraph);
                    }
                    if (obj is Table)
                    {
                        Table table = obj as Table;
                        foreach (TableRow row in table.Rows)
                        {
                            row.RowFormat.ClearBackground();
                            foreach (TableCell cell in row.Cells)
                            {
                                cell.CellFormat.ClearBackground();
                                foreach (Paragraph para in cell.Paragraphs)
                                {
                                    RemoveParagraphBgc(para);
                                }
                            }
                        }
                    }
                   
                }
            }
            foreach (TextBox textbox in docBlobRtf.TextBoxes)
            {
                textbox.Format.FillColor = Color.Empty;
                foreach (DocumentObject cobj in textbox.ChildObjects)
                {
                    if (cobj is Paragraph)
                    {
                        RemoveParagraphBgc(cobj as Paragraph);
                    }
                }               
            }
        }

        public static void RemoveParagraphBgc(Paragraph para)
        {
            for (int i = 0; i < para.ChildObjects.Count; i++)
            {
                if (para.ChildObjects[i].DocumentObjectType == DocumentObjectType.TextRange)
                {
                    (para.ChildObjects[i] as TextRange).CharacterFormat.TextBackgroundColor = Color.Empty;
                    (para.ChildObjects[i] as TextRange).CharacterFormat.HighlightColor = Color.Empty;
                }
            }
            para.Format.BackColor = Color.Empty;
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Feb 03, 2021 10:17 am

I tried your solution but it doesn't change background, it stays grey

Also I have an unknown method : ClearBackground() on RowFormat and CellFormat

Spire.Doc Version : 7.4.6.4040

Sorry, I cannot provide the real Word file because it's very sensible content (health data)
But I provide a "lorem ipsum" doc with two grey backgrounds

Thanks

Regards

Alexandre

alexdocpdf
 
Posts: 2
Joined: Tue Feb 02, 2021 6:28 pm

Thu Feb 04, 2021 2:26 am

Hello,

Thanks for your response and your sharing.
For your file, please refer to the following modified code. Besides, sorry that Spire.Doc v7.4.6 doesn't support the method "ClearBackground()". Please download the latest Spire.Doc Pack(hot fix) Version:9.2.0 for testing.
Code: Select all
        public static void RemoveTexture(Document docBlobRtf)
        {
            docBlobRtf.Background.Type = BackgroundType.NoBackground;
            foreach (Section section in docBlobRtf.Sections)
            {
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        RemoveParagraphBgc(obj as Paragraph);
                    }
                    if (obj is Table)
                    {
                        Table table = obj as Table;
                        foreach (TableRow row in table.Rows)
                        {
                            row.RowFormat.ClearBackground();
                            foreach (TableCell cell in row.Cells)
                            {
                                cell.CellFormat.ClearBackground();
                                foreach (Paragraph para in cell.Paragraphs)
                                {
                                    RemoveParagraphBgc(para);
                                }
                            }
                        }
                    }
                   
                }
            }
            foreach (TextBox textbox in docBlobRtf.TextBoxes)
            {
                textbox.Format.FillColor = Color.Empty;
                foreach (DocumentObject cobj in textbox.ChildObjects)
                {
                    if (cobj is Paragraph)
                    {
                        RemoveParagraphBgc(cobj as Paragraph);
                    }
                }               
            }
        }

        public static void RemoveParagraphBgc(Paragraph para)
        {
            for (int i = 0; i < para.ChildObjects.Count; i++)
            {
                if (para.ChildObjects[i].DocumentObjectType == DocumentObjectType.TextRange)
                {
                    (para.ChildObjects[i] as TextRange).CharacterFormat.TextBackgroundColor = Color.Empty;
                    (para.ChildObjects[i] as TextRange).CharacterFormat.HighlightColor = Color.Empty;
                }
            }
            //!!!clear the background for paragraph
            para.Format.ClearBackground();
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Sat Feb 20, 2021 1:44 am

Hello,

Hope you are doing well.
Has your issue been resolved? Any feedback will be greatly appreciated!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc