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.

Fri Jul 01, 2011 8:44 am

Hi. I need to replace a dummy image from my document by another image (byte[]).
Problem 1: I'm using the Traverse a Document Tree as posted in forum so i can catch the document objects. My document have 3 pictures and few text but, only 1 object is returned by GetAllObjects(doc).
Problem 2: After resolving problem 1, how can i replace the image? I was trying like below.

...
IList<Spire.Doc.Interface.IDocumentObject> nodes = GetAllObjects(doc);

foreach (Spire.Doc.Interface.IDocumentObject node in nodes)
{
if (node.DocumentObjectType == Spire.Doc.Documents.DocumentObjectType.Picture)
{

//here i need to compare if found picture is the picture that i want to replace
Spire.Doc.Fields.DocPicture pictureNode = node as Spire.Doc.Fields.DocPicture;

if (pictureNode.ImageBytes.Length == DummySignature.Length)
{
//The node has the picture i want to replace! Now how can i replace it? In variable byte[] NewSignature i have the new signature.

}

}
}

Can you help me with this?

Best regards,
Nuno

nunojesus
 
Posts: 3
Joined: Thu Jun 30, 2011 2:28 pm

Mon Jul 04, 2011 4:51 am

Dear Nuno,
Thanks for your inquiry.
Here is a sample code.
Code: Select all
private struct DocPictureInfo
        {
            public ICompositeObject Parent { get; set; }
            public int Index { get; set; }
           // public IDocumentObject NewPicture { get; set; }
        }
 private void button1_Click(object sender, EventArgs e)
        {
//open document
            Document document = new Document(@"..\..\..\..\..\..\Data\Summary_of_Science.doc");

            //document elements, each of them has child elements
            Queue<ICompositeObject> nodes = new Queue<ICompositeObject>();
            nodes.Enqueue(document);

            //embedded images list.
            IList<Image> images = new List<Image>();

            IList<DocPictureInfo> forReplaced = new List<DocPictureInfo>();

            //traverse
            while (nodes.Count > 0)
            {
                ICompositeObject node = nodes.Dequeue();
                for (int i = 0, len = node.ChildObjects.Count; i < len; i++)

                //foreach (IDocumentObject child in node.ChildObjects)
                {
                    IDocumentObject child = node.ChildObjects[i];
                    if (child is ICompositeObject)
                    {
                        nodes.Enqueue(child as ICompositeObject);
                    }
                    else if (child.DocumentObjectType == DocumentObjectType.Picture)
                    {
                        DocPicture pic = (DocPicture)child;
                        if (pic.ImageBytes.Length == DummySignature.Length)
                        {
                            forReplaced.Add(new DocPictureInfo()
                            {
                                Parent = node,
                                Index = i
                            });
                        }
                        //if (pic.ImageBytes.Length == DummySignature.Length)
                        //{
                        //    pic.Image=//picture you want to replace

                        //    DocPicture picture = child as DocPicture;
                        //    images.Add(picture.Image);
                        //}
                    }
                }
            }

            foreach (DocPictureInfo dpi in forReplaced)
            {
                dpi.Parent.ChildObjects.RemoveAt(dpi.Index);
                //dpi.Parent.ChildObjects.Insert(dpi.Index, dpi.NewPicture);
                dpi.Parent.ChildObjects.Insert(dpi.Index,//the picture you want to replace to);
            }
}
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Return to Spire.Doc