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

Free Support Forum - aspose.com

Manipulate all images in a DOC file

Hi,
Is there a way to extract all images from a given doc file, do some manipulations on them (watermark, etc.) and put them back into the doc?

We may need to change the file types as well (original image = PNG, after transformation image should be JPG)?

Thanks.

Hi Pitky,

Thanks for your inquiry.

Images in Word documents are either represented by Shape nodes or by DrawingML nodes when loading into Aspose.Words’ DOM. Please read the members of DrawingML and Shape class from here:
http://www.aspose.com/docs/display/wordsnet/Shape+Members
http://www.aspose.com/docs/display/wordsnet/DrawingML+Members

Please use the GetShapeRenderer.Save method to save image into a stream or file. ImageSaveOptions allows to specify additional options when rendering document pages or shapes to images. Following code example shows how to save DrawingML and Shape to image file format (Jpeg) and insert it again into Document at same location.

Document doc = new
Document(MyDir + “in.docx”);<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

foreach (DrawingML dml in doc.GetChildNodes(NodeType.DrawingML, true))

{

MemoryStream stream = new MemoryStream();

//Save image to Jpeg

dml.GetShapeRenderer().Save(stream, new ImageSaveOptions(SaveFormat.Jpeg));

builder.MoveTo(dml);

//Insert image again

builder.InsertImage(stream);

dml.Remove();

}

foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))

{

MemoryStream stream = new MemoryStream();

//Save image to Jpeg

shape.GetShapeRenderer().Save(stream, new ImageSaveOptions(SaveFormat.Jpeg));

builder.MoveTo(shape);

//Insert image again

builder.InsertImage(stream);

shape.Remove();

}

doc.Save(MyDir + "Out.docx");