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 09, 2021 1:38 am

how to replace a image in header or footer, i search in documentation but i dont find this response

mestrinel
 
Posts: 7
Joined: Wed Apr 10, 2019 6:12 pm

Fri Jul 09, 2021 2:53 am

Hello,

Thanks for your inquiry.
please refer to the following code to meet your requirement.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"Template.docx");

            Image newImage = Image.FromFile(@"E-iceblue.png");
            DocPicture newPic = new DocPicture(doc);
            newPic.LoadImage(newImage);

            Section section = doc.Sections[0];
            HeaderFooter header = section.HeadersFooters.Header;

            foreach (Paragraph para in header.Paragraphs)
            {
                List<DocumentObject> pictures = new List<DocumentObject>();
               

                //Get all pictures in the header
                foreach (DocumentObject docObj in para.ChildObjects)
                {
                    if (docObj.DocumentObjectType == DocumentObjectType.Picture)
                    {
                        pictures.Add(docObj);
                    }
                }

                //Replace pitures with the 'newPic'"
                foreach (DocumentObject pic in pictures)
                {
                    int index = para.ChildObjects.IndexOf(pic);
                    para.ChildObjects.Insert(index, newPic);
                    para.ChildObjects.Remove(pic);
                }
            }

            doc.SaveToFile("ReplaceWithImage.docx", FileFormat.Docx);

If this is not what you want, please provide your input file and desired output for further investigation. You could send them to us ([email protected]) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Jul 14, 2021 2:37 pm

Hello, thanks for the feedback, I had already tried this approach, but Header.Paragraph doesn't have access to the ChildObjects method and so I can't validate the components of this property like the image below.


Only this item is missing for us to check the possibility of acquiring the library, thank you for your feedback.

Image

mestrinel
 
Posts: 7
Joined: Wed Apr 10, 2019 6:12 pm

Thu Jul 15, 2021 3:19 am

Hello,

Thanks for your feedback.
I tested the code that I provided above, but did not reproduce your problem. Here I uploaded my test project, please run it directly on your side to see if the issue occurs. Looking forward to your test results.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Jul 20, 2021 9:50 am

Hello,

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

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc

cron