Header Image Replacement Problem

Oct 31, 2011 at 6:29 PM

Hi All,

I'm currently having problem with replacing a header image in an existing document.

Currently I am adding the image to the document, creating a picture from the image (to set the width and height) then selecting the header paragraghs and replacing the images in each one (remove old, append new).

However, this is not working.  None of the images are replaced and instead I still have the default images.

Any help would be appreciated!

Cheers,

Kenny

 

>>Code<<

Novacode.Image logo = tendordoc.AddImage(data.CustomItems.LogoPath);
Novacode.Picture logo_picture = logo.CreatePicture();
logo_picture.Height = 50;
logo_picture.Width = 150;
                    
//Get Headers
Novacode.Header HeaderOdd = tendordoc.Headers.odd;
Novacode.Header HeaderFirst = tendordoc.Headers.first;
Novacode.Paragraph picture_header_odd = HeaderOdd.Paragraphs[0];
Novacode.Paragraph picture_header_first = HeaderFirst.Paragraphs[0];

//Remove Picture
picture_header_odd.Pictures[0].Remove();
picture_header_first.Pictures[0].Remove();

//Add new picture
picture_header_odd.AppendPicture(logo_picture);
picture_header_first.AppendPicture(logo_picture);

Developer
Nov 1, 2011 at 1:47 PM

Hi, momentai.

I tried to repeat your problem, but the same source code works fine on my PC. Could you attach docx file to which this source code does not work...

Nov 1, 2011 at 3:01 PM

Hi DragonFire,

I've uploaded my file to mediafire.

http://www.mediafire.com/?k03xlodzjhvd15u

It's for a project, so the text is pretty much garbled (hope you understand).  I'm attempting to change the header image on all the pages.

Thanks

Kenny

Developer
Nov 1, 2011 at 3:36 PM
// Load template
using (DocX document = DocX.Load(@"docs\LoremIpsumTemplate.docx"))
{
    // Get new picture
    Novacode.Image logo = document.AddImage(@"images\logo_template2.png");
    Novacode.Picture logo_picture = logo.CreatePicture();
    logo_picture.Height = 50;
    logo_picture.Width = 150;

    // Get Headers
    Novacode.Header HeaderOdd = document.Headers.odd;
    Novacode.Header HeaderFirst = document.Headers.first;
    Novacode.Paragraph picture_header_odd = HeaderOdd.Paragraphs[0];
    Novacode.Paragraph picture_header_first = HeaderFirst.Paragraphs[0];

    // Remove Picture
    picture_header_odd.Pictures[0].Remove();
    picture_header_first.Pictures[0].Remove();

    // Add new picture
    picture_header_odd.AppendPicture(logo_picture);
    picture_header_first.AppendPicture(logo_picture);

    // Save template as new document
    document.SaveAs(@"docs\LoremIpsumTemplateNew.docx");
}

This code opens your template and changes a picture. The code work fine. I suppose that problem is in data.CustomItems.LogoPath, becouse other code is the same. 
Nov 1, 2011 at 4:35 PM

Found out what it was!

The variable for the header and paragraph were going out of scope before the document was saved.  There is an IF statement to determine which logo would be displayed (default/customitem) where all the header/paragraph code lived in.  Move the Variable declarations out of the IF statement and voila.

Thank you for the help, DragonFire

// Load template
using (DocX document = DocX.Load(@"docs\LoremIpsumTemplate.docx"))
{
    if(CustomLogo.exists){
    // Get new picture
    Novacode.Image logo = document.AddImage(@"images\logo_template2.png");
    Novacode.Picture logo_picture = logo.CreatePicture();
    logo_picture.Height = 50;
    logo_picture.Width = 150;

    // Get Headers
    Novacode.Header HeaderOdd = document.Headers.odd;
    Novacode.Header HeaderFirst = document.Headers.first;
    Novacode.Paragraph picture_header_odd = HeaderOdd.Paragraphs[0];
    Novacode.Paragraph picture_header_first = HeaderFirst.Paragraphs[0];

    // Remove Picture
    picture_header_odd.Pictures[0].Remove();
    picture_header_first.Pictures[0].Remove();

    // Add new picture
    picture_header_odd.AppendPicture(logo_picture);
    picture_header_first.AppendPicture(logo_picture);
    }
    // Save template as new document
    document.SaveAs(@"docs\LoremIpsumTemplateNew.docx");
}
And cheers for making such a workable lib, Cathal!

Developer
Nov 1, 2011 at 4:50 PM

Glad to help you =)