Editing Word images within Autoshapes using C#
Ok, this has had me stumped for a while - One of our clients would like to have all of their images replaced in a list of a word document. Finding images themselves is fine, with example code as follows:
Microsoft.Office.Interop.Word.Application wordDoc;
wordDoc = new Microsoft.Office.Interop.Word.Application();
object docName = @"c:\temp\Client.doc";
object objBool = false;
object objBoolTrue = true;
object objNull = System.Reflection.Missing.Value;
Document openDoc = null;
openDoc = wordDoc.Documents.Open(ref docName, ref objBool, ref objBool, ref objBool, ref objNull,ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);
List<Microsoft.Office.Interop.Word.Range> ranges = new List<Microsoft.Office.Interop.Word.Range>();
foreach (Microsoft.Office.Interop.Word.Shape s in openDoc.Shapes)
{
// do stuff here with comparing and replacing images
}
However I noticed that it wasn't recognising some images (irrespective of where they were, this is not a header or footer issue). On further research it appears that the image itself is not a normal imbedded image, but rather an Autoshape.
My code must therefore also be able to inspect the autoshapes in the document and change them if required as well. However for the life of me, I cannot determine how to inspect the image in an autoshape.
Finding the autoshapes themselves is fairly straightforward, as follows:
ranges = new List<Microsoft.Office.Interop.Word.Range>();
foreach (Microsoft.Office.Interop.Word.Shape s in openDoc.Shapes)
{
...
}
However once I identify the autoshape, I cannot determine 开发者_运维百科how I find information about the enclosed image.
Does anyone know how I would go about doing this?
You either have a grouped image, in which case you should recursively crawl through GroupItems
, or an inline image inside a textbox, in which case you should look inside the TextFrame.TextRange.ShapeRange
.
精彩评论