Add image to footer

Apr 19, 2012 at 1:01 PM
Edited Apr 19, 2012 at 1: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 1:10 PM
Edited Apr 19, 2012 at 1: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 1: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 1:31 PM

Thanks to both of you, it works :)

Aug 26, 2013 at 11:45 AM
I believe the AppendPicture method solves this problem but I think I found the problem and the solution for the InsertPicture method as well:

The problem:
Inspecting the xml files, I discovered that the relationship Id in the footer file doesn't match the Id in the footerx.xml.rels file, which results in a broken image.
I believe this happens because when the (Paragraph.)InsertPicture method encounters an index of 0, then the Id generated by the GetOrGenerateRel(p) method is not used.

The solution:
Moving the following code down one bracket, so it's outside of the (first) else block, solves this problem:
// Extract the attribute id from the Pictures Xml.
XAttribute a_id =
(
    from e in p_xml.Descendants()
    where e.Name.LocalName.Equals("blip")
    select e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"))
).Single();

// Set its value to the Pictures relationships id.
a_id.SetValue(Id)
Even though the AppendPicture method solves this problem, I thought it might be useful to share this (I only found out about AppendPicture after figuring this out anyway...)
Besides, I'm not entirely certain if this really is a bug, could be the code was placed there for a reason. (Which is also why I didn't immediately submit this as a patch, let me know if I should though.)
Developer
Aug 26, 2013 at 2:20 PM
Hello Annika89,

Can you "again" provide this as a patch. I will be happy to integrate it. Latly I've created and example and added some fixes to images in tables in headers and footers (Allows pictures in headers and footers inside Tables using AppendPicture(). Credit goes to Slipy (Adam) but this solution uses again AppendPicture. If you can create a patch I will be happy to implement it and test it and prepare example of usage :)

With regards,
Przemek
Coordinator
Aug 26, 2013 at 4:06 PM
Annika,

This is a bug. I double check it this morning. Nice find!
Your solution is also perfect. I foresee no issues with it.

Please create and submit a patch.

Thanks,
Cathal
Aug 30, 2013 at 3:02 PM
Hi,
Just uploaded the patch. I'm terribly sorry for the delay, I was relying on the email notification (which doesn't seem to work if you don't check the box.. how strange... )
Developer
Aug 30, 2013 at 3:07 PM
Thank you. I will try to add it later today.

With regards,
Przemek
Developer
Aug 31, 2013 at 11:23 PM
Added. Thank you. Please verify when you have some time that it works for you. I've also added an example of usage/testing.
Sep 2, 2013 at 9:16 AM
Thanks, it works :)

The code from the first post, provided by simon, will work now if the image is added to each type of footer. Else (if you use the code above) only footer3.xml.rels is created, which will cause problems for the other types of footers. (The same will happen if you substitute 'first' with 'odd' or 'even' of course, only the created rels file will be different.)

An example of adding an image to all types of footers, similar to the code from simon:
private static DocX AddFooter()
{

    DocX document = DocX.Create(@"C:\Temp\DocxTools\Handtekening\testing_ex.docx");
    document.AddFooters();
    
    Novacode.Image iso = document.AddImage(@"C:\Temp\DocxTools\Handtekening\otho.gif");
    Picture pic1 = iso.CreatePicture();
    pic1.SetPictureShape(BasicShapes.cube);
    pic1.Width = 100;
    pic1.Height = 50;

    Footer footer1 = document.Footers.first;
    Paragraph pFooter = footer1.InsertParagraph();
    pFooter.InsertPicture(pic1);
    
    Footer footer2 = document.Footers.even;
    footer2.InsertParagraph().InsertPicture(pic1);
    
    document.Footers.odd.InsertParagraph().InsertPicture(pic1);

    document.Save();
    
    return document;    
}
Thanks again for patching this in :)
Developer
Sep 2, 2013 at 9:59 AM
Annika89 please contact me privately. Got a present for you for your recent commits :-)