Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a picture of internet . I want save it in special part of word .

I find special part with this code .

  XNamespace w =
          "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
        XNamespace r =
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships";


        MainDocumentPart mainPart = wordDoc.MainDocumentPart;
        XDocument mainDocumentXDoc = GetXDocument(wordDoc);
        var q1 = mainDocumentXDoc
        .Root
        .Element(w + "body")
        .Elements();

        // project collection of tuples containing element and type
        var q2 = q1
            .Select(s =>
            {
                string keyForGroupAdjacent = ".NonContentControl";
                if (s.Name == w + "sdt")
                    keyForGroupAdjacent = (s.Element(w + "sdtPr").Element(w + "tag") != null) ? s.Element(w + "sdtPr").Element(w + "tag").Attribute(w + "val").Value : "";
                if (s.Name == w + "sectPr")
                    keyForGroupAdjacent = null;
                return new
                {
                    Element = s,
                    KeyForGroupAdjacent = keyForGroupAdjacent
                };
            }
            ).Where(s => s.KeyForGroupAdjacent != null);



            foreach (var item in q2)
        {
            if (item.Element.Value == "[متن]")
            {
                item.Element.AddFirst(**Picture**);
                break;
            }
        }

Who do add this picture in item ?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I find :)

I search imagepart with name (Picture 1 ). Then replace my new picture with old picture .

  IEnumerable<Blip> Blips = from run in mainPart.Document.Descendants<Drawing>()
                                      where
                                         run.Descendants<Inline>().First() != null
                                         &&
                                         run.Descendants<Inline>().First().DocProperties.Name != null
                                      &&
                                      run.Descendants<Inline>().First().DocProperties.Name.ToString() == "Picture 1"
                                      select run.Descendants<Blip>().First();
            string imageId = "default value";

            if (Blips != null || Blips.Count() > 0)
            {
                imageId = Blips.First().Embed.Value;
                ImagePart imagePart1 = (ImagePart)mainPart.GetPartById(imageId);
                imagePart1.FeedData(stream);
            }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.