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

# Insert Image in word document header

Hi ,
I am inserting an Image in the header of a word document for this I refferred
https://forum.aspose.com/t/109419

I want after inserting an image in to header it should take it’s orginal width and height but it gets streched .

I tried with by

1. removing tag <a:stretch><a:fillRect/></a:stretch>
2. Also by setting height and width if shape to the height and width of image through code

but it didn’t work

Hi

Thanks for your inquiry. Could you please show me your code and attach your document and the image here for testing? I will check the problem on my side and provide you more information.
Best regards,

Hi Andrey Noskov ,
This is My code

string destinationImagePath1 = getLogoPath();
Aspose.Words.Document doc = new Aspose.Words.Document(inFileName);
try
{
(shapes[1] as Aspose.Words.Drawing.Shape).ImageData.SetImage(destinationImagePath1);
// (shapesFirstHeader[1] as Aspose.Words.Drawing.Shape).Height = imageProperty.Height;
// (shapesFirstHeader[1] as Aspose.Words.Drawing.Shape).Width = imageProperty.Width;
// (shapesFirstHeader[1] as Aspose.Words.Drawing.Shape).Height = imageProperty.Height;
// (shapesFirstHeader[1] as Aspose.Words.Drawing.Shape).Width = imageProperty.Width;
}
catch (Exception ex)
{
System.IO.File.AppendAllText("c:\\Log.txt", "_convertDoc2PDF_Exp ::" + ex.Message);
}
doc.SaveToPdf(outFileName);


I am attaching header xml and image
Thanks

Hi Janina,

Document doc = new Document(@"Test090\in.docx");
// Search for Image shapes. There could be also textboxes, which are also shapes,
// but textboxes do not have ImageData.
Shape imagePrimary = null;
foreach (Shape shape in shapes)
{
if (shape.IsImage)
{
imagePrimary = shape;
break;
}
}
Shape imageFirst = null;
{
if (shape.IsImage)
{
imageFirst = shape;
break;
}
}
// Create Image object.
Image img = Image.FromFile(@"C:\Temp\k.BMP");
// Get width and height of the image in points.
double width = ConvertUtil.PixelToPoint(img.Width, img.HorizontalResolution);
double height = ConvertUtil.PixelToPoint(img.Height, img.VerticalResolution);
if (imagePrimary != null)
{
imagePrimary.ImageData.SetImage(img);
imagePrimary.Height = height;
imagePrimary.Width = width;
}
if (imageFirst != null)
{
imageFirst.ImageData.SetImage(img);
imageFirst.Height = height;
imageFirst.Width = width;
}
doc.SaveToPdf(@"Test090\out.pdf");


Hope this helps,
Best regards,

The code below does not work with aspose.words version 9.5

Shape imagePrimary = null;
foreach (Shape shape in shapes)
{
if (shape.IsImage)
{
imagePrimary = shape;
break;
}
}
Shape imageFirst = null;
{
if (shape.IsImage)
{
imageFirst = shape;
break;
}
}


Hi

Thanks for your request. Probably this code does not work for you because you are using DOCX documents and your shapes are actually DrawingML. In 9.5.0 version we added partial support of DrawingML into Aspose.Words.
Best regards,

Hi,
Yes, it works for DOC but not for DOCX document
Thank you

Hi

Thank you for additional information. I was right; there is DrawingML in your document. DOC format does not support DrawingML that is why all DrawingML are converted to regular shapes after converting to DOC.
Best regards,

Hi,
Thank you for your reply, would you provide to me sample code how to insert image into DOCX document header
Best regards

Hi

Thanks for your request. The code provided above in this thread is a workaround of the problem with image size. If you need just insert an image into a header of your document, you can just move DocumentBuilder cursor into a header and insert an image using Documentbuilder.InsertImage method.
Best regards,

Hi,
What I need is to replace an existed image on DOCX template with an new image that will keep the size of the old one. the code above works perfectly for DOC document
Thank you

Hi Kamel,

Thank you for additional information. Currently, there is no way to replace an image inside DrawingML object using Aspose.Words. however, I can suggest you a simple workaround of this issue. You can just convert your document to DOC and then reopen DOC file using Aspose.Words. In this case, all DrawingML objects will be converted to Shapes and you will be able to use the code provided above to replace images.
Best regards,

Hi Alexey,
I have to use DOCX document because I’m using quick parts,I will use another way to update the image.

Document asposeWordsDoc = new Document(wordDoc);
DocumentBuilder asposeDocBuilder = new DocumentBuilder(asposeWordsDoc);
asposeDocBuilder.MoveToBookmark("bookMarkName");
asposeDocBuilder.InsertImage(img_name);
asposeDocBuilder.CurrentNode.Remove();


Thank you

Hi Kamel,