Dhawal Chauhan Dhawal Chauhan - 1 year ago 129
C# Question

Extract Visio diagram from word document using open xml?

I am trying to parse the word document, It has visio diagram and images. I am not able to extract diagram and save it in visio format (.vsd, .vdx, .vsdx, .vsdm) Using C# Open XML.

Answer Source

I have found and answer to my own question. below is the code if any one its..

Below code will help you get all the image in word document.

foreach (ImagePart imagePart in doc.MainDocumentPart.ImageParts)` { var uri = imagePart.Uri; var IdR = doc.MainDocumentPart.GetIdOfPart(imagePart); string FileExtension = uri.OriginalString.Split('.').Last(); var filename = uri.ToString().Split('/').Last(); stream = doc.Package.GetPart(uri).GetStream(); Bitmap b = new Bitmap(stream); string FilePath = @"C:\test"." + FileExtension; b.Save(FilePath); }`

Below will help you get the embedded object in word document like: Visio, MP3 video.

var IdR = doc.MainDocumentPart.GetIdOfPart(embeddedobjectpart); string FileExtension = >embeddedobjectpart.Uri.OriginalString.Split('.').Last(); FileExtension = "vsd"; stream = doc.Package.GetPart(embeddedobjectpart.Uri).GetStream(); long length = stream.Length; byte[] byteStream = new byte[length]; stream.Read(byteStream, 0, (int)length); string FilePath =@"C:\test"." + "." + FileExtension; fstream = new FileStream(FilePath, FileMode.OpenOrCreate) fstream.Write(byteStream, 0, (int)length); fstream.Close();`

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download