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 7739397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:30:44+00:00 2026-06-01T08:30:44+00:00

I have code which works in all sorts of different situations, including when copying

  • 0

I have code which works in all sorts of different situations, including when copying images into the body of the document.

The code works when copying (adding) headers and footers from one document to the other, as long as the headers/footers being copied do not contain images.

When I copy a header which has an image in it, then the resulting file is corrupt, and when I try to open it with the OpenXML SDK it throws an exception saying “Compressed part has inconsistent data length”.
I do know that the image has to be created in the HeaderPart (as against the MainDocumentPart when copying into the body).

The code which does the merging of the image looks something like:

    private void AddSourceImagesToDestination(XElement sourceXml, OpenXmlPart sourcePart, OpenXmlPart destPart) {
      foreach(XElement drawingElement in sourceXml.Descendants(_mswDrawingElementName)) {

        XAttribute aBlipEmbedAttribute = drawingElement.Descendants(_ablipElementName).First().Attribute(_embedAttributeName);
        string relationshipId = aBlipEmbedAttribute.Value;
        ImagePart sourceImagePart = (ImagePart)sourcePart.GetPartById(relationshipId);
        ImagePart destinationImagePart = ((HeaderPart)destPart).AddImagePart(sourceImagePart.ContentType);
        string newRelationshipId = destPart.GetIdOfPart(destinationImagePart);
        aBlipEmbedAttribute.SetValue(newRelationshipId);

        destinationImagePart.FeedData(sourceImagePart.GetStream(FileMode.Open, FileAccess.Read));
      }  
  }

The above is called passing the source and destination HeaderParts, and the XML of the source header which will after this be copied into the destination document. After calling the above procedure, destinationHeaderPart.Header.Save() is called.

As I said above, if there are no images in the source header, then the resulting document is fine (i.e. when the foreach doesn’t find any drawing elements in the source XML).

I wonder, though, whether this symptom of the images in the header is perhaps a red herring and the real problem is somewhere else.

  • 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-06-01T08:30:46+00:00Added an answer on June 1, 2026 at 8:30 am

    As I said in the comment on the question, the code to include the images into the header and footer was fine – it did the trick.

    How I solved the problem of the corrupt file that my code (elsewhere) was creating was by a bit of trial and error. As other contributors have said, the documentation around OpenXML is, to put it mildly, not very good. So there might be another resolution to this problem, and maybe my “solution” just works because of some other side effects.

    Anyway, I have some code which looks like this:

        private MemoryStream _memoryStream;
        private WordprocessingDocument _wordDocument;
          ...
        _wordDocument = WordprocessingDocument.Open(_memoryStream, true);
          ... 
    
        private void ReopenDocument() {
          _wordDocument.Package.Flush();
          _wordDocument.Close();
          MemoryStream newStream = new MemoryStream();
          _memoryStream.WriteTo(newStream);
          _memoryStream.Close();
          _memoryStream = newStream;
          _memoryStream.Position = 0L;
          _wordDocument = WordprocessingDocument.Open(_memoryStream, true);
        }
    

    If I call the ReopenDocument method immediately prior to writing the _memoryStream to a FileStream, then the corruption is avoided.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code which works for radio buttons but need to be changed
I have the following code which works just fine when the method is POST,
I have the following code which works perfectly, but I want to allow access
I have some code here which works perfectly in firefox but not in chrome
I have the following code which works when i put it in any blank
At the moment I have the following code which works fine. label = new
I have trouble to get gluLookAt working. I have the following code which works
I have the following PHP code which works out the possible combinations from a
I have this code which compiles and works as expected: class Right {}; class
We've all been there. You have written some code and unit tests, the tests

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.