Set Transparent Color for Images in Word in C#

2018-05-07 08:36:11 Written by  support iceblue
Rate this item
(0 votes)

This tutorial is going to show you how to make a color of a image transparent using Spire.Doc.

Below screenshot shows an example image with black and white colors:

Set transparent Color for Images in Word in C#

Detail steps:

Step 1: Instantiate a Document object and Load the Word file.

Document doc = new Document();
doc.LoadFromFile("Input.docx");

Step 2: Get the first Paragraph in the first section.

Paragraph paragraph = doc.Sections[0].Paragraphs[0];

Step 3: Set the black color of the image(s) in the paragraph to transperant.

foreach (DocumentObject obj in paragraph.ChildObjects)
{
    if (obj is DocPicture)
    {
        (obj as DocPicture).TransparentColor = Color.Black;
    }
}

Step 4: Save the file.

doc.SaveToFile("Result.docx", FileFormat.Docx2013);

Screenshot:

Set transparent Color for Images in Word in C#

Full code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace Transeperant
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Document object
            Document doc = new Document();
            //Load the Word file
            doc.LoadFromFile("Input.docx");

            //Get the first paragraph in the first section
            Paragraph paragraph = doc.Sections[0].Paragraphs[0];

            //Set the black color of the image(s) in the paragraph to transperant
            foreach (DocumentObject obj in paragraph.ChildObjects)
            {
                if (obj is DocPicture)
                {
                    (obj as DocPicture).TransparentColor = Color.Black;
                }
            }

            //Save the file
            doc.SaveToFile("Result.docx", FileFormat.Docx2013);
        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Tuesday, 19 December 2023 03:21