This project is read-only.

Problem with images in Header/Footer



I am using this library to merge large numbers of genereted documents together. Sometimes these documents contain Picutres that repeat in every document. The library is supposed to reuse these images. This only works in the body, not in Header or Footers.
To solve this i have implemented a fix in die DocumentBuilder.cs File, for anyone who is having the same Problem. Maybe one of the developers can copy this fix into the sourccode...

I changed the Method ManageImageCopy to this:
 // General function for handling images that tries to use an existing image if they are the same
        private static ImageData ManageImageCopy(ImagePart oldImage, OpenXmlPart newContentPart, List<ImageData> images)
            ImageData oldImageData = new ImageData(oldImage);
            foreach (ImageData item in images)
                __if (newContentPart.GetType() != typeof(MainDocumentPart) && newContentPart.GetType() != typeof(HeaderPart) && newContentPart.GetType() != typeof(FooterPart) && newContentPart != item.ImagePart)__
                if (item.Compare(oldImageData))
                    return item;
            return oldImageData;
And fixed the Function CopyRelatedImage:
  private static void CopyRelatedImage(OpenXmlPart oldContentPart, OpenXmlPart newContentPart, XElement imageReference, XName attributeName,
            List<ImageData> images)
            string relId = (string)imageReference.Attribute(attributeName);

            if (string.IsNullOrEmpty(relId))

            // First look to see if this relId has already been added to the new document.
            // This is necessary for those parts that get processed with both old and new ids, such as the comments
            // part.  This is not necessary for parts such as the main document part, but this code won't malfunction
            // in that case.
            var tempPartIdPair5 = newContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId);
            if (tempPartIdPair5 != null)

            ExternalRelationship tempEr5 = newContentPart.ExternalRelationships.FirstOrDefault(er => er.Id == relId);
            if (tempEr5 != null)

            var ipp2 = oldContentPart.Parts.FirstOrDefault(ipp => ipp.RelationshipId == relId);

            if (ipp2 != null)
                ImagePart oldPart = (ImagePart)ipp2.OpenXmlPart;
                ImageData temp = ManageImageCopy(oldPart, newContentPart, images);

                if (temp.ImagePart == null)
                    ImagePart newPart = null;
                    if (newContentPart is MainDocumentPart)
                        newPart = ((MainDocumentPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is HeaderPart)
                        newPart = ((HeaderPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is FooterPart)
                        newPart = ((FooterPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is EndnotesPart)
                        newPart = ((EndnotesPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is FootnotesPart)
                        newPart = ((FootnotesPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ThemePart)
                        newPart = ((ThemePart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is WordprocessingCommentsPart)
                        newPart = ((WordprocessingCommentsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is DocumentSettingsPart)
                        newPart = ((DocumentSettingsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ChartPart)
                        newPart = ((ChartPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is NumberingDefinitionsPart)
                        newPart = ((NumberingDefinitionsPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is DiagramDataPart)
                        newPart = ((DiagramDataPart)newContentPart).AddImagePart(oldPart.ContentType);
                    if (newContentPart is ChartDrawingPart)
                        newPart = ((ChartDrawingPart)newContentPart).AddImagePart(oldPart.ContentType);
                    temp.ImagePart = newPart;
                    var id = newContentPart.GetIdOfPart(newPart);
                    temp.AddContentPartRelTypeResourceIdTupple(newContentPart, newPart.RelationshipType, id);
                    imageReference.Attribute(attributeName).Value = id;
                    var refRel = newContentPart.Parts.FirstOrDefault(pip =>
                        var rel = temp.ContentPartRelTypeIdList.FirstOrDefault(cpr =>
                            var found = cpr.ContentPart == newContentPart;
                            return found;

                        return rel != null;

                    if (refRel != null)
                        imageReference.Attribute(attributeName).Value = temp.ContentPartRelTypeIdList.First(cpr =>
                            var found = cpr.ContentPart == newContentPart;
                            return found;


                    var newId = "R" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
                    newContentPart.CreateRelationshipToPart(temp.ImagePart, newId);
                    __imageReference.SetAttributeValue(attributeName, newId);__
                ExternalRelationship er = oldContentPart.ExternalRelationships.FirstOrDefault(er1 => er1.Id == relId);
                if (er != null)
                    ExternalRelationship newEr = newContentPart.AddExternalRelationship(er.RelationshipType, er.Uri);
                    imageReference.Attribute( = newEr.Id;
                throw new DocumentBuilderInternalException("Source {0} is unsupported document - contains reference to NULL image");
Best Regards


EricWhiteDpe wrote Aug 4, 2015 at 4:56 PM

Hi Tristan,

This is a good fix. I appreciate it.

Good news - I am moving PowerTools to GitHub. I am projecting that this will be done in the next week or so.

Then, you can fork the repo, create a branch, put your code in the branch, submit a pull request, and I'll be happy to merge it. Is that acceptable?

One more thing - I have added a fair number of XUnit tests, including many for DocumentBuilder. These will be in the xUnit repo. It would be great if you would create XUnit tests that exercise the fixed functionality.

We're moving PowerTools into the future, and it will be great!


tgr86 wrote Aug 5, 2015 at 7:50 AM

Hi Eric,

thanks for your feedback. Its great to see that things are happing here :)
I will gladly entry the fix into a branch, as soon as github is up and running.

I will try and create a Unit-Test, but i am afraid I do not have the time at the moment.

Best Regards,