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

Many thanks for your co-operation

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();

Awaiting your reply

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.
alexey.noskov> please see the following link to learn how to create header.
Dinesh> ok
alexey.noskov> https://docs.aspose.com/words/net/working-with-headers-and-footers/
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> you can also insert your images into this header
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); 
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
NodeCollection shapes = myHeader.GetChildNodes(NodeType.Shape, true); 
(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); 
HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
NodeCollection shapes = myHeader.GetChildNodes(NodeType.Shape, true); 
(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> :slight_smile:
alexey.noskov> anyway this old version
Dinesh> I am working on ASP.net 1.1
alexey.noskov> I think you should try the latest version 5.1.0
alexey.noskov> you can use the latest version with 1.1
Dinesh> can you please give me a link to download
Dinesh> latest version
alexey.noskov> yes, wait a sec
Dinesh> thanks
alexey.noskov> https://downloads.aspose.com/words/net
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.

Please try using the following code instead yours.

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); 
HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary]; 
NodeCollection shapes = myHeader.GetChildNodes(NodeType.Shape, true);

(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

Thanks for your request. Please try adding Aspose.Words.Drawing namespace or using Aspose.Words.Drawing.Shape.

Best regards.

Hi,

Many thanks for your quick reply.

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]; 

myHeader is comming null.

HeaderFooter myHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary]; 
NodeCollection shapes = myHeader.GetChildNodes(NodeType.Shape, true);

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

Thanks again

Hi

There are three types of header in word document: HeaderPrimary, HeaderFirst and HeaderEven. Maybe your document doesn’t contains primary header.

Try using

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

Or

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

Also please attach your document for testing.

Best regards.

Many Thanks for your great help.

It was

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

My problem is solved

Thanks again