Removing the image placeholder (picture) in Word using OpenXML

470 views Asked by At

I have a Word file that I'm manipulating with OpenXML. I have nothing but a picture content control there and I have successfully added a picture ("content") into that picture content control.

Now everything is otherwise fine, but I don't have a slightest clue of how to remove the placeholder picture. That is, I have that "little icon with monitor, sun and mountain" there right in the middle of my inserted picture. You know, the icon that you see when you insert a picture content control. I can take the count of content controls thru VBA and it says there's exactly one, so there's not two controls on top of each other.

If I delete the target content control (with remove-method), the entire content control (including the correctly set picture) gets deleted.

Is there something like "placeholderimage.Hide"-method or something that I should use?

I set the content to the picture like this:

        DocumentFormat.OpenXml.Drawing.Blip blip = targetpicturecontrol.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault(); 
        blip.Embed = mainPart.GetIdOfPart(imagePart);
1

There are 1 answers

0
Arto Kilponen Os Kilponen On

This code hides (or removes, whatever) the place holder

private static void RemovePlaceHolder(SdtProperties targetproperties)
        {
            Appearance appearance = new Appearance();
            appearance.Val = new EnumValue<SdtAppearance>();
            SdtAppearance sdtAppearance = new SdtAppearance();
            sdtAppearance = SdtAppearance.Hidden;
            appearance.Val.Value = sdtAppearance;
            targetproperties.AppendChild(appearance);
        }