locked
C# openxml sdk convert all wmf and emf images found in a docx into png and replace the occurences in all corresponding xmls. RRS feed

  • Question

  • I am using openxml sdk to read and manipulate docx.

    I am able to iterate through all the images found in a docx and my task is to replace all the wmf/emf images into png or jpg.

    string sourceFile="@D:\temp\sample.docx";
    using (WordprocessingDocument myDocument = WordprocessingDocument.Open(sourceFile, true))
    {
     var imageParts = myDocument.MainDocumentPart.ImageParts;
     foreach (ImagePart imagePart in imageParts)
     {
       var uri = imagePart.Uri;
       var filename = uri.ToString().Split('/').Last();
       if (imagePart.ContentType.Equals("image/x-wmf"))
       {
          Image img = Image.FromStream(imagePart.GetStream());
          /*
            Now I need to convert the image format from WMF or      
            EMF to PNG and replace the output in all the xml.
          */
       }
    
     }
    }

    Tuesday, May 19, 2015 6:40 AM

Answers

  • Hi rosh,

    >> Now I need to convert the image format from WMF or EMF to PNG and replace the output in all the xml.

    This forum is discussing about developer topics related to the Open XML Format SDK. For converting the image format from wmf or emf to png, it is more related with C#, and if you have issue about this, I will recommend you go to the forum below for help about C# issue.

    Reference: https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral

    For replacing the images with openxml, you could add an ImagePart to the document first, and then edit the Blip element to refer the new ImagePart. Some key code as below:

    public void ReplaceImage(Blip blipElement, FileInfo newImg)
            {
                string rid = AddImagePart(newImg);
                blipElement.Embed.Value = rid;
                this.OnImagesChanged();
            }
    
           string AddImagePart(FileInfo newImg)
            {
                ImagePartType type = ImagePartType.Bmp ;
                switch(newImg.Extension.ToLower())
                {
                    case ".jpeg":
                    case ".jpg":
                        type = ImagePartType.Jpeg;
                        break;
                   case ".png":
                        type = ImagePartType.Png;
                        break;
                    default:
                        type = ImagePartType.Bmp;
                        break;
                }
    
               ImagePart newImgPart = Document.MainDocumentPart.AddImagePart(type);
                using (FileStream stream = newImg.OpenRead())
                {
                    newImgPart.FeedData(stream);
                }
    
               string rId = Document.MainDocumentPart.GetIdOfPart(newImgPart);
                return rId;
            }

    The link below shows a code sample demonstrates how to replace images in an Office Word document using Openxml.

    # Use Open XML to manipulate images in Word (CSManipulateImagesInWordDocument)

    https://code.msdn.microsoft.com/office/CSManipulateImagesInWordDoc-312da7ef

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, May 20, 2015 9:47 AM