Add image to footer

Apr 19, 2012 at 12:01 PM
Edited Apr 19, 2012 at 12:03 PM

I have problems adding image to footer, this is my code:

 

        private static DocX AddFooter()
        {
            DocX document = DocX.Create(new MemoryStream());
            document.AddFooters();

            Footer footer = document.Footers.first;
            Image iso = document.AddImage(@"C:\iso.jpg");

            Picture pic1 = iso.CreatePicture();
            pic1.SetPictureShape(BasicShapes.cube);
            pic1.Width = 100;
            pic1.Height = 50;

            Paragraph pFooter = footer.InsertParagraph();
            pFooter.InsertPicture(pic1);

            document.Footers.first = footer;
            document.Footers.even = footer;
            document.Footers.odd = footer;
            document.Save();

            return document;
        }

But instead of the picture I get an empty rectangle with the message 'This image cannot currently be displayed". What's wrong with my code?

Edit - I am using Windows 7, .NET 4.0, Visual Studio 2010, DocX.dll version 1.0.0.12.

Developer
Apr 19, 2012 at 12:10 PM
Edited Apr 19, 2012 at 12:11 PM

Try it like that (more or less):

                    Image footer = documentWord.AddImage(Assembly.GetExecutingAssembly().GetManifestResourceStream("iso.jpg"));
                    Picture footerPicture = footer.CreatePicture();
                    footerPicture.Height = 45;
                    footerPicture.Width = 605;
                    documentWord.AddFooters();
                    Footer footerOdd = documentWord.Footers.odd;
                    Footer footerFirst = documentWord.Footers.first;
                    Paragraph picture_footer_odd = footerOdd.Paragraphs[0];
                    Paragraph picture_footer_first = footerFirst.Paragraphs[0];
                    picture_footer_odd.AppendPicture(footerPicture);
                    picture_footer_first.AppendPicture(footerPicture);
Coordinator
Apr 19, 2012 at 12:16 PM
simon,

in addition to MadBoys answer.

By default a Word document only has one footer, this is displayed on every page.
The default footer is the odd footer.

You only need to define the even and first footer if you want them to be different.
You also need to set the properties accordingly.

document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = true;

Here are some examples of how to use Headers and Footers

Happy coding,
Cathal
Apr 19, 2012 at 12:31 PM

Thanks to both of you, it works :)