Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 792277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:58:00+00:00 2026-05-14T21:58:00+00:00

Following on from my last question here OpenXML looks like it probably does exactly

  • 0

Following on from my last question here

OpenXML looks like it probably does exactly what I want, but the documentation is terrible. An hour of googling hasn’t got me any closer to figuring out what I need to do.

I have a word document. I want to add an image to that word document (using word) in such a way that I can then open the document in OpenXML and replace that image. Should be simple enough, yes?

I’m assuming I should be able to give my image ‘placeholder’ an id of some sort and then use GetPartById to locate the image and replace it. Would this be the correct method? What is this Id? How do you add it using Word?

Every example I can find which does anything remotely similar starts by building the whole word document from scratch in ML, which really isn’t a lot of use.

EDIT: it occured to me that it would be easier to just replace the image in the media folder with the new image, but again can’t find any indication of how to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:58:01+00:00Added an answer on May 14, 2026 at 9:58 pm

    Although the documentation for OpenXML isn’t great, there is an excellent tool that you can use to see how existing Word documents are built. If you install the OpenXml SDK it comes with the DocumentReflector.exe tool under the Open XML Format SDK\V2.0\tools directory.

    Images in Word documents consist of the image data and an ID that is assigned to it that is referenced in the body of the document. It seems like your problem can be broken down into two parts: finding the ID of the image in the document, and then re-writing the image data for it.

    To find the ID of the image, you’ll need to parse the MainDocumentPart. Images are stored in Runs as a Drawing element

    <w:p>
      <w:r>
        <w:drawing>
          <wp:inline>
            <wp:extent cx="3200400" cy="704850" /> <!-- describes the size of the image -->
            <wp:docPr id="2" name="Picture 1" descr="filename.JPG" />
            <a:graphic>
              <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                <pic:pic>
                  <pic:nvPicPr>
                    <pic:cNvPr id="0" name="filename.JPG" />
                    <pic:cNvPicPr />
                  </pic:nvPicPr>
                  <pic:blipFill>
                    <a:blip r:embed="rId5" /> <!-- this is the ID you need to find -->
                    <a:stretch>
                      <a:fillRect />
                    </a:stretch>
                  </pic:blipFill>
                  <pic:spPr>
                    <a:xfrm>
                      <a:ext cx="3200400" cy="704850" />
                    </a:xfrm>
                    <a:prstGeom prst="rect" />
                  </pic:spPr>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>
      </w:r>
    </w:p>
    

    In the above example, you need to find the ID of the image stored in the blip element. How you go about finding that is dependent on your problem, but if you know the filename of the original image you can look at the docPr element:

    using (WordprocessingDocument document = WordprocessingDocument.Open("docfilename.docx", true)) {
    
      // go through the document and pull out the inline image elements
      IEnumerable<Inline> imageElements = from run in Document.MainDocumentPart.Document.Descendants<Run>()
          where run.Descendants<Inline>().First() != null
          select run.Descendants<Inline>().First();
    
      // select the image that has the correct filename (chooses the first if there are many)
      Inline selectedImage = (from image in imageElements
          where (image.DocProperties != null &&
              image.DocProperties.Equals("image filename"))
          select image).First();
    
      // get the ID from the inline element
      string imageId = "default value";
      Blip blipElement = selectedImage.Descendants<Blip>().First();
      if (blipElement != null) {
          imageId = blipElement.Embed.Value;
      }
    }
    

    Then when you have the image ID, you can use that to rewrite the image data. I think this is how you would do it:

    ImagePart imagePart = (ImagePart)document.MainDocumentPart.GetPartById(imageId);
    byte[] imageBytes = File.ReadAllBytes("new_image.jpg");
    BinaryWriter writer = new BinaryWriter(imagePart.GetStream());
    writer.Write(imageBytes);
    writer.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from my last question What is a Stub , I would really
Following on from this question , I now want to know how to stop
Following on from my recent question on Large, Complex Objects as a Web Service
Following on from this question what would be the best way to write a
Following on from the question asked by Mykroft Best way to handle input from
Following up from this question: How can I unlock a file that is locked
Following on from my question on using frozen Capistrano a couple of days back
Following on from this question I now have code that can attach to a
Following on from this question, what would be the best way to represent a
Following techniques from 'Modern C++ Design', I am implementing a persistence library with various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.