Free Support Forum - aspose.com

Insert Image in word document header

Hi ,

I am inserting an Image in the header of a word document for this I refferred

http://www.aspose.com/community/forums/thread/122151/insert-image-on-the-header-in-the-word-doc.aspx

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

Please suggest how to go about it

thanks

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

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

Aspose.Words.HeaderFooter firstHeader = doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst];

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

Aspose.Words.NodeCollection shapesFirstHeader = firstHeader.GetChildNodes(Aspose.Words.NodeType.Shape, true);

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).ImageData.SetImage(destinationImagePath1);

// (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,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Please try using the following code:

Document doc = new Document(@"Test090\in.docx");

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

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

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

NodeCollection shapesFirstHeader = firstHeader.GetChildNodes(NodeType.Shape, true);

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

foreach (Shape shape in shapesFirstHeader)

{

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;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

foreach (Shape shape in shapes)

{

if (shape.IsImage)

{

imagePrimary = shape;

break;

}

}

Shape imageFirst = null;

foreach (Shape shape in shapesFirstHeader)

{

if (shape.IsImage)

{

imageFirst = shape;

break;

}

}

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

May I ask you to attach your documents here for testing? I will check them and provide you more information.

Best regards,

Hi,

Yes, it works for DOC but not for DOCX document

Thank you

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

Please let me know if you have some special requirements. We will be glad to help you.

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,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Currently, there is no way to replace an image inside DrawingML object using Apsose.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,

Thanks for your inquiry.

Hopefully your work around will suffice for now. We will inform you when further members of the DrawingML class are made avaliable and what you are trying to achieve can be done directly.

Our apologises for any inconvenience.

Thanks,

The issues you have found earlier (filed as WORDSNET-3958) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(11)