We're sorry Aspose doesn't work properply without JavaScript enabled.

# Insert Image on the header in the word doc

Hi alexey.noskov,

This is Dinesh, We were discussing about replacing Image on the header on the word doc using Aspose.

But for some reason chat was disconnected.

I tried your code but then you find that I am using older verison of Aspose,
I have downlaoded the latest version and it is now giving me errors at many places in the application

Mainly I have below 2 errors, I am also pasting my old code on which I am getting the errors

I am also attaching the chat which we had

My email is dinesh@bizzybit.co.uk

The type or namespace name ‘Word’ could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name ‘app’ could not be found (are you missing a using directive or an assembly reference?)

‘Aspose.Words.SaveFormat’ does not contain a definition for ‘FormatDocument’

// COde which was running with older version.
private Document OpenDoc()
{
Word app = new Words();
return app.Open(Server.MapPath("Templates_Office/dispatchnote.doc"));
}

Document doc;
doc = CreateInvoiceFromDataSet();
doc.Save(
"DispatchNote.doc",
SaveFormat.FormatDocument, // Is it fine If here I write "SaveFormat.Doc"
SaveType.OpenInBrowser, Response);
Response.End();


Regards,
Dinesh
dinesh@bizzybit.co.uk

Our Chat was:

You’re now talking to alexey.noskov from ‘Aspose.Words’ The topic is : Insert Image on the header in the word doc

Dinesh> hi
alexey.noskov> hi
Dinesh> I am working on ASP.net 1.1
Dinesh> and I use the Aspose.Words
Dinesh> It uses the word templete and fill the data into it
Dinesh> on the header there is a pic
Dinesh> I want to remove that image and add the image dynamically
Dinesh> how to di this
Dinesh> * do this
alexey.noskov> I think that you can use DocumentBuilder to achieve this.
Dinesh> ok
Dinesh> can I find the current image from the doc and replace it it with my pic in the header
alexey.noskov> you can get chield node of specified type
alexey.noskov> for example doc.GetChieldNodes(NodeType.Shape, true)
Dinesh> let me try with the code u sent and if there is problem then I will come back
Dinesh> for now, many thanks for your help
Dinesh> for example doc.GetChieldNodes(NodeType.Shape, true)
alexey.noskov> Please take a look the following code.
alexey.noskov> Document doc = new Document(@"Test116\in.doc");
Dinesh> which
alexey.noskov> HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
alexey.noskov> NodeCollection shapes = myHeader.GetChildNodes(NodeType.Shape, true);
alexey.noskov> shapes.Clear();
alexey.noskov> doc.Save(@"Test116\out.doc");
alexey.noskov> sorry, there are problems with multiline text in this chat
Dinesh> yes
alexey.noskov> this code removes all shapes from primary header of document
Dinesh> cool
alexey.noskov> DocumentBuilder builder = new DocumentBuilder(doc); builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); builder.InsertImage("myimg.jpg");
Dinesh> then for inserting the image on the top left
Dinesh> but need to insert the image at topp left
alexey.noskov> so you need to replace an existing image
Dinesh> yes
Dinesh> but in the above code
alexey.noskov> wait a second I’ll try to create an exampel for you
Dinesh> yeah many thanks
alexey.noskov> try using the following snippet
alexey.noskov> NodeCollection shapes = myHeader.GetChildNodes(NodeType.Shape, true); (shapes[0] as Shape).ImageData.SetImage(@"Test116\myimg.jpg");
Dinesh> ok
Dinesh> this looks good to me
alexey.noskov> let me know if this helps
Dinesh> ok
Dinesh> let me try
Dinesh> are you there
alexey.noskov> yes
Dinesh> My code is ready to test now
Dinesh> I wrote the code
alexey.noskov> good!
Dinesh>

Document doc = OpenDoc(strTemplate);
doc.MailMerge.ExecuteWithRegions(dataSet);
DocumentBuilder builder = new DocumentBuilder(doc);
(shapes[0] as Shape).ImageData.SetImage(HttpContext.Current.Server.MapPath("")+"\\Images\\assets\\logo_nucleus.gif");
doc.Save(HttpContext.Current.Server.MapPath("")+"\\pdf\\Supplier\\TestDoc.doc");


Dinesh> I get the following exceptions
Dinesh> The type or namespace name ‘myHeader’ could not be found (are you missing a using directive or an assembly reference?) ‘Aspose.Words.Shape’ does not contain a definition for ‘ImageData’
Dinesh> above has 2 errors
Dinesh> in which namespace I will get Header
Dinesh> My below code works fine
Dinesh>

Document doc = OpenDoc(strTemplate); doc.MailMerge.ExecuteWithRegions(dataSet);
doc.Save(HttpContext.Current.Server.MapPath("")+"\\pdf\\Supplier\\TestDoc.doc");


Dinesh> the above code is good
Dinesh> but when I write documentbuilder
Dinesh> then I get the errors
Dinesh> I understand it is not getting the reference for header
alexey.noskov> please try using the following
alexey.noskov>

Document doc = OpenDoc(strTemplate);
doc.MailMerge.ExecuteWithRegions(dataSet);
(shapes[0] as Shape).ImageData.SetImage(HttpContext.Current.Server.MapPath("") + "\\Images\\assets\\logo_nucleus.gif");
doc.Save(HttpContext.Current.Server.MapPath("") + "\\pdf\\Supplier\\TestDoc.doc");


Dinesh> ok
alexey.noskov> you just missed this HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
Dinesh> ‘Aspose.Words.Shape’ does not contain a definition for ‘ImageData’
Dinesh> yes
alexey.noskov> which version of Aspose.Words are you using?
Dinesh> using WebSupergoo.ABCpdf6;
Dinesh> wait
Dinesh> 1.4.9.0
Dinesh> sorry it os
Dinesh> is
Dinesh> 3.5.2.0
alexey.noskov>
alexey.noskov> anyway this old version
Dinesh> I am working on ASP.net 1.1
alexey.noskov> yes, wait a sec
Dinesh> thanks
Dinesh> thanks
Dinesh> let me downlaod and then try
alexey.noskov> ok

This message was posted using Aspose.Live 2 Forum

Hi

Sorry, internet connection was lost on my side. There are some differences in API.

The breaking changes in Aspose.Words for .NET 5.x.x are:

1. All collection classes, such as Paragraphs, Runs, Tables, Bookmarks etc have been renamed into ParagraphCollection, RunCollection, TableCollection, BookmarkCollection, CellCollection accordingly.

2. New namespaces Aspose.Words.Fields, Aspose.Words.Lists, Aspose.Words.Markup, Aspose.Words.Properties and Aspose.Words.Tables were introduced and classes moved into the new namespaces.

private Aspose.Words.Document OpenDoc()
{
Aspose.Words.Document doc = new Aspose.Words.Document(Server.MapPath("Templates_Office/dispatchnote.doc"));
return doc;
}

Document doc;
doc = CreateInvoiceFromDataSet();
doc.Save("DispatchNote.doc",
Aspose.Words.SaveFormat.Doc, // Is it fine If here I write "SaveFormat.Doc"
Aspose.Words.SaveType.OpenInBrowser, Response);
Response.End();


Best regards.

Hi Alexey Noskov,

Many thanks for your great help.

But I am still getting the same error

The type or namespace name ‘Shape’ could not be found (are you missing a using directive or an assembly reference?)

AT LINE

(shapes[0] as Shape).ImageData.SetImage(HttpContext.Current.Server.MapPath("") + “\Images\assets\logo_nucleus.gif”);

In below code:

Document doc = OpenDoc(strTemplate); doc.MailMerge.ExecuteWithRegions(dataSet);

(shapes[0] as Shape).ImageData.SetImage(HttpContext.Current.Server.MapPath("") + "\\Images\\assets\\logo_nucleus.gif");

doc.Save(HttpContext.Current.Server.MapPath("") + "\\pdf\\Supplier\\TestDoc.doc");


Now what is the problem

Regards
Dinesh

Hi

Best regards.

Hi,

I am now getting error

System.Web.HttpUnhandledException: Exception of type System.Web.HttpUnhandledException was thrown. —> System.NullReferenceException: Object reference not set to an instance of an object.

at line

NodeCollection shapes = myHeader.GetChildNodes(NodeType.Shape, true);


because

HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];



Why header is null when my doc has the header in it?

Thanks again

Hi

Try using

HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst];


Or

HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderEven];


HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst];